Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help with Arrays...

Reply
Thread Tools

Help with Arrays...

 
 
Jonathan ---
Guest
Posts: n/a
 
      08-17-2007
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/.

 
Reply With Quote
 
 
 
 
Dan Zwell
Guest
Posts: n/a
 
      08-17-2007
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

 
Reply With Quote
 
 
 
 
Jonathan Carbon
Guest
Posts: n/a
 
      08-17-2007
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/.

 
Reply With Quote
 
Dan Zwell
Guest
Posts: n/a
 
      08-17-2007
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

 
Reply With Quote
 
Jonathan Carbon
Guest
Posts: n/a
 
      08-17-2007
Dan,

Many thanks for the extended response. Its much appreciated.

J
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-17-2007
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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-17-2007
On Aug 17, 3:49 am, "Robert Klemme" <(E-Mail Removed)>
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


 
Reply With Quote
 
Peņa, Botp
Guest
Posts: n/a
 
      08-18-2007
From: William James [(E-Mail Removed)]=20
# From: Robert Klemme <(E-Mail Removed)>
# > 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

 
Reply With Quote
 
Brian Adkins
Guest
Posts: n/a
 
      08-18-2007
On Aug 17, 2:04 pm, William James <(E-Mail Removed)> 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'






 
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
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP - HELP - HELP =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?= ASP .Net 4 03-09-2006 12:46 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments