[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Filling a hash from an enumerable

Pete Hodgson

10/9/2008 1:00:00 AM

Hi folks,

Given:

Person = Struct.new(:name,:age,:city)
people_array = [
Person.new('bob',12,'SFO'),
Person.new('dave',14,'NYC'),
Person.new('jane',6,'LDN') ]

people_map = {}
people_array.each{ |p| people_map[p.name] = p }


Is there a cleaner way to build people_map? I'm thinking there might be
something like:

people_map = people_array.to_map{ |x| x.name }

in the standard library somewhere that I don't know of.

Thanks for any pointers,
Pete

8 Answers

Peña, Botp

10/9/2008 1:38:00 AM

0

From: Pete Hodgson [mailto:phodgson@lyris.com]=20
# people_map =3D people_array.to_map{ |x| x.name }

you should try it,

>> people_array.map{ |p| p.name }
=3D> ["bob", "dave", "jane"]

Trans

10/9/2008 2:54:00 AM

0



On Oct 8, 9:00=A0pm, Pete Hodgson <phodg...@lyris.com> wrote:
> Hi folks,
>
> Given:
>
> Person =3D Struct.new(:name,:age,:city)
> people_array =3D [
> =A0 =A0 Person.new('bob',12,'SFO'),
> =A0 =A0 Person.new('dave',14,'NYC'),
> =A0 =A0 Person.new('jane',6,'LDN') ]
>
> people_map =3D {}
> people_array.each{ |p| people_map[p.name] =3D p }
>
> Is there a cleaner way to build people_map? I'm thinking there might be
> something like:
>
> people_map =3D people_array.to_map{ |x| x.name }
>
> in the standard library somewhere that I don't know of.

Facets has #graph / #mash.

require 'facets'

people_array.graph{ |p| [p.name, p] }

# or

people_array.mash{ |p| [p.name, p] }

T.

Peña, Botp

10/9/2008 3:06:00 AM

0

From: Pe=F1a, Botp [mailto:botp@delmonte-phil.com]=20
# >> people_array.map{ |p| p.name }
# =3D> ["bob", "dave", "jane"]

pls ignore above, i'm still searching my lost brain :)

try #group_by,

> people_array.group_by{ |p| p.name }
=3D> {"bob"=3D>[#<struct Person name=3D"bob", age=3D12, city=3D"SFO">], =
"dave"=3D>[#<struct Person name=3D"dave", age=3D14, city=3D"NYC">], =
"jane"=3D>[#<struct Person name=3D"jane", age=3D6, city=3D"LDN">]}

Pete Hodgson

10/9/2008 7:10:00 AM

0


Peña, Botp wrote:
> From: Peña, Botp [mailto:botp@delmonte-phil.com]
> # >> people_array.map{ |p| p.name }
> # => ["bob", "dave", "jane"]
>
> pls ignore above, i'm still searching my lost brain :)
>
> try #group_by,
>
>
>> people_array.group_by{ |p| p.name }
>>
> => {"bob"=>[#<struct Person name="bob", age=12, city="SFO">], "dave"=>[#<struct Person name="dave", age=14, city="NYC">], "jane"=>[#<struct Person name="jane", age=6, city="LDN">]}
>
Thanks, but that's not quite what I wanted. It creates a hash whose
values are lonely arrays, rather than a map whose values are the structs
themselves.

I guess I'll just have to monkey-patch Enumerable with a to_map method.
Should probably figure out a better name first tho.

William James

10/9/2008 7:24:00 AM

0

On Oct 8, 8:00 pm, Pete Hodgson <phodg...@lyris.com> wrote:
> Hi folks,
>
> Given:
>
> Person = Struct.new(:name,:age,:city)
> people_array = [
>     Person.new('bob',12,'SFO'),
>     Person.new('dave',14,'NYC'),
>     Person.new('jane',6,'LDN') ]
>
> people_map = {}
> people_array.each{ |p| people_map[p.name] = p }
>
> Is there a cleaner way to build people_map? I'm thinking there might be
> something like:
>
> people_map = people_array.to_map{ |x| x.name }
>
> in the standard library somewhere that I don't know of.
>
> Thanks for any pointers,
> Pete

Hash[ * people_array.map{|x| [ x.name, x ] }.flatten ]
==>{"dave" => #<struct Person name="dave", age=14, city="NYC">,
"jane" => #<struct Person name="jane", age=6, city="LDN">,
"bob" => #<struct Person name="bob", age=12, city="SFO">}

Robert Klemme

10/9/2008 7:31:00 AM

0

2008/10/9 William James <w_a_x_man@yahoo.com>:
> On Oct 8, 8:00 pm, Pete Hodgson <phodg...@lyris.com> wrote:
>> Given:
>>
>> Person = Struct.new(:name,:age,:city)
>> people_array = [
>> Person.new('bob',12,'SFO'),
>> Person.new('dave',14,'NYC'),
>> Person.new('jane',6,'LDN') ]
>>
>> Is there a cleaner way to build people_map? I'm thinking there might be
>> something like:

> Hash[ * people_array.map{|x| [ x.name, x ] }.flatten ]
> ==>{"dave" => #<struct Person name="dave", age=14, city="NYC">,
> "jane" => #<struct Person name="jane", age=6, city="LDN">,
> "bob" => #<struct Person name="bob", age=12, city="SFO">}

And since we did not have an inject version so far:

irb(main):001:0> require 'pp'
=> true
irb(main):002:0> Person = Struct.new(:name,:age,:city)
=> Person
irb(main):003:0> people_array = [
irb(main):004:1* Person.new('bob',12,'SFO'),
irb(main):005:1* Person.new('dave',14,'NYC'),
irb(main):006:1* Person.new('jane',6,'LDN'),
irb(main):007:1* ]
=> [#<struct Person name="bob", age=12, city="SFO">, #<struct Person
name="dave", age=14, city="NYC">, #<struct Person name="jane", age=6,
city="LDN">]
irb(main):008:0> pp( people_map = people_array.inject({}) do |ha,pe|
irb(main):009:2* ha[pe.name] = pe
irb(main):010:2> ha
irb(main):011:2> end )
{"dave"=>#<struct Person name="dave", age=14, city="NYC">,
"jane"=>#<struct Person name="jane", age=6, city="LDN">,
"bob"=>#<struct Person name="bob", age=12, city="SFO">}
=> nil

Just for completeness reasons of course. ;-)

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end

Peña, Botp

10/9/2008 9:35:00 AM

0

From: Pete Hodgson [mailto:phodgson@lyris.com]=20
# Pe=F1a, Botp wrote:
# >> people_array.group_by{ |p| p.name }
# > =3D> {"bob"=3D>[#<struct Person name=3D"bob", age=3D12,=20
# city=3D"SFO">], "dave"=3D>[#<struct Person name=3D"dave", age=3D14,=20
# city=3D"NYC">], "jane"=3D>[#<struct Person name=3D"jane", age=3D6,=20
# city=3D"LDN">]}
# > =20
# Thanks, but that's not quite what I wanted. It creates a hash whose=20
# values are lonely arrays,...

lonely indeed, but consider the case when you have dup names

kind regards -botp


Trans

10/9/2008 9:46:00 AM

0



On Oct 9, 3:10=A0am, Pete Hodgson <phodg...@lyris.com> wrote:

> I guess I'll just have to monkey-patch Enumerable with a to_map method.
> Should probably figure out a better name first tho.

You mean like "map hash", aka "mash".

T.