The input is going to be split by separate lines, and I want to put each
into a separate element in an array. Now, this code works in irb (modified
to look more like str = soandso, str = str.to_s.split.....), but when I run
it in my program, everything gets thrown into one element in the array. Can
someone tell me why?
the #to_s method is unnecessary here, as #gets returns a String
already
if you're splitting on new lines ("\n"), everything you get with
`gets` will be in one element of the array, as there is only one new
line... similarly, if you change the string to be "soandsoandsoandso,"
it will still wind up as only one element of the array, as there is only
one new line...
The input is going to be split by separate lines, and I want to put each
into a separate element in an array. Now, this code works in irb (modified
to look more like str = soandso, str = str.to_s.split.....), but when I run
it in my program, everything gets thrown into one element in the array. Can
someone tell me why?
--
Reese Chappuis
--
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.ithttp://www.email.it/f
the #to_s method is unnecessary here, as #gets returns a String
already
if you're splitting on new lines ("\n"), everything you get with
`gets` will be in one element of the array, as there is only one new
line... similarly, if you change the string to be "soandsoandsoandso,"
it will still wind up as only one element of the array, as there is only
one new line...
No, it's the program, if you do indeed want to use gets. Thing is,
you're just using gets *once*, and gets just reads one line. I don't
see how you could have wound up with multi-line results from one gets
call in irb. Possibly the modifications you glossed over, were doing
a good bit more than that. Anyway, there are a couple alternate
suggestions that spring to mind.
One is to use a different input reading function that will read until
end-of-file, and then split it as you were doing. The user must press
^Z when done (^D on Mac or Linux). However, typical non-technical
end-users might not understand things like "press ^Z". (I'm guessing
this is really just an academic exercise so that wouldn't really
matter, but it's still a good idea to get into the habit of thinking
about what your users will understand or not.) Also, reading until
EOF in one chunk can be a dangerous habit to get into, as some joker
(or malicious attacker) may enter much more data than your program can
really handle. (Again, good habit to get into thinking about security
and scalability.)
A much better way is to put gets in a loop with some condition to end
it, such as entering a blank line. How to initialize the data and
execute that loop, are left as exercises for the reader.
-Dave
···
On Sat, Nov 12, 2011 at 21:42, Reese Chappuis <c0dege3k@gmail.com> wrote:
Ok, that does work- so the problem is with how the data is being inputted,
not with the program?
--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)
That's a good point & idea. I will take that into consideration and will
most likely use the loop like you suggested. Thanks.
···
On Sun, Nov 13, 2011 at 12:27 PM, Dave Aronson < rubytalk2dave@davearonson.com> wrote:
On Sat, Nov 12, 2011 at 21:42, Reese Chappuis <c0dege3k@gmail.com> wrote:
> Ok, that does work- so the problem is with how the data is being
inputted,
> not with the program?
No, it's the program, if you do indeed want to use gets. Thing is,
you're just using gets *once*, and gets just reads one line. I don't
see how you could have wound up with multi-line results from one gets
call in irb. Possibly the modifications you glossed over, were doing
a good bit more than that. Anyway, there are a couple alternate
suggestions that spring to mind.
One is to use a different input reading function that will read until
end-of-file, and then split it as you were doing. The user must press
^Z when done (^D on Mac or Linux). However, typical non-technical
end-users might not understand things like "press ^Z". (I'm guessing
this is really just an academic exercise so that wouldn't really
matter, but it's still a good idea to get into the habit of thinking
about what your users will understand or not.) Also, reading until
EOF in one chunk can be a dangerous habit to get into, as some joker
(or malicious attacker) may enter much more data than your program can
really handle. (Again, good habit to get into thinking about security
and scalability.)
A much better way is to put gets in a loop with some condition to end
it, such as entering a blank line. How to initialize the data and
execute that loop, are left as exercises for the reader.
-Dave
--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern
languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote
work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com(excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble!
(Aronson)
Da: Reese Chappuis [mailto:c0dege3k@gmail.com]
Inviato: lunedì 14 novembre 2011 00:04
A: ruby-talk ML
Oggetto: Re: Using gets and split
That's a good point & idea. I will take that into consideration and will
most likely use the loop like you suggested. Thanks.
On Sun, Nov 13, 2011 at 12:27 PM, Dave Aronson < rubytalk2dave@davearonson.com> wrote:
On Sat, Nov 12, 2011 at 21:42, Reese Chappuis <c0dege3k@gmail.com> wrote:
> Ok, that does work- so the problem is with how the data is being
inputted,
> not with the program?
No, it's the program, if you do indeed want to use gets. Thing is,
you're just using gets *once*, and gets just reads one line. I don't
see how you could have wound up with multi-line results from one gets
call in irb. Possibly the modifications you glossed over, were doing
a good bit more than that. Anyway, there are a couple alternate
suggestions that spring to mind.
One is to use a different input reading function that will read until
end-of-file, and then split it as you were doing. The user must press
^Z when done (^D on Mac or Linux). However, typical non-technical
end-users might not understand things like "press ^Z". (I'm guessing
this is really just an academic exercise so that wouldn't really
matter, but it's still a good idea to get into the habit of thinking
about what your users will understand or not.) Also, reading until
EOF in one chunk can be a dangerous habit to get into, as some joker
(or malicious attacker) may enter much more data than your program can
really handle. (Again, good habit to get into thinking about security
and scalability.)
A much better way is to put gets in a loop with some condition to end
it, such as entering a blank line. How to initialize the data and
execute that loop, are left as exercises for the reader.
-Dave
--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern
languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote
work.
See: davearonson.com (main) * codosaur.us (code) *
dare2xl.com(excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble!
(Aronson)
--
Reese Chappuis
--
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f
Sponsor:
Riccione Hotel 3 stelle in centro: Pacchetto Capodanno mezza pensione, animazione bimbi, zona relax, parcheggio. Scopri l'offerta solo per oggi...
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid982&d)-12