How to bail from an iteration but not the program?

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

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.

“Kurt Euler” keuler@portal.com 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.>