[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Noob Questions [arrays]

RoRNoob

4/16/2008 5:27:00 AM

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

Justin Collins

4/16/2008 5:44:00 AM

0

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

calderson

4/16/2008 6:22:00 AM

0

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

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.

Peña, Botp

4/16/2008 6:52:00 AM

0

From: calderson [mailto:chadalderson@gmail.com]=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

Ron Fox

4/16/2008 10:25:00 AM

0

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!

RoRNoob

4/16/2008 4:56:00 PM

0

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!

RoRNoob

4/16/2008 5:29:00 PM

0

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

Justin Collins

4/16/2008 9:49:00 PM

0

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

RoRNoob

4/16/2008 9:56:00 PM

0

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

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!

Kurt Nicklas

10/4/2011 9:55:00 PM

0

On Oct 4, 11:26 am, "6250 Dead, 1393 since 1/20/09" <d...@gone.com>
wrote:
> On Tue, 04 Oct 2011 04:26:17 -0700, Kurt Nicklas wrote:
> > On Oct 3, 12:43 am, "6250 Dead, 1393 since 1/20/09" <d...@gone.com>
> > wrote:
> >>http://www.guardian.co.uk/world/2011/oct/01/atheism-america-...
> >> right/print
>
> >> Rising atheism in America puts 'religious right on the defensive'
>
> > The "Guardian" needs to worry more about the rise of Islam in the UK.
> > Pretty
> > soon cities like Birmingham and Manchester will be ruled by sharia.
>
> Moslems would have to make up 90% of British population for that to
> happen,

90%? From where did you pull out that figure? Hint: your fat butt is
not a valid
source of information.

> and probably not even then. Most nations that ARE Islamic don't
> practice Sharia law over their own secular law.  
>
> America, on the other hand, is a nation convulsed by pseudo-serious
> discussions over whether evolution is "real" or Washington considered
> America a "Christian nation" (he didn't).  It has a lot more to worry
> about from home-grown religious zanies than Britain does from the 5% or
> so of it's population that is slowly assimilating there.  

Baloney. You're deliberately ignorant of Islamic fundamentalism abroad
and you
hate America.

Actually you have a number of things in common with Islamic
fundamentalists.

You try to make the world conform to your preconceived prejudices and
you
fantasize about the deaths of people who dare to hold different
beliefs from yours.

---------------------------------------

"I eat organic meat, and try to minimize my footprint in terms of car
useage. So get off your high horse: I'm not one of your little
strawmen
opponents."
------------ "Zepp" Jamieson not explaining why he doesn't eat tofu
and ride a bicycle 7/5/10


"Well, that's the funny thing about terrorists. If they get what
they
want, they stop being terrorists."
--David 'Zepp' Jamieson

Lee Curtis

10/5/2011 3:32:00 PM

0

Ray Fischer wrote:

> Kurt Nicklas <kurtnicklas@gmail.com> wrote:
> > On Oct 3, 12:43 am, "6250 Dead, 1393 since 1/20/09" <d...@gone.com>
> > wrote:
> >>
> http://www.guardian.co.uk/world/2011/oct/01/atheism-america-...
> >> right/print
> > >
> >> Rising atheism in America puts 'religious right on the defensive'
> >
> > The "Guardian" needs to worry more about the rise of Islam in the
> > UK.
>
> Why?
>
> > Pretty
> > soon cities like Birmingham and Manchester will be ruled by sharia.
>
> And there's more of that bigotry and hate that the religious have for
> people of other religions.


What if the religious fundies were to just try
to convert each other and leave the rest of us alone?

That is too much to ask, I guess.