[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Adding user input to a hash

John Maclean

2/6/2006 11:17:00 AM

Chaps,

Say you've got an hash that you are using to capturing user input....
nearly there:-

#!/usr/bin/ruby -w
def testwrite
ask_list = {
'Your name' => 'Nil',
'Your dob' => 'Nil',
}
ask_list.each_key {
|z| puts "#{z} : "
user_input = gets.chomp
if user_input.chomp! != "qq"
ask_list.each_value = user_input # this line don't work
end
}
end

testwrite


--
John Maclean
MSc (DIC)
07739 171 531



17 Answers

Levin Alexander

2/6/2006 11:32:00 AM

0

On 2/6/06, John Maclean <info@jayeola.org> wrote:> if user_input.chomp! != "qq"> ask_list.each_value = user_input # this line don't workHow could it, you are trying to assign to a method.Try this: questions = %w{Name Job} answers = questions.inject({}) { |hash, question| print "#{question}: " answer = gets.chomp break hash if answer == "q" hash[question] = answer hash } p answers-Levin

Mike Stok

2/6/2006 11:33:00 AM

0


On 6-Feb-06, at 6:16 AM, John Maclean wrote:

> def testwrite
> ask_list = {
> 'Your name' => 'Nil',
> 'Your dob' => 'Nil',
> }
> ask_list.each_key {
> |z| puts "#{z} : "
> user_input = gets.chomp
> if user_input.chomp! != "qq"
> ask_list.each_value = user_input # this line don't work

Do you mean

ask_list[z] = user_input

here?

> end
> }
> end
>
> testwrite

Mike

--

Mike Stok <mike@stok.co.uk>
http://www.stok.co...

The "`Stok' disclaimers" apply.






John Maclean

2/6/2006 11:54:00 AM

0

whoa! Thanks. It works but asa novice to this language I would never
have known of questions.inject({}) { |hash, question| ... pouring over
this line and ri Hash.

On Mon, 6 Feb 2006 20:32:13 +0900 Levin Alexander
<levin@grundeis.net> wrote:

> On 2/6/06, John Maclean <info@jayeola.org> wrote:
>
> > if user_input.chomp! != "qq"
> > ask_list.each_value = user_input # this line don't work
>
> How could it, you are trying to assign to a method.
>
> Try this:
>
> questions = %w{Name Job}
> answers = questions.inject({}) { |hash, question|
> print "#{question}: "
> answer = gets.chomp
> break hash if answer == "q"
> hash[question] = answer
> hash
> }
> p answers
>
> -Levin


--
John Maclean
MSc (DIC)
07739 171 531



Robert Klemme

2/6/2006 12:39:00 PM

0

John Maclean wrote:
> Chaps,
>
> Say you've got an hash that you are using to capturing user input....
> nearly there:-
>
> #!/usr/bin/ruby -w
> def testwrite
> ask_list = {
> 'Your name' => 'Nil',
> 'Your dob' => 'Nil',
> }
> ask_list.each_key {
> |z| puts "#{z} : "
> user_input = gets.chomp
> if user_input.chomp! != "qq"
> ask_list.each_value = user_input # this line don't work
> end
> }
> end
>
> testwrite

I'd choose a completely different design: I'd have questions in a list
(Arry) and answers in another list (Array) or Hash.

You could do:

questions = [
'Your name',
'Your job',
]

answers = questions.map |q|
puts q
gets.chomp
end

Reason is that your question list is likely constant over time and your
answers depend on each run. If you put your original solution into a loop
for different users then you end up having old answers from someone still
in the hash. Concurrency won't work either.

Kind regards

robert

John Maclean

2/7/2006 12:15:00 PM

0

Thanks for that reply. I think that I will go for the two array
method. One for the input and the other to save the data. On a
side-note but related issue I'd like to save this data to a file. The
name of the filename to be created is based on the user input....

jayeola@tp20$ cat testwrite_using_filename.rb
#!/usr/bin/ruby -wv
def testwrite_using_array
ask_list = [
'Your name',
'Your stuff',
'Your foo',
]
array_counter = 0
user_response = []
ask_list.each {
|z| puts "#{z} : "
xx = gets.chomp
if xx.chomp! != "qq"
array_counter += 1
user_response += "#{xx}".to_a
# puts array_counter # check number of elements in array
end

}
puts user_response
# name of next variable used user respose....we need to save this
data to a file name like "name + age + foo".txt
filename =user_response[1..3].to_s
puts "filename #{filename}.txt created"
File.open(filename, "a") { "#{user_response}" }
end

testwrite_using_array


# So far the file is created, but nothing's in it - it's empty...I know
I'll get there. Just need a few pointers. Any ideas chaps?



On Mon,
6 Feb 2006 21:43:21 +0900 "Robert Klemme" <bob.news@gmx.net> wrote:

> John Maclean wrote:
> > Chaps,
> >
> > Say you've got an hash that you are using to capturing user
> > input.... nearly there:-
> >
> > #!/usr/bin/ruby -w
> > def testwrite
> > ask_list = {
> > 'Your name' => 'Nil',
> > 'Your dob' => 'Nil',
> > }
> > ask_list.each_key {
> > |z| puts "#{z} : "
> > user_input = gets.chomp
> > if user_input.chomp! != "qq"
> > ask_list.each_value = user_input # this line don't work
> > end
> > }
> > end
> >
> > testwrite
>
> I'd choose a completely different design: I'd have questions in a list
> (Arry) and answers in another list (Array) or Hash.
>
> You could do:
>
> questions = [
> 'Your name',
> 'Your job',
> ]
>
> answers = questions.map |q|
> puts q
> gets.chomp
> end
>
> Reason is that your question list is likely constant over time and
> your answers depend on each run. If you put your original solution
> into a loop for different users then you end up having old answers
> from someone still in the hash. Concurrency won't work either.
>
> Kind regards
>
> robert
>
>
>
>


--
John Maclean
MSc (DIC)
07739 171 531



Robert Klemme

2/7/2006 12:22:00 PM

0

John Maclean wrote:
> Thanks for that reply. I think that I will go for the two array
> method. One for the input and the other to save the data. On a
> side-note but related issue I'd like to save this data to a file. The
> name of the filename to be created is based on the user input....
>
> jayeola@tp20$ cat testwrite_using_filename.rb
> #!/usr/bin/ruby -wv
> def testwrite_using_array
> ask_list = [
> 'Your name',
> 'Your stuff',
> 'Your foo',
> ]

Better put questions in a global constant. Otherwise you'll always
recreate the array on each invocation of testwrite_using_array().

> array_counter = 0
> user_response = []
> ask_list.each {
> |z| puts "#{z} : "
> xx = gets.chomp
> if xx.chomp! != "qq"

There's no point in chompig the same string twice.

> array_counter += 1

You don't need array_counter because the array knows its size.

> user_response += "#{xx}".to_a

You rather want:

user_responses << xx

Cleaner, easier and faster.

> # puts array_counter # check number of elements in array
> end
>
> }
> puts user_response
> # name of next variable used user respose....we need to save this
> data to a file name like "name + age + foo".txt
> filename =user_response[1..3].to_s
> puts "filename #{filename}.txt created"
> File.open(filename, "a") { "#{user_response}" }
> end
>
> testwrite_using_array
>
>
> # So far the file is created, but nothing's in it - it's empty...I
> know I'll get there. Just need a few pointers. Any ideas chaps?

You don't write to the file - as a consequence there is nothing in there.
:-)

File.open(filename, "a") {|io| io.puts answers}

HTH

robert


Chris X

3/15/2010 7:10:00 PM

0


"The gods have made us mad" <staring@destruction.com> wrote in message
news:807ejoFmmjU1@mid.individual.net...
>
>
> "Chris X" <Chris_X2006@yahoo.co.uk> wrote in message
> news:3aWdnT4pY7Qx5APWnZ2dnUVZ7q2dnZ2d@giganews.com...
>>
>> "Maria" <fallingdown@holeinshoe.co.uk> wrote in message
>> news:8-SdncIHJOf80APWnZ2dnUVZ8lSdnZ2d@bt.com...
>>> So...we wrote them emails which they ignored, we telephoned them and
>>> they refused to answer, so we wrote to the manager of Comet to ask if it
>>> was true that he was not wanted back on site because his work is not
>>> good enough, and delivered the letter by hand. 10 minutes after
>>> delivering letter, we had phone call from the Polish woman at the
>>> agency, reinstating him with full pay for today. No explanation.
>>> I'm glad he has got his job back, but unfortunately it has just
>>> confirmed what I thought.
>>> Of course if someone else can come up with another reason why he was
>>> locked out like this without a moment's notice, I'm willing to
>>> listen...it seems that the senior management at the cleaning agency had
>>> no idea what was going on.
>>> Lesson learned - fight for your job -
>>
>> OK - a very small victory against the hordes of the Eastern job thieves
>> and their Globalist handlers - but don't rest on your laurels ... stay
>> angry. You've been given a glimpse of the state of fear which
>> International Finance wants ALL of us to PERMANENTLY live under in the
>> not too distant future. Learn from it.<<<<
>
>
>
> I can't remember the name of one of the saddest films I ever saw (someone
> here might recognise it, though) some 20 years, or more, ago.
>
> Set in the aftermath of the Wall Street crash, it examined the 'Dance
> Marathon' phenomenon, where penniless young couples would compete in
> events organised by the radio networks to see who could dance continually
> for the longest period of time.
>
> A hall packed with hundreds of couples eventually dwindled, after several
> days and nights, to just the determined handful - taking turns to sleep in
> their partners arms as they barely managed to keep shuffling around the
> dance floor.
>
> They were doing it, of course, for the prize money, such was their
> desperation to survive the economic turmoil.
>
> What was brilliantly portrayed was their fear of failing in the final
> hours - a palpable terror that after so many days of physical torture they
> might yet collapse and leave the hall as penniless as when they entered
> it.
>
> It was a vivid portrayal of unrestrained Capitalism, where men and women
> are reduced to the level of beasts and have their miseries exhibited by a
> media wiling to profit from the degradation of their fellow human beings.
>
> It certainly helped me to understand the appeal of the Nazis to the German
> people who saw the dignity of National Socialism as vastly preferable to
> being humiliated and made to perform like circus animals for the
> enrichment of their economic tormentors.
>
> Better to march in torchlight parades, than dance for fat bankers.

Might the film have been "They Shoot Horses" ? I'd not heard of such a
plotline but I Googled it and up it came;
http://www.imdb.com/title/...

You're 100% correct though - Nationalism = Pride and dignity .... Globalism
= dancing for pennies.


The gods have made us mad

3/15/2010 7:20:00 PM

0



"Chris X" <Chris_X2006@yahoo.co.uk> wrote in message
news:l6udnYN0uOmaGwPWnZ2dnUVZ8s6dnZ2d@giganews.com...
>
> "The gods have made us mad" <staring@destruction.com> wrote in message
> news:807ejoFmmjU1@mid.individual.net...
>>
>>
>> "Chris X" <Chris_X2006@yahoo.co.uk> wrote in message
>> news:3aWdnT4pY7Qx5APWnZ2dnUVZ7q2dnZ2d@giganews.com...
>>>
>>> "Maria" <fallingdown@holeinshoe.co.uk> wrote in message
>>> news:8-SdncIHJOf80APWnZ2dnUVZ8lSdnZ2d@bt.com...
>>>> So...we wrote them emails which they ignored, we telephoned them and
>>>> they refused to answer, so we wrote to the manager of Comet to ask if
>>>> it was true that he was not wanted back on site because his work is not
>>>> good enough, and delivered the letter by hand. 10 minutes after
>>>> delivering letter, we had phone call from the Polish woman at the
>>>> agency, reinstating him with full pay for today. No explanation.
>>>> I'm glad he has got his job back, but unfortunately it has just
>>>> confirmed what I thought.
>>>> Of course if someone else can come up with another reason why he was
>>>> locked out like this without a moment's notice, I'm willing to
>>>> listen...it seems that the senior management at the cleaning agency had
>>>> no idea what was going on.
>>>> Lesson learned - fight for your job -
>>>
>>> OK - a very small victory against the hordes of the Eastern job thieves
>>> and their Globalist handlers - but don't rest on your laurels ... stay
>>> angry. You've been given a glimpse of the state of fear which
>>> International Finance wants ALL of us to PERMANENTLY live under in the
>>> not too distant future. Learn from it.<<<<
>>
>>
>>
>> I can't remember the name of one of the saddest films I ever saw (someone
>> here might recognise it, though) some 20 years, or more, ago.
>>
>> Set in the aftermath of the Wall Street crash, it examined the 'Dance
>> Marathon' phenomenon, where penniless young couples would compete in
>> events organised by the radio networks to see who could dance continually
>> for the longest period of time.
>>
>> A hall packed with hundreds of couples eventually dwindled, after several
>> days and nights, to just the determined handful - taking turns to sleep
>> in their partners arms as they barely managed to keep shuffling around
>> the dance floor.
>>
>> They were doing it, of course, for the prize money, such was their
>> desperation to survive the economic turmoil.
>>
>> What was brilliantly portrayed was their fear of failing in the final
>> hours - a palpable terror that after so many days of physical torture
>> they might yet collapse and leave the hall as penniless as when they
>> entered it.
>>
>> It was a vivid portrayal of unrestrained Capitalism, where men and women
>> are reduced to the level of beasts and have their miseries exhibited by a
>> media wiling to profit from the degradation of their fellow human beings.
>>
>> It certainly helped me to understand the appeal of the Nazis to the
>> German people who saw the dignity of National Socialism as vastly
>> preferable to being humiliated and made to perform like circus animals
>> for the enrichment of their economic tormentors.
>>
>> Better to march in torchlight parades, than dance for fat bankers.
>
> Might the film have been "They Shoot Horses" ? I'd not heard of such a
> plotline but I Googled it and up it came;
> http://www.imdb.com/title/...
>
> You're 100% correct though - Nationalism = Pride and dignity ....
> Globalism = dancing for pennies.



Sounds like it - well worth watching if you get the chance.

What particularly impressed itself on me was the terrible reality of a
completely unregulated market (as advocated by Abelard and others) which
reduces ordinary people to a constant state of penury and fear.

Like I said, no *wonder* the German cheered Hitler until they were hoarse -
he didn't have to trick his way to power, the people *wanted* to be free
from their oppressors.

Mel Rowing

3/15/2010 7:22:00 PM

0

On 15 Mar, 19:03, "The gods have made us mad"
<star...@destruction.com> wrote:
> "Chris X" <Chris_X2...@yahoo.co.uk> wrote in message

> I can't remember the name of one of the saddest films I ever saw (someone
> here might recognise it, though) some 20 years, or more, ago.
>
> Set in the aftermath of the Wall Street crash, it examined the 'Dance
> Marathon' phenomenon, where penniless young couples would compete in events
> organised by the radio networks to see who could dance continually for the
> longest period of time.
>
> A hall packed with hundreds of couples eventually dwindled, after several
> days and nights, to just the determined handful - taking turns to sleep in
> their partners arms as they barely managed to keep shuffling around the
> dance floor.
>
> They were doing it, of course, for the prize money, such was their
> desperation to survive the economic turmoil.
>
> What was brilliantly portrayed was their fear of failing in the final
> hours - a palpable terror that after so many days of physical torture they
> might yet collapse and leave the hall as penniless as when they entered it.

They Shoot Horses Don't They ?

http://en.wikipedia.org/wiki/They_Shoot_Horses,_Don'...(film)




The gods have made us mad

3/15/2010 7:26:00 PM

0



"Mel Rowing" <mel.rowing@btinternet.com> wrote in message
news:d5544c24-9013-4515-97d6-7275bde4e714@b7g2000yqd.googlegroups.com...
> On 15 Mar, 19:03, "The gods have made us mad"
> <star...@destruction.com> wrote:
>> "Chris X" <Chris_X2...@yahoo.co.uk> wrote in message
>
>> I can't remember the name of one of the saddest films I ever saw (someone
>> here might recognise it, though) some 20 years, or more, ago.
>>
>> Set in the aftermath of the Wall Street crash, it examined the 'Dance
>> Marathon' phenomenon, where penniless young couples would compete in
>> events
>> organised by the radio networks to see who could dance continually for
>> the
>> longest period of time.
>>
>> A hall packed with hundreds of couples eventually dwindled, after several
>> days and nights, to just the determined handful - taking turns to sleep
>> in
>> their partners arms as they barely managed to keep shuffling around the
>> dance floor.
>>
>> They were doing it, of course, for the prize money, such was their
>> desperation to survive the economic turmoil.
>>
>> What was brilliantly portrayed was their fear of failing in the final
>> hours - a palpable terror that after so many days of physical torture
>> they
>> might yet collapse and leave the hall as penniless as when they entered
>> it.
>
> They Shoot Horses Don't They ?
>
> http://en.wikipedia.org/wiki/They_Shoot_Horses,_Don'...(film)<<<<<


Yes, thanks - Chris X also came up with that one, which I'm pretty sure is
the film I was thinking about.