Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adding keys to a hash in a loop

Reply
Thread Tools

Adding keys to a hash in a loop

 
 
Jack Bauer
Guest
Posts: n/a
 
      02-01-2010
Hi all,

I'm trying to essentially accomplish this behavior:

output.each do |item|
output_hash << {item.name => []}
end

So that I can end up with:

{"first_set" => [], "second_set" => [], "third_set" => [], ... }

I know the first block of code won't work, but it's just to illustrate
what it is I wish to accomplish. Can anyone help me out? Thanks.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-01-2010
On 01.02.2010 18:35, Jack Bauer wrote:

> I'm trying to essentially accomplish this behavior:
>
> output.each do |item|
> output_hash<< {item.name => []}
> end
>
> So that I can end up with:
>
> {"first_set" => [], "second_set" => [], "third_set" => [], ... }
>
> I know the first block of code won't work, but it's just to illustrate
> what it is I wish to accomplish. Can anyone help me out? Thanks.


Erm, how would you put a key value pair into a Hash without a loop?

http://www.ruby-doc.org/core/classes/Hash.html

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Jack Bauer
Guest
Posts: n/a
 
      02-01-2010
Robert Klemme wrote:

> Erm, how would you put a key value pair into a Hash without a loop?


This is the loop...

output.each do |item|
output_hash << {item.name => []}
end

Iterate through items in the "output" array and create a key for the
"output_hash" hash using the value of "item.name" as the key, and an
empty array as the value for that key.

I know the output_hash << won't work, that's why I'm asking how I can go
about it. I was just using that as a sort of pseudo-code to get my point
across.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      02-01-2010
Jack Bauer wrote:
> Robert Klemme wrote:
>
>> Erm, how would you put a key value pair into a Hash without a loop?

>
> This is the loop...
>
> output.each do |item|
> output_hash << {item.name => []}
> end
>
> Iterate through items in the "output" array and create a key for the
> "output_hash" hash using the value of "item.name" as the key, and an
> empty array as the value for that key.
>
> I know the output_hash << won't work, that's why I'm asking how I can go
> about it. I was just using that as a sort of pseudo-code to get my point
> across.


The same way you assign to a hash element outside a loop:
output_hash[item.name] = value . << is only useful with arrays, not
hashes.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
http://www.velocityreviews.com/forums/(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jack Bauer
Guest
Posts: n/a
 
      02-01-2010
Marnen Laibow-Koser wrote:
> The same way you assign to a hash element outside a loop:
> output_hash[item.name] = value . << is only useful with arrays, not
> hashes.



Thanks, I know << is only for arrays. Using = will only end up with one
key and not all 10 that I'm trying to get.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      02-01-2010
Jack Bauer wrote:
> Marnen Laibow-Koser wrote:
>> The same way you assign to a hash element outside a loop:
>> output_hash[item.name] = value . << is only useful with arrays, not
>> hashes.

>
>
> Thanks, I know << is only for arrays. Using = will only end up with one
> key and not all 10 that I'm trying to get.


Wrong. Do it inside the loop, for each key in turn. Try it!

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Matthew Pounsett
Guest
Posts: n/a
 
      02-01-2010

On 2010/02/01, at 13:07, Jack Bauer wrote:

> Marnen Laibow-Koser wrote:
>> The same way you assign to a hash element outside a loop:
>> output_hash[item.name] = value . << is only useful with arrays, not
>> hashes.

>
>
> Thanks, I know << is only for arrays. Using = will only end up with one
> key and not all 10 that I'm trying to get.


Hi Jack.

The suggestion is not to just replace << with = like this:

output.each do |item|
output_hash = {item.name => []}
end

The suggestion is to use = in a normal hash assignment:

output.each do |item|
output_hash[item.name] = []
end

HTH,
Matt



 
Reply With Quote
 
Jack Bauer
Guest
Posts: n/a
 
      02-01-2010
Matthew Pounsett wrote:
> The suggestion is to use = in a normal hash assignment:



There we go! Thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      02-01-2010
Jack Bauer wrote:
> Hi all,
>
> I'm trying to essentially accomplish this behavior:
>
> output.each do |item|
> output_hash << {item.name => []}
> end


Apart from the other suggestions, you can do:

output.each do |item|
output_hash.merge!(item.name => [])
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      02-01-2010
Brian Candler wrote:
> Jack Bauer wrote:
>> Hi all,
>>
>> I'm trying to essentially accomplish this behavior:
>>
>> output.each do |item|
>> output_hash << {item.name => []}
>> end

>
> Apart from the other suggestions, you can do:
>
> output.each do |item|
> output_hash.merge!(item.name => [])
> end


Yes. Although merging a single-key hash seems silly, and creates extra
objects...

Best,
--*
Marnen Laibow-Koser
http://www.marnen.org
(E-Mail Removed)

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
hash.keys and hash.values Mage Ruby 14 08-15-2006 08:44 PM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
No Keys, nor other hash methods on multidimensional hash Xeno Campanoli Ruby 16 08-25-2005 07:04 AM



Advertisments