Filling a hash from an enumerable

P

Pete Hodgson

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
 
T

Trans

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.
 
P

Peña, Botp

From: Pe=F1a, Botp [mailto:[email protected]]=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">]}
 
P

Pete Hodgson

From: Peña, Botp [mailto:[email protected]]
# >> 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.
 
W

William James

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">}
 
R

Robert Klemme

2008/10/9 William James said:
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
 
P

Peña, Botp

From: Pete Hodgson [mailto:p[email protected]]=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
 
T

Trans

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top