Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to bail from an iteration but not the program?...

Reply
Thread Tools

How to bail from an iteration but not the program?...

 
 
Kurt Euler
Guest
Posts: n/a
 
      09-03-2003
All-

How can I terminate the running of a program from within an iteration, but not leave the program?

EG, In this code:

IO.foreach("control.txt") { |x|
field = x.chop.split("\t", -1)
<Do stuff "A" here.>
<If some condition is met, leave this iteration BEFORE the end of control.txt file occurs and continue with stuff B>
}
<Do stuff "B" here.>
...
...
...
end


Thanks for your comments (and to those who assisted recently with the abort command).

-Kurt

 
Reply With Quote
 
 
 
 
Daniel Carrera
Guest
Posts: n/a
 
      09-03-2003
You can use 'break':

dcarrera ~ $ cat control.txt
line 1
line 2
line 3
line 4
line 5
dcarrera ~ $ cat test.rb
IO.foreach("control.txt") do |x|
puts "Stuff A goes here: #{x}"
break if x =~ /3/
end
puts "Stuff B goes here"
dcarrera ~ $ ruby test.rb
Stuff A goes here: line 1
Stuff A goes here: line 2
Stuff A goes here: line 3
Stuff B goes here
dcarrera ~ $

Cheers,
Daniel.

On Wed, Sep 03, 2003 at 12:23:20PM +0900, Kurt Euler wrote:
> All-
>
> How can I terminate the running of a program from within an iteration, but not leave the program?
>
> EG, In this code:
>
> IO.foreach("control.txt") { |x|
> field = x.chop.split("\t", -1)
> <Do stuff "A" here.>
> <If some condition is met, leave this iteration BEFORE the end of
> control.txt file occurs and continue with stuff B>
> }
> <Do stuff "B" here.>
> ...
> ...
> ...
> end
>
>
> Thanks for your comments (and to those who assisted recently with the abort command).
>
> -Kurt


--
Daniel Carrera, Math PhD student at UMD. PGP KeyID: 9AF77A88
.-"~~~"-.
/ O O \ ATTENTION ALL PASCAL USERS:
: s :
\ \___/ / To commemorate the anniversary of Blaise Pascal's
`-.___.-' birth (today) all your programs will run at half speed.


 
Reply With Quote
 
 
 
 
Anita Date
Guest
Posts: n/a
 
      09-03-2003

"Kurt Euler" <(E-Mail Removed)> wrote in message
>
> How can I terminate the running of a program from within an iteration, but

not leave the program?

Use break

> EG, In this code:


IO.foreach("control.txt") { |x|
field = x.chop.split("\t", -1)
<Do stuff "A" here.>
break if some_condition_is_met
}

<Do stuff "B" here.>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
French government to bail out their banks Aardvark Computer Support 7 10-22-2008 02:25 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
how to bail if stdin is empty Jon Schull Python 2 06-01-2004 10:58 AM
Steve-O: Volume III: Out On Bail DVD Review @ GENRE ONLINE.NET! Writer R5 DVD Video 0 12-08-2003 12:46 AM



Advertisments