Create array of hash values

D

David Lelong

Hi,

I have a hash that contains the following keys/values:

[#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

I want to create an array containing the contact_id values. What's an
easy way to do this?

Thanks,

David
 
P

Peter Szinek

David said:
Hi,

I have a hash that contains the following keys/values:

[#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

I want to create an array containing the contact_id values. What's an
easy way to do this?

Try this:

result = x.inject([]) {|a,h| a << h.values[0]; a}

(x is the array of hashes)

Cheers,
Peter

__
http://www.rubyrailways.com
 
P

Peter Szinek

Peter said:
David said:
Hi,

I have a hash that contains the following keys/values:

[#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

I want to create an array containing the contact_id values. What's an
easy way to do this?

Try this:

result = x.inject([]) {|a,h| a << h.values[0]; a}

Sorry I have overlooked a little detail... My solution would work if the
original array would contain

[{"contact_id"=>"4"}, {"contact_id"=>"8"}]

For the original question, the answer should be probably

result = x.inject([]) {|a,h| a << h.attributes.values[0]; a}

HTH,
Peter

__
http://www.rubyrailways.com
 
J

James Edward Gray II

result = x.inject([]) {|a,h| a << h.values[0]; a}

Any time you see inject() used as it is above, the intention was
really map():

result = x.map { |h| h.values[0] }

Same thing.

James Edward Gray II
 
R

Robert Klemme

Hi,

I have a hash that contains the following keys/values:

[#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

Actually it seems you rather have an Array of Contatc_List instances.
I want to create an array containing the contact_id values. What's an
easy way to do this?

your_array.map {|cl| cl.attributes["contact_id"]}

Regards

robert
 
R

Rob Biedenharn

Hi,

I have a hash that contains the following keys/values:

[#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

I want to create an array containing the contact_id values.
What's an
easy way to do this?

Thanks,

David

Assuming Contact_List (aside: why an underscored class name?) has
an attr_accessor for 'attributes', you might try:

a.map { |clist| clist.attributes['contact_id'] }

(where 'a' is the array you show above).

Well, the OP asked for easy and what's easier than Symbol#to_proc
(or am I reading Rails and ActiveRecord into this question where it's
missing?)

a = [#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

a.map(&:contact_id)
=> [4, 8]

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

J. B. Rainsberger

David said:
Hi,

I have a hash that contains the following keys/values:

[#<Contact_List:0x2607150 @attributes={"contact_id"=>"4"}>,
#<Contact_List:0x2607114 @attributes={"contact_id"=>"8"}>]

I want to create an array containing the contact_id values. What's an
easy way to do this?

contact_lists.map { |each| each.attributes["contact_id"] }

Is each of those objects really a ContactList if each only has a single
contact ID?

Take care.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top