Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hash#each with nested array as value

Reply
Thread Tools

Hash#each with nested array as value

 
 
Earle Clubb
Guest
Posts: n/a
 
      10-17-2007
I have a hash where the values are nested arrays. I'd like to be able to
iterate over the hash and have 3 vars available in the block: key,
value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
work. Is there a better way to do it than the last line below? Thanks.

Earle

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

irb(main):001:0> require 'pp'
=> true
irb(main):002:0> h = {0 => [[1], [2]]}
=> {0=>[[1], [2]]}
irb(main):003:0> h.each {|a, b| pp a, b}
0
[[1], [2]]
=> {0=>[[1], [2]]}
irb(main):004:0> h.each {|a, b, c| pp a, b, c}
0
[[1], [2]]
nil
=> {0=>[[1], [2]]}
irb(main):005:0> h.each {|a, b| pp a, b[0], b[1]}
0
[1]
[2]
=> {0=>[[1], [2]]}

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

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 9:45 am, Earle Clubb <(E-Mail Removed)> wrote:
> I have a hash where the values are nested arrays. I'd like to be able to
> iterate over the hash and have 3 vars available in the block: key,
> value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
> work.


Not much better, but:

irb(main):006:0> h = { :a => [:foo, :bar] }
=> {:a=>[:foo, :bar]}

irb(main):007:0> h.each{ |key,pair| a,b = *pair; p key, a, b }
:a
:foo
:bar

 
Reply With Quote
 
 
 
 
Gordon Thiesfeld
Guest
Posts: n/a
 
      10-17-2007
On 10/17/07, Earle Clubb <(E-Mail Removed)> wrote:
> I have a hash where the values are nested arrays. I'd like to be able to
> iterate over the hash and have 3 vars available in the block: key,
> value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
> work. Is there a better way to do it than the last line below? Thanks.
>
> Earle


Use parentheses.

>> h = [0 => [[1],[2]]}
>> h.each{|k,(a,b)| p k, a, b}

0
[1]
[2]
=> {0=>[[1], [2]]}

Regards,

Gordon
>>


 
Reply With Quote
 
Earle Clubb
Guest
Posts: n/a
 
      10-17-2007
Gordon Thiesfeld wrote:
> On 10/17/07, Earle Clubb <(E-Mail Removed)> wrote:
>> I have a hash where the values are nested arrays. I'd like to be able to
>> iterate over the hash and have 3 vars available in the block: key,
>> value[0], and value[1]. As you can see, h.each {|a,b,c|...} doesn't
>> work. Is there a better way to do it than the last line below? Thanks.
>>
>> Earle

>
> Use parentheses.
>
>>> h = [0 => [[1],[2]]}
>>> h.each{|k,(a,b)| p k, a, b}

> 0
> [1]
> [2]
> => {0=>[[1], [2]]}
>
> Regards,
>
> Gordon


Perfect. Thanks, Gordon.

Earle
--
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
want to search value in nested arrays and return other value Joao Silva Ruby 4 12-10-2008 10:41 AM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments