[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Create array of hash values

David Lelong

11/20/2006 3:34:00 PM

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

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

10 Answers

Peter Szinek

11/20/2006 3:39:00 PM

0

David Lelong 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.rubyra...



Ken Bloom

11/20/2006 3:40:00 PM

0

On Tue, 21 Nov 2006 00:33:33 +0900, David Lelong 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?
>
> Thanks,
>
> David
>

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

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


--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Ken Bloom

11/20/2006 3:42:00 PM

0

On Tue, 21 Nov 2006 00:33:33 +0900, David Lelong 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?
>
> 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

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Peter Szinek

11/20/2006 3:43:00 PM

0

Peter Szinek wrote:
> David Lelong 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}

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


James Gray

11/20/2006 3:44:00 PM

0

On Nov 20, 2006, at 9:39 AM, Peter Szinek 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 Gray II

Robert Klemme

11/20/2006 3:44:00 PM

0

On 20.11.2006 16:33, David Lelong 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

Ross Bamford

11/20/2006 3:47:00 PM

0

On Mon, 20 Nov 2006 15:33:33 -0000, David Lelong <drlelon@yahoo.com> wro=
te:

> Hi,
>
> I have a hash that contains the following keys/values:
>
> [#<Contact_List:0x2607150 @attributes=3D{"contact_id"=3D>"4"}>,
> #<Contact_List:0x2607114 @attributes=3D{"contact_id"=3D>"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).

-- =

Ross Bamford - rosco@roscopeco.remove.co.uk

Rob Biedenharn

11/20/2006 4:10:00 PM

0

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

> On Mon, 20 Nov 2006 15:33:33 -0000, David Lelong
> <drlelon@yahoo.com> 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?
>>
>> 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).
>
> --
> Ross Bamford - rosco@roscopeco.remove.co.uk

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://agileconsult...
Rob@AgileConsultingLLC.com



J. B. Rainsberger

11/21/2006 6:10:00 AM

0

David Lelong 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.
--
J. B. (Joe) Rainsberger :: http://www....
Your guide to software craftsmanship
JUnit Recipes: Practical Methods for Programmer Testing
2005 Gordon Pask Award for contribution Agile Software Practice

atoz.cd

11/21/2006 6:40:00 AM

0

Personally I'd do:

c_ids=[]

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

David Lelong 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?
>
> Thanks,
>
> David
>
> --
> Posted via http://www.ruby-....