Create array of hash values

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

David L. wrote:

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

On Tue, 21 Nov 2006 00:33:33 +0900, David L. wrote:

Thanks,

David

irb(main):001:0> {1=>2,3=>4}.values
=> [2, 4]

irb(main):002:0> {1=>2,3=>4}.keys
=> [1, 3]

Peter S. wrote:

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

On Nov 20, 2006, at 9:39 AM, Peter S. wrote:

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 G. II

On Tue, 21 Nov 2006 00:33:33 +0900, David L. wrote:

Thanks,

David

Sorry. I should look at the code you have before I respond.

You have an array full of objects. I’m not sure how to get a contact_id
out of a Contact_List, but let’s assume you’ve defined a method that
gets
you the contact id, called Contact_List#id

Then, you would get the contact id by

[#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>].collect do
|x|
x.id
end

On Mon, 20 Nov 2006 15:33:33 -0000, David L. [email protected]
wrote:

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

On 20.11.2006 16:33, David L. wrote:

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

On Nov 20, 2006, at 10:50 AM, Ross B. wrote:

I want to create an array containing the contact_id values.

a.map { |clist| clist.attributes[‘contact_id’] }

(where ‘a’ is the array you show above).


Ross B. - [email protected]

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 B. http://agileconsultingllc.com
[email protected]

David L. wrote:

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.

Personally I’d do:

c_ids=[]

for l in @attributes.values
c_ids << l
end

c_ids = my_hash.values.collect{|value| value.attributes[‘contact_id’]}

2006/11/23, [email protected] [email protected]: