Noob Questions [arrays]

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!

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

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.

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.push gets.chomp
end
puts alphalist.sort

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

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.

···

On Apr 15, 10:43 pm, Justin Collins <justincoll...@ucla.edu> wrote:

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

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

···

On Apr 16, 3:25 am, Ron Fox <f...@nscl.msu.edu> wrote:

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!

# alphalist = ['']
# 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 == ''

and here ^^^

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

# puts alphalist.sort
# end
# end

···

From: calderson [mailto:chadalderson@gmail.com]
#
# ... 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 = []
loop do
  puts 'type words one at a time and I will sort them for you.'
  input = gets.strip
  exit if input.empty?
  puts "-----o-----"
  puts alphalist.push(input).sort
end

kind regards -botp

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

···

On Apr 16, 9:55 am, RoRNoob <rorn...@gmail.com> wrote:

On Apr 16, 3:25 am, Ron Fox <f...@nscl.msu.edu> wrote:

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

-----

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

RoRNoob wrote:

···

On Apr 16, 9:55 am, RoRNoob <rorn...@gmail.com> wrote:
  

On Apr 16, 3:25 am, Ron Fox <f...@nscl.msu.edu> wrote:

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!

···

On Apr 16, 2:49 pm, Justin Collins <justincoll...@ucla.edu> wrote:

RoRNoob wrote:
> On Apr 16, 9:55 am, RoRNoob <rorn...@gmail.com> wrote:

>> On Apr 16, 3:25 am, Ron Fox <f...@nscl.msu.edu> wrote:

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