[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

displaying user inputed arrays

Isaac

2/17/2008 3:19:00 PM

[code]

puts "Ok now enter in 5 words, just be sure there are spaces inbetween
them"

i = 0
while i < 5
words_group = gets["","","","",""]
print "#{words_group[i]}"
i =+ 1
end

[/code]

What im trying to do is ask the user to input some words and display
them using arrays (learning program) i know you could do this easy other
ways but im trying to do it with arrays. What needs to be changed?

THanks in advance
--
Posted via http://www.ruby-....

37 Answers

Phlip

2/17/2008 3:31:00 PM

0

Isaac Toothyxdip wrote:

> words_group = gets["","","","",""]

words_group = gets.split(' ')

?

--
Phlip
http://www.oreillynet.com/ruby/blog/2008/02/as...


Wally Terrible

2/17/2008 7:04:00 PM

0

I'm not entirely sure what you intend to do. If you wanted to get five
separate words and then put them into an array:

words_group = []
while i<5
new_word = gets.chomp
words_group<< new_word
i = i +1
end
print words_group

or if you wanted to get all five words on the same line:

word_group = []
words_group = gets.chomp.split(" ")
print words_group

and to print them
(word_group.length).times {|i| print words_group[i]," "}


Isaac Toothyxdip wrote:
> [code]
>
> puts "Ok now enter in 5 words, just be sure there are spaces inbetween
> them"
>
> i = 0
> while i < 5
> words_group = gets["","","","",""]
> print "#{words_group[i]}"
> i =+ 1
> end
>
> [/code]
>
> What im trying to do is ask the user to input some words and display
> them using arrays (learning program) i know you could do this easy other
> ways but im trying to do it with arrays. What needs to be changed?
>
> THanks in advance
>


Morton Goldberg

2/17/2008 7:34:00 PM

0

On Feb 17, 2008, at 10:18 AM, Isaac Toothyxdip wrote:

> [code]
>
> puts "Ok now enter in 5 words, just be sure there are spaces
> inbetween them"
>
> i = 0
> while i < 5
> words_group = gets["","","","",""]
> print "#{words_group[i]}"
> i =+ 1
> end
>
> [/code]
>
> What im trying to do is ask the user to input some words and display
> them using arrays (learning program) i know you could do this easy
> other
> ways but im trying to do it with arrays. What needs to be changed?


Before you change anything you should reach a better understanding of
why your code won't work. The line

words_group = gets["","","","",""]

is interpreted by Ruby as

words_group = gets().[]("","","","","")

That is it means

1. Get a newline-terminated string from STDIN.
2. Call the [] method on that string, passing it five empty strings
as arguments.

The String method [] won't like getting five empty strings as its
argument.

Regards, Morton

Marc Heiler

2/17/2008 7:57:00 PM

0

What you should also not forget:

require 'pp'
pp your_array


I <3 pretty print
--
Posted via http://www.ruby-....

Siep Korteling

2/17/2008 9:07:00 PM

0

Wally T Terrible wrote:
> I'm not entirely sure what you intend to do. If you wanted to get five
>(...)
> (word_group.length).times {|i| print words_group[i]," "}


length.times ? With "each" you'll get the value at once, not the index.

word_group.each{|word| puts word}

Regards,

Siep

--
Posted via http://www.ruby-....

Wally Terrible

2/17/2008 9:21:00 PM

0

Siep Korteling wrote:
> Wally T Terrible wrote:
>
>> I'm not entirely sure what you intend to do. If you wanted to get five
>> (...)
>> (word_group.length).times {|i| print words_group[i]," "}
>>
>
>
> length.times ? With "each" you'll get the value at once, not the index.
>
> word_group.each{|word| puts word}
>
> Regards,
>
> Siep
>
>
This is definitely better. I was was looking at the original line that
used an index to print.
(Part of the issue was putting up a solution without really
understanding the problem. =))

Isaac

2/17/2008 9:25:00 PM

0

Im going to try the first solution because i seems like that would work.
But first im going to give a detailed explanation for what im trying to
do.

In the the terminal (im on mac) it askes the user to enter 5 words on
the same line

blah-macbook:~ blahblah$ ruby whatever.rb (before running)
(running program)
Please enter 5 words on the same line with spaces then press enter:
Bobo Momo Hoho Lolo Jojo
The five words you entered were: Bobo, Momo, Hoho, Lolo, Jojo
blah-macbook:~ blahblah$ (program finished)

but i want it to be in array forum and i think that the first reply
might work so i will try that and get back to you

--
Posted via http://www.ruby-....

7stud --

2/17/2008 11:46:00 PM

0

Isaac Toothyxdip wrote:
> Im going to try the first solution because i seems like that would work.
> But first im going to give a detailed explanation for what im trying to
> do.
>
> In the the terminal (im on mac) it askes the user to enter 5 words on
> the same line
>
> blah-macbook:~ blahblah$ ruby whatever.rb (before running)
> (running program)
> Please enter 5 words on the same line with spaces then press enter:
> Bobo Momo Hoho Lolo Jojo
> The five words you entered were: Bobo, Momo, Hoho, Lolo, Jojo
> blah-macbook:~ blahblah$ (program finished)
>
> but i want it to be in array forum and i think that the first reply
> might work so i will try that and get back to you

puts 'Please enter 5 words on the same line with spaces then press
enter:'

input = gets.chomp.split
p input

print 'The five words you entered were: '
puts input.join(' ')


--output:--
Please enter 5 words on the same line with spaces then press enter:
tom dick mo larry curly
["tom", "dick", "mo", "larry", "curly"]
The five words you entered were: tom dick mo larry curly
--
Posted via http://www.ruby-....

Isaac

2/18/2008 1:18:00 AM

0

Ok now im trying to ask for 5 words ON THE SAME LINE then it prints the
first word also once i get this to work then im going to make it where
it has 2 variables. one is the array and it's adding 1 so the value of
the array is diff. then ill use that same array as a text variable to
show which array your on.

First this is my code with help from the first reply

puts "Ok now enter in words just be sure there are spaces inbetween
them"
word_group = []
words_group = gets.chomp.split(" " + " ")
word_group<< words_group
print "The first word you entered was: #{word_group[0]}"

this is my goal of a running program:

____________________________________________________________________
|
|Ok now enter in 5 words just be sure there are spaces inbetween them: Jo, Mo, Lo, Ho, No
|The first word you entered was Jo followed by Mo
|____________________________________________________________________

Next this is what i want it to look like once that works
(im going to have it check for how many words there are and for ever
word it creates The (First, Second, Third, Etc) word you entered was
_______
and for the first, second, third, etc it will be the same var as the var
stating the array number like
i =+1
word[i]
so that the numbers will be the same and then it will add 1 for ever
word
____________________________________________________________________
|
|Ok now enter in 5 words just be sure there are spaces inbetween them: Jo, Mo, Lo, Ho, No
|The first word you entered was Jo
|The second word you entered was Mo
|The third word you entered was Lo
|The fourth word you entered was Ho
|The fifth word you entered was No
|_______________________________________________________________________________________________

yeah i understand that it might be hard to understand what im asking so
if more explaining is needed i will
--
Posted via http://www.ruby-....

darren kirby

2/18/2008 1:59:00 AM

0

quoth the Isaac Toothyxdip:
> Ok now im trying to ask for 5 words ON THE SAME LINE then it prints the
> first word also once i get this to work then im going to make it where
> it has 2 variables. one is the array and it's adding 1 so the value of
> the array is diff. then ill use that same array as a text variable to
> show which array your on.
>
> First this is my code with help from the first reply
>
> puts "Ok now enter in words just be sure there are spaces inbetween
> them"
> word_group = []
> words_group = gets.chomp.split(" " + " ")
> word_group<< words_group

This is really redundant. First you create an empty array. Then you input your
words to a different array, then you append your word array onto the end of
the empty array.

And this:
> " " + " "

just concatenates two spaces resulting in the call to 'split' being:

> words_group = gets.chomp.split(" ") # two spaces

Which won't split your input with 1 space between each word at all. The
default argument to 'split' is a single space, so try this:

> word_group = gets.chomp.split

which will replace your three lines above.

Use irb to explore and play with all these code snippets to get instant
feedback on what things do!

> print "The first word you entered was: #{word_group[0]}"
>
> this is my goal of a running program:
>
> ____________________________________________________________________
>
> |Ok now enter in 5 words just be sure there are spaces inbetween them: Jo,
> | Mo, Lo, Ho, No The first word you entered was Jo followed by Mo
> |____________________________________________________________________
>
> Next this is what i want it to look like once that works
> (im going to have it check for how many words there are and for ever
> word it creates The (First, Second, Third, Etc) word you entered was
> _______
> and for the first, second, third, etc it will be the same var as the var
> stating the array number like
> i =+1
> word[i]
> so that the numbers will be the same and then it will add 1 for ever
> word
> ____________________________________________________________________
>
> |Ok now enter in 5 words just be sure there are spaces inbetween them: Jo,
> | Mo, Lo, Ho, No The first word you entered was Jo
> |The second word you entered was Mo
> |The third word you entered was Lo
> |The fourth word you entered was Ho
> |The fifth word you entered was No
> |__________________________________________________________________________
> |_____________________
>
> yeah i understand that it might be hard to understand what im asking so
> if more explaining is needed i will

a = ["fifth", "fourth", "third", "second", "first"]
print "Gimme wordz: "; gets.chomp.split.each { |w| puts "The #{a.pop} word is
#{w}" }

Seriously though, try to break your problem into as little of units as
possible, and try to solve each one by playing around in irb and using the
online docs (links follow) if you don't have hard copies. I especially
recommend you read about blocks, and look at the various methods in
class "Array".

http://www.whytheluckystiff.net/rub...
http://www.rub...

-d

--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972