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

K

Kurt Euler

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
 
D

Daniel Carrera

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.

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.
 
A

Anita Date

Kurt Euler said:
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.>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top