Noob Questions [arrays]

R

RoRNoob

Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.

I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.

The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.

The program lives in a .rb file and is executed and ran from the
terminal.

Here is my code:

alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.push gets.chomp
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!
 
J

Justin Collins

RoRNoob said:
Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.

I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.

The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.

The program lives in a .rb file and is executed and ran from the
terminal.

Here is my code:

alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.push gets.chomp
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!

Take a look at your alphalist: it's just an empty array. When you do
alphalist.each, you are 'visiting' each item in the array, but there are
none. So it just skips down to 'puts alphalist.sort' and then exits.

-Justin
 
C

calderson

RoRNoob said:
Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.
I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.
The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.
The program lives in a .rb file and is executed and ran from the
terminal.
Here is my code:
alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.push gets.chomp
end
puts alphalist.sort
When I run the .rb file, nothing happens. Any help is appreciated!

Take a look at your alphalist: it's just an empty array. When you do
alphalist.each, you are 'visiting' each item in the array, but there are
none. So it just skips down to 'puts alphalist.sort' and then exits.

-Justin

OK that helped - I'm able to get the array to load up and print out
with this:

alphalist = ['']
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.push gets.chomp
puts alphalist.sort
end

But now I can't figure out how to keep it from printing until the user
enters an empty value. I've tried:

alphalist = ['']
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.push gets.chomp
if gets == ''
puts alphalist.sort
end
end

.... it prints a gets value twice, then starts again with "type words
one at a time and I will alphabetize them for you."

If i hit enter it just puts a blank line.
 
P

Peña, Botp

From: calderson [mailto:[email protected]]=20
# alphalist =3D ['']
# alphalist.each do |item|
# puts 'type words one at a time and I will alphabetize them for you.'
# alphalist.push gets.chomp

you run gets here ^^^^

# if gets =3D=3D ''

and here ^^^

that will execute gets twice
the gets =3D=3D '' will never be true so the ff line is never run

# puts alphalist.sort
# end
# end
#=20
# ... it prints a gets value twice, then starts again with "type words
# one at a time and I will alphabetize them for you."
# If i hit enter it just puts a blank line.


how about trying something simple

alphalist =3D []
loop do
puts 'type words one at a time and I will sort them for you.'
input =3D gets.strip
exit if input.empty?
puts "-----o-----"
puts alphalist.push(input).sort
end

kind regards -botp
 
R

Ron Fox

Approach it as follows;
1. A loop that is not a .each loop that reads in user input
until the input is an empty line (that is size 0 after being chomped)
2. Then sort the array.
3. Then a .each iteration through the array to output the contents.
 
R

RoRNoob

Approach it as follows;
1. A loop that is not a .each loop that reads in user input
until the input is an empty line (that is size 0 after being chomped)
2. Then sort thearray.
3. Then a .each iteration through thearrayto output the contents.

RF
Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.
I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.
The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.
The program lives in a .rb file and is executed and ran from the
terminal.
Here is my code:
alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.pushgets.chomp
end
puts alphalist.sort
When I run the .rb file, nothing happens. Any help is appreciated!

Cool - I will give that a try - thanks for your time everyone!
 
R

RoRNoob

Approach it as follows;
1. A loop that is not a .each loop that reads in user input
until the input is an empty line (that is size 0 after being chomped)
2. Then sort thearray.
3. Then a .each iteration through thearrayto output the contents.

RoRNoob said:
Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.
I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.
The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.
The program lives in a .rb file and is executed and ran from the
terminal.
Here is my code:
alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.pushgets.chomp
end
puts alphalist.sort
When I run the .rb file, nothing happens. Any help is appreciated!

Cool - I will give that a try - thanks for your time everyone!

OK progress:

alphalist = ['']
input = ''

while input != 'bye'
puts 'type words one at a time and I will alphabetize them for you.'
input = gets.chomp
if input != 'bye'
alphalist.push input
end
end

puts alphalist.sort


-----

My problem is that I can't figure out how to make it exit the loop if
the user submits a 0 length string. The only way I've been able to get
it to work is to watch for 'bye' - which I like better than the empty
string, but the problem from the book calls for an empty string loop
exit
 
J

Justin Collins

RoRNoob said:
Approach it as follows;
1. A loop that is not a .each loop that reads in user input
until the input is an empty line (that is size 0 after being chomped)
2. Then sort thearray.
3. Then a .each iteration through thearrayto output the contents.

RF

RoRNoob wrote:

Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.

I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.

The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.

The program lives in a .rb file and is executed and ran from the
terminal.

Here is my code:

alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.pushgets.chomp
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!
Cool - I will give that a try - thanks for your time everyone!

OK progress:

alphalist = ['']
input = ''

while input != 'bye'
puts 'type words one at a time and I will alphabetize them for you.'
input = gets.chomp
if input != 'bye'
alphalist.push input
end
end

puts alphalist.sort


-----

My problem is that I can't figure out how to make it exit the loop if
the user submits a 0 length string. The only way I've been able to get
it to work is to watch for 'bye' - which I like better than the empty
string, but the problem from the book calls for an empty string loop
exit

I suspect your difficulty comes from the fact that you are setting input
to the empty string before entering the while loop. One approach would
be to start off with input as something else, like nil, then doing while
input != ""

-Justin
 
R

RoRNoob

RoRNoob said:
Approach it as follows;
1. A loop that is not a .each loop that reads in user input
until the input is an empty line (that is size 0 after being chomped)
2. Then sort thearray.
3. Then a .each iteration through thearrayto output the contents.
RF
RoRNoob wrote:
Be warned - I'm new to Ruby and programming in general, so I'm sure
this will be a trivial question for most of you.
I'm currently working through the exercises in the book Learn to
Program by Chris Pine. At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I've been able
to follow along just fine up until tonight.
The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.
The program lives in a .rb file and is executed and ran from the
terminal.
Here is my code:
alphalist = []
alphalist.each do |item|
puts 'type words one at a time and I will alphabetize them for you.'
alphalist.pushgets.chomp
end
puts alphalist.sort
When I run the .rb file, nothing happens. Any help is appreciated!
Cool - I will give that a try - thanks for your time everyone!
OK progress:
alphalist = ['']
input = ''
while input != 'bye'
puts 'type words one at a time and I will alphabetize them for you.'
input = gets.chomp
if input != 'bye'
alphalist.push input
end
end
puts alphalist.sort

My problem is that I can't figure out how to make it exit the loop if
the user submits a 0 length string. The only way I've been able to get
it to work is to watch for 'bye' - which I like better than the empty
string, but the problem from the book calls for an empty string loop
exit

I suspect your difficulty comes from the fact that you are setting input
to the empty string before entering the while loop. One approach would
be to start off with input as something else, like nil, then doing while
input != ""

-Justin

Solved - Thanks for the suggestion Justin. For any other noobs - here
is the solution:

alphalist = ['']
input = nil

while input != ""
puts 'type words one at a time and I will alphabetize them for you.'
input = gets.chomp
alphalist.push input
end

puts alphalist.sort

Cheers!
 

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,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top