Help with Arrays...

Discussion in 'Ruby' started by Jonathan ---, Aug 17, 2007.

  1. Jonathan ---

    Jonathan --- Guest

    I have the following code:

    words = []

    while gets.chomp != ''
    words.push gets.chomp
    end

    puts words.sort

    Its supposed to capture from the user, a bunch of words, until just
    enter is pressed, and put them in an array. When I run the code, it
    only enters every other word i type into the array. I cannot figure it
    out! I know this is simple for you all, so have pity on me! Im super
    new to ruby and programming in general.

    Thanks in advance.

    J
    --
    Posted via http://www.ruby-forum.com/.
     
    Jonathan ---, Aug 17, 2007
    #1
    1. Advertising

  2. Jonathan ---

    Dan Zwell Guest

    Jonathan --- wrote:
    > I have the following code:
    >
    > words = []
    >
    > while gets.chomp != ''
    > words.push gets.chomp
    > end


    Jonathan,

    ruby reads another line each time you call gets or gets.chomp (or
    gets.anything). You need to cache the result of the call to gets(). I
    would write:

    while line = gets.chomp && line != ""
    words.push line
    end

    Dan
     
    Dan Zwell, Aug 17, 2007
    #2
    1. Advertising

  3. Dan Zwell wrote:
    > Jonathan --- wrote:
    >> I have the following code:
    >>
    >> words = []
    >>
    >> while gets.chomp != ''
    >> words.push gets.chomp
    >> end

    >
    > Jonathan,
    >
    > ruby reads another line each time you call gets or gets.chomp (or
    > gets.anything). You need to cache the result of the call to gets(). I
    > would write:
    >
    > while line = gets.chomp && line != ""
    > words.push line
    > end
    >
    > Dan



    Hi Dan,

    thanks very much for the response. The program does not seem to get to
    end when just the enter is pressed...
    Im doing my best to understand...

    Sorry to be so thick!

    Thanks!


    --
    Posted via http://www.ruby-forum.com/.
     
    Jonathan Carbon, Aug 17, 2007
    #3
  4. Jonathan ---

    Dan Zwell Guest

    Jonathan Carbon wrote:
    > Hi Dan,
    >
    > thanks very much for the response. The program does not seem to get to
    > end when just the enter is pressed...
    > Im doing my best to understand...
    >
    > Sorry to be so thick!

    Not at all, the thickness is mine...

    >
    > Thanks!
    >
    >


    Giving advice without testing it is a bad habit of mine. I wasn't paying
    attention to Ruby's precedence. Apparently,
    while line = gets.chomp && line != ""
    needs some parentheses:
    while (line = gets.chomp) && line != ""

    However, there's another small gotcha. When you write a loop
    while line = gets.chomp; something; end
    users will often end the loop by hitting control-d, which means "end of
    file" (on unix/linux, at least). This causes gets() to return nil, and
    in that case, line.chomp throws an error (nil lacks the method chomp()).

    This does what you want and also handles the control-d case:
    while line=gets and line.chomp != ""
    Note that the chomp() is not cached in this case--in the body of the
    loop, you need to call chomp() when you refer to the line, or just call
    "line.chomp!". You could probably be more efficient, but often the Ruby
    way is either the most easily readable thing, or the most elegant.

    Dan
     
    Dan Zwell, Aug 17, 2007
    #4
  5. Jonathan Carbon, Aug 17, 2007
    #5
  6. I'd rather do this because the test is much easier:

    words = []

    while ( line = gets )
    words << line.chomp
    end

    However, there is a much shorter solution available:

    puts ARGF.readlines.sort

    Or even using "sort" directly (on a Unix like system):

    $ sort <<XXX
    > foo
    > bar
    > baz
    > XXX

    bar
    baz
    foo

    Kind regards

    robert
     
    Robert Klemme, Aug 17, 2007
    #6
  7. On Aug 17, 3:49 am, "Robert Klemme" <>
    wrote:
    > I'd rather do this because the test is much easier:
    >
    > words = []
    >
    > while ( line = gets )
    > words << line.chomp
    > end


    The original poster wants the loop to end when the user presses
    <Enter>
    without entering any text. How about this?

    words = []
    while line = gets and line.chomp! != ""
    words << line
    end
     
    William James, Aug 17, 2007
    #7
  8. Jonathan ---

    Peña, Botp Guest

    From: William James [mailto:]=20
    # From: Robert Klemme <>
    # > I'd rather do this because the test is much easier:
    # > words =3D []
    # > while ( line =3D gets )
    # > words << line.chomp
    # > end
    #=20
    # The original poster wants the loop to end when the user presses
    # <Enter> without entering any text. How about this?
    # words =3D []
    # while line =3D gets and line.chomp! !=3D ""
    # words << line
    # end

    in (my) practice, i usually go the simple way (like robert's), then =
    refine it as needed by just adding the lines.

    words =3D []
    while line=3Dgets # break on eof
    break if line.strip!.empty? # break on empty lines including spaces
    next if line !=3D /\w/ # special chars not allowed

    words << line # passed
    end

    sometimes, (as a nuby,) putting all conditions in one line may be =
    dangerous. it is dificult to test, difficult to comment, and one may =
    miss edge cases...=20
    i know it's not clever, but i value safety first, and it's cleaner and =
    clearer to me.. but ymmv.. i haven't reach the mile yet :)

    kind regards -botp
     
    Peña, Botp, Aug 18, 2007
    #8
  9. Jonathan ---

    Brian Adkins Guest

    On Aug 17, 2:04 pm, William James <> wrote:
    > The original poster wants the loop to end when the user presses
    > <Enter>
    > without entering any text. How about this?
    >
    > words = []
    > while line = gets and line.chomp! != ""
    > words << line
    > end


    Might as well simply evaluate the assignment expression directly:

    words = []
    while (line = gets.chomp) != ''
    words << line.chomp
    end
    puts words.sort

    While that fulfills the original spec, I agree with Dan's comment
    regarding <ctrl>-D or EOF. It would be nice to handle that to allow
    piping a file of words to the program. Before I noticed Robert's post,
    I came up with the following:

    puts ARGF.inject([]) {|memo,obj| memo << obj}.sort

    although fun, it's way too verbose. I ended up shortening Robert's to
    this:

    puts ARGF.sort

    If you don't want to create a .rb file, just:

    ruby -e 'puts ARGF.sort'

    :)
     
    Brian Adkins, Aug 18, 2007
    #9
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,666
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,263
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    569
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
  5. Philipp
    Replies:
    21
    Views:
    1,135
    Philipp
    Jan 20, 2009
Loading...

Share This Page