displaying user inputed arrays

I

Isaac Toothyxdip

Isaac said:
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

actually this doesnt work the way i want it to.

when i try to retrieve and array from that i think that the scan thing
returns it as a integer other then the string i need why is this?

this is the code that im using to do this:

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

answered = false
while not answered
input_words = (gets.scan /[\w-]*/) - [""]
if input_words.length > 5
puts "Please type in FIVE words no more: "
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, word]
end
 
M

Martin DeMello

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

You're very close - it's simply a = gets.chomp.split (without the
square brackets). I recommend splitting on " " explicitly rather than
relying on it being the default.

a = gets.chomp.split(" ")

Note that what you did was create an array containing the return value
of gets.chomp.split:

input = "hello world"
a = input.split #=> ["hello", "world"]
a = [input.split] #=> [["hello", "world"]]

in the latter case, you get an array of length 1, whose only element
is an array of length 2.

martin
 
I

Isaac Toothyxdip

The gets.chomp.split(" ") works for checking the amount of words but now
for my code it doesnt return the words

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

answered = false
while not answered
input_words = gets.chomp.split(" ")
if input_words.length > 5
puts "Please type in FIVE words no more: "
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, word]
end

it just returns "The (place) word you entered was" and thats it
i think its because now that its not in the brackets that when i do
word its not returning the array word[whatever]
 
I

Isaac Toothyxdip

sorry i dont think you can edit posts...
i found out that the amount of words that you enter is the amount of
times it loops the question to pick a word
 
T

Todd Benson

Isaac said:
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

actually this doesnt work the way i want it to.

when i try to retrieve and array from that i think that the scan thing
returns it as a integer other then the string i need why is this?

this is the code that im using to do this:


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


answered = false
while not answered
input_words = (gets.scan /[\w-]*/) - [""]
if input_words.length > 5
puts "Please type in FIVE words no more: "

else
answered = true
end
end

a, n = nil
puts "Please enter 5 words"
until (a = (gets.scan /[\w'-]*/) - [""]).length == 5
puts "Please type in exactly FIVE words, thanks."
end
puts "You would like to print out which word number? (1-5)"
until (1..5).include? (n = gets.chomp.to_i)
puts "Please pick a number 1 through 5"
end
puts a[n - 1]

Todd
 
T

Todd Benson

Isaac said:
Thanks that works!

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

scan /[\w'-]*/

...returns an array of everything that matches the regular expression /[\w'-]*/

This returns every string that has zero or more of a word character
(\w), an apostrophe ('), or a hyphen (-). The - [""] part is to
subtract all empty strings from the array. I just noticed, however,
though that you can omit that last bit if you use /[\w'-]+/ instead
(the + instead of the *). That scans for _one_ or more, instead of
_zero_ or more characters.

Same code, but the scan changed...

a, n = nil
puts "Please enter 5 words"
until (a = gets.scan /[\w'-]+/).length == 5
puts "Please type in exactly FIVE words, thanks."
end
puts "You would like to print out which word number? (1-5)"
until (1..5).include? (n = gets.chomp.to_i)
puts "Please pick a number 1 through 5"
end
puts a[n - 1]

Todd
 
I

Isaac Toothyxdip

Thanks that really shortens up my code and works but i have a couple
questions



a, n = nil
puts "Please enter 5 words"
until (a = (gets.scan /[\w'-]*/) - [""]).length == 5
puts "Please type in exactly FIVE words, thanks."
end
puts "You would like to print out which word number? (1-5)"
until (1..5).include? (n = gets.chomp.to_i)
puts "Please pick a number 1 through 5"
end
puts a[n - 1]

i under stand almost all of it (i just dont get what the - [""] does and
i think that means that it puts it into an array but the main questions
i have are why did you type a, n = nil that isnt needed and also why did
you type /[\w'-]*/) when /\w+/ works the same ... i think
 
T

Todd Benson

Thanks that really shortens up my code and works but i have a couple
questions




a, n = nil
puts "Please enter 5 words"
until (a = (gets.scan /[\w'-]*/) - [""]).length == 5
puts "Please type in exactly FIVE words, thanks."
end
puts "You would like to print out which word number? (1-5)"
until (1..5).include? (n = gets.chomp.to_i)
puts "Please pick a number 1 through 5"
end
puts a[n - 1]

i under stand almost all of it (i just dont get what the - [""] does and

Try [1, 2, 3, 4] - [1, 4] to understand the methodology, - [""]
removes empty strings created by scan because /[\w'-]/ is not only
greedy, but _zero_or_more_, which means it will return empty strings
for characters you don't include (like spaces).
i think that means that it puts it into an array but the main questions
i have are why did you type a, n = nil that isnt needed

You're right. It isn't necessary. I thought it might be for scope reasons.
and also why did
you type /[\w'-]*/) when /\w+/ works the same ... i think

Yep, you're right accept you won't need the - [""] with /[\w'-]+/
because you won't get empty strings in the array with _one_or_more_ of
those characters (the + instead of the *).

I sort of tried to get that across with my last post.

I'm not saying the last code I posted (with +, not *, and without the
- [""]) is the best, but I like it because it sort of makes
grammatical sense.

cheers,
Todd
 
T

Todd Benson

Thanks that really shortens up my code and works but i have a couple
questions




a, n = nil
puts "Please enter 5 words"
until (a = (gets.scan /[\w'-]*/) - [""]).length == 5
puts "Please type in exactly FIVE words, thanks."
end
puts "You would like to print out which word number? (1-5)"
until (1..5).include? (n = gets.chomp.to_i)
puts "Please pick a number 1 through 5"
end
puts a[n - 1]

i under stand almost all of it (i just dont get what the - [""] does and

Try [1, 2, 3, 4] - [1, 4] to understand the methodology, - [""]
removes empty strings created by scan because /[\w'-]/ is not only
greedy, but _zero_or_more_, which means it will return empty strings
for characters you don't include (like spaces).

i think that means that it puts it into an array

I forgot to mention the #scan method is what creates an array.
but the main questions
i have are why did you type a, n = nil that isnt needed

You're right. It isn't necessary. I thought it might be for scope reasons.

and also why did
you type /[\w'-]*/) when /\w+/ works the same ... i think

I created a character class (the square brackets) that included \w and
' and - because "you're" is a word, as is "able-bodied". Like I said,
if you use the *, you will get several empty strings in the array the
the #scan method gives you, which you would have to subtract, but you
can bypass that ugliness with using the + instead.

hope that makes some sense,
Todd
 
I

Isaac Toothyxdip

Thanks you all for your help i have finished my code (so far) and just
have one question...
is this enough to be ready to move on to making a small game?

print "What is your name? "
name = gets.chomp
puts "Hello #{name}"

print "How many years old are you? "
years_old = gets.chomp.to_i
hours_old = (years_old * 365) * 24
puts "Your name is #{name} and your are #{hours_old} hours old"


answered = false #if person has answered y/n question
print "Do you want to know how many seconds old you are #{name}? (Y/N)"
while not answered #if the person hasnt answered (makes loop question if
wrong answer)
answer = gets.chomp
seconds_old = hours_old.to_i * 60 * 60

case answer.upcase[0] #makes first letter of the answer cap and and
array
when "Y"[0] #if first letter of answer is y, Y then... ([0] is
making y, Y first letter of array or answer)
puts "You are #{seconds_old} seconds old"
answered = true #leaves the while not loop
when "N"[0] #same as Y but with N
puts "Ok Then"
answered = true #leaves the while not loop
else
puts "Ummm...Yes or No?"
end
end

answered = false
puts "type in a number 1-100: "
while not answered
print_times = gets.chomp.to_i - 1
if print_times > 100
puts "Please enter a number between 1 and 100"
else
answered = true
end
end

answered = false
puts "ok now type in a word 1-10 letters long: "
while not answered
print_word = gets.chomp
if print_word.length > 10
puts "Please enter a word between 1 and 10 letters long"
else
answered = true
end
end
1.upto(print_times) do |q|
puts print_word + "\n"
end

places = ["first", "second", "third", "fourth", "fifth"]
puts "Please enter 5 words"
until (a = (gets.scan /\w+/) - [""]).length == 5
puts "Please type in exactly FIVE words, thanks."
end
puts "You would like to print out which word number? (1-5)"
until (1..5).include?(n = gets.chomp.to_i)
puts "Please pick a number 1 through 5"
end
puts "The #{places[n - 1]} word you choose was #{a[n - 1]}"
 
T

Tim Hunter

Isaac said:
Thanks you all for your help i have finished my code (so far) and just
have one question...
is this enough to be ready to move on to making a small game?

If coding up little programs like this interests you, I heartily
recommend Peter Cooper's _Beginning_Ruby_ book. It's got a lot of
fascinating examples aimed at people who are learning how to program in
Ruby.

Good luck!
 
I

Isaac Toothyxdip

Yeah i think its interesting to learn with but i hope to learn gui and
start making applications at some point.
 
I

Isaac Toothyxdip

Isaac said:
Yeah i think its interesting to learn with but i hope to learn gui and
start making applications at some point.

sorry i forgot to ask this what is the best to program gui in ruby?

ive heard of Tk and a few others im working on a mac so i would like one
that operates on all platforms and OS's.

The main problem is i dont really know where to start like what do i
need to do first and are there any good tutorials for coding GUI? Just
something to get started on a program that maybe creates text in a
window if a button is pressed maybe hello world?

Thanks in advance
 
T

Todd Benson

sorry i forgot to ask this what is the best to program gui in ruby?

ive heard of Tk and a few others im working on a mac so i would like one
that operates on all platforms and OS's.

The main problem is i dont really know where to start like what do i
need to do first and are there any good tutorials for coding GUI? Just
something to get started on a program that maybe creates text in a
window if a button is pressed maybe hello world?

Thanks in advance

I haven't played with it yet, but there's Shoes. You can find it at

http://code.whytheluckystiff.net/shoes/

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top