Displaying user inputed arrays


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

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

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

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

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]," "}

What you should also not forget:

require ‘pp’
pp your_array

I <3 pretty print

Siep K. wrote:

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. =))

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

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

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

quoth the Isaac T.:

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!

Next this is what i want it to look like once that works

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/ruby/pickaxe/

-d

Isaac T. 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

Isaac T. wrote:

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
|____________________________________________________________________

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

input = gets.chomp.split
print ‘The first word you entered was ’
puts input.join(’ followed by ')

–output:–
Please enter 5 words on the same line with spaces then press enter:
jack diane larry mo curly
The first word you entered was jack followed by diane followed by larry
followed by mo followed by curly


|
|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

You don’t write well enough yet to convey ideas in words. You
especially have to be a good writer when asking about computer
programming problems because the details are very important.

places = [‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
puts ‘Please enter 5 words on the same line with spaces then press
enter:’

input_words = gets.chomp.split

input_words.each_with_index do |word, i|
puts “The %s word you entered was %s” % [places[i], word]
end

–output:–
Please enter 5 words on the same line with spaces then press enter:
jack diane larry mo curly
The first word you entered was jack
The second word you entered was diane
The third word you entered was larry
The fourth word you entered was mo
The fifth word you entered was curly

7stud – wrote:

input_words.each_with_index do |word, i|

ok i under stand everything but this in the 2nd answer. I mean it must
be with telling it to go to the next array for i but idk could you tell
me a play by play of whats happening

Isaac T. wrote:

7stud – wrote:

input_words.each_with_index do |word, i|

ok i under stand everything but this in the 2nd answer. I mean it must
be with telling it to go to the next array for i but idk could you tell
me a play by play of whats happening

nevermind i understand this now

Ok im trying to check how many words the user inputs to see if its to
many like this (>>)

places = [‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
puts ‘Please enter 5 words on the same line with spaces then press
enter:’

Way wrote:

input_words = [gets.chomp.split]
answered = false
while not answered
if input_words.index.length > 5
puts “Please type in FIVE words”
else
answered = true
end
end

input_words.each_with_index do |word, i|
puts “To view a word you picked type:\n 1: first word\n 2: second
word\n 3: third word\n 4: fourth word\n 5: fifth word”
answered = false
while not answered
num_answer = gets.chomp
case num_answer[0]
when ‘1’[0]
i = 0
answered = true
when ‘2’[0]
i = 1
answered = true
when ‘3’[0]
i = 2
answered = true
when ‘4’[0]
i = 3
answered = true
when ‘5’[0]
i = 4
answered = true
else
puts “Try again…enter a number 1 - 5 to see one of the words
you typed”
end
end
puts “The %s word you entered was %s” % [places[i], word[i]]
end

ive tried many things including putting it after
input_word.each_with_index do |word, i| and if i is > 5 then do whatever
but i cant figure it out whats wrong with what is going on now because
it seems like its saying get the index of how ever many there are and
get the length of the index ( i think i could have just done index but i
was trying this when i decided to post this ) and if it is greater then
5 dont allow it and i looked at a array tut and you can do that with
index or maybe even with indexes

why doesnt it work?

Isaac T. wrote:

7stud – wrote:

input_words.each_with_index do |word, i|

ok i under stand everything but this in the 2nd answer. I mean it must
be with telling it to go to the next array for i but idk could you tell
me a play by play of whats happening

Isaac T. wrote:

7stud – wrote:

input_words.each_with_index do |word, i|

ok i under stand everything but this in the 2nd answer. I mean it must
be with telling it to go to the next array for i but idk could you tell
me a play by play of whats happening

These blocks are hard to grasp in the beginning. Once you get the hang
of it,
they are real easy. OK let’s go:

numbers=[“one”, “two”, “three”]

numbers now represents a list of values. This list is in a specific
order.
Ruby starts counting at zero, so :
0 “one”
1 “two”
2 “three”

The 0,1,2 are called indexes (also: indices). numbers[1] will give you
“two”.
The “one”, “two”, “three” bit is called: values.

You have seen what the method .each does in

numbers.each do |number|

It will stuff every value of “numbers”, one by one, in the variable
“number”.
Sometimes you want both the value and the index to do all kinds of smart
things; and there it is: each_with index.
But if we ask two things, we’d better have two variables to hold them.
Let’s call the second one “index”. (After a while you’ll probably get
tired of typing “index” all the time and just choose the lazy “i”).

numbers=[“one”, “two”, “three”]
numbers.each_with_index do |number, index|
print index + 1 # smart thing
puts ": " + number
puts
end

There is a LOT of methods to be learned. Most of them are surprisingly
simple, some of them have very powerfull features. You can google them
(ruby each_with_index), you can use ri (start a cmd box, or a session or
whatever in Linux; if it’s a black box with a blinking cursor you’ll be
fine. Type “ri each_with_index”. Or type “irb” and start toying). And
there is always http://tryruby.hobix.com/ to get you going.

On Feb 17, 2008 9:18 AM, Isaac T. [email protected] wrote:

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

It depends if you must have 5 words entered or if you just want the
first 5 words. In either case, look at the many available methods
split, compact, scan. Also…

((gets.scan /w’*/) - [“”])[0…4]

This simply takes all the words typed on a line (a space is a word),
deletes the spaces from the array, and takes the first five. I like
this better than the gets.chomp.split way because with
gets.chomp.split you will get punctuation (unless that is not
important to you).

With the other way, for example…
words = “hi there, how are you? i’m fine, thanks.”

words.chomp.split(" ")
=> [“hi”, “there,”, “how”, “are”, “you?”]

Note, the inclusion there of the comma and question mark.
Technically, I would think “you?” is not a word. Depends on what you
want to do.

Todd

Ok i tried using .length to check how many words the user inputed. It
works if you just do it like this:

a = [ “a”, “b”, “c”, “d” ]
if a.length > 3
puts “yeah”
else
puts “no”
end

but how come it doesnt work like this

a = [gets.chomp.split]
if a.length > 3
puts “yeah”
else
puts “no”
end

i think its cause of the split but what other alternatives are there?

Im not really wanting it to take only the first 5 words i want it to say
something if it is more then 5 words

print "Please type in 5 words with spaces inbetween them: "
answered = false
while not answered
a = [gets.chomp.split]
if a.length > 5
puts “Type in FIVE words no more:”
else
answered = true
end
end

but i know that the gets.chomp.split is wrong i have tried many ways
including split and index and indexes and rindex and many other ways but
i cant seem to get it to work. Im either using the wrong method or im
not following the correct syntax of the code.

Maybe now you have an idea of what im trying to do. Im going to sleep
for a little because im sick but im once i wake up im going to keep
working on it to try to find a way but any help will be very much
appreciated!

Thanks in advance

On Wed, Feb 20, 2008 at 1:30 PM, Todd B. [email protected]
wrote:

words_group = gets[“”,“”,“”,“”,“”]
THanks in advance

It depends if you must have 5 words entered or if you just want the
first 5 words. In either case, look at the many available methods
split, compact, scan. Also…

((gets.scan /w’*/) - [“”])[0…4]

Wrong regexp anyways. That should have been more like…

array = (gets.scan /[\w’-]*/) - [“”]
if array.length == 5
#do something
else
#do something else
#for just printing it out
#puts array or puts array.join(’ ')
end

I think you get the general idea, though. The #scan method is doing
the iterating for you (no need for while loop).

Todd

Thanks that works!

but one thing i dont really get what is going on with (gets.scan
/[\w’-]*/) - [""] ?

could you explain it? like every party i under stand the gets and i
guess the scan reads whats entered but i dont really understand the
/[\w-]*/) then i guess the - [""] is the array that what ever is
scanned is added to