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-forum.com/ .
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.rubyrailways.com
Ken_Bloom
(Ken Bloom)
20 November 2006 15:40
3
irb(main):001:0> {1=>2,3=>4}.values
=> [2, 4]
irb(main):002:0> {1=>2,3=>4}.keys
=> [1, 3]
···
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
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
Ken_Bloom
(Ken Bloom)
20 November 2006 15:45
4
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 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
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
Robert_K1
(Robert K.)
20 November 2006 15:45
5
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 20.11.2006 16:33, David Lelong wrote:
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 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
--
Ross Bamford - rosco@roscopeco.remove.co.uk
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.jbrains.ca
Your guide to software craftsmanship
JUnit Recipes: Practical Methods for Programmer Testing
2005 Gordon Pask Award for contribution Agile Software Practice
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-forum.com/\ .
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.rubyrailways.com
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
···
On Nov 20, 2006, at 9:39 AM, Peter Szinek wrote:
result = x.inject( ) {|a,h| a << h.values[0]; a}
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
Rob@AgileConsultingLLC.com
···
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
c_ids = my_hash.values.collect{|value| value.attributes['contact_id']}
···
2006/11/23, atoz.cd@googlemail.com <atoz.cd@googlemail.com>:
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-forum.com/\ .
--
Michel Casabianca