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

Discussion in 'Ruby' started by Kurt Euler, Sep 3, 2003.

  1. Kurt Euler

    Kurt Euler Guest

    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
    Kurt Euler, Sep 3, 2003
    #1
    1. Advertising

  2. 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.
    Daniel Carrera, Sep 3, 2003
    #2
    1. Advertising

  3. Kurt Euler

    Anita Date Guest

    "Kurt Euler" <> 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.>
    Anita Date, Sep 3, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollie
    Replies:
    0
    Views:
    367
    Ollie
    Oct 2, 2003
  2. Jon Schull

    how to bail if stdin is empty

    Jon Schull, May 31, 2004, in forum: Python
    Replies:
    2
    Views:
    386
    Antoon Pardon
    Jun 1, 2004
  3. Rudi
    Replies:
    5
    Views:
    5,006
  4. Pallav singh
    Replies:
    17
    Views:
    610
    Jerry Coffin
    Aug 7, 2009
  5. Heinrich Wolf
    Replies:
    3
    Views:
    402
    Heinrich Wolf
    Apr 29, 2012
Loading...

Share This Page