Create array of hash values

Discussion in 'Ruby' started by David Lelong, Nov 20, 2006.

  1. David Lelong

    David Lelong Guest

    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, Nov 20, 2006
    #1
    1. Advertising

  2. David Lelong

    Peter Szinek Guest

    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
    Peter Szinek, Nov 20, 2006
    #2
    1. Advertising

  3. David Lelong

    Peter Szinek Guest

    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
    Peter Szinek, Nov 20, 2006
    #3
  4. 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
    James Edward Gray II, Nov 20, 2006
    #4
  5. 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
    Robert Klemme, Nov 20, 2006
    #5
  6. On Nov 20, 2006, at 10:50 AM, Ross Bamford wrote:

    > On Mon, 20 Nov 2006 15:33:33 -0000, 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
    >>

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


    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 Biedenharn, Nov 20, 2006
    #6
  7. 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
    J. B. Rainsberger, Nov 21, 2006
    #7
  8. David Lelong

    Guest

    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/.
    , Nov 21, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  2. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    132
    George Ogata
    Apr 15, 2006
  3. Anthony Martinez
    Replies:
    4
    Views:
    260
    Robert Klemme
    Jun 11, 2007
  4. Michal Suchanek
    Replies:
    6
    Views:
    217
    Nobuyoshi Nakada
    Jun 13, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    598
    David A. Black
    Jul 2, 2008
Loading...

Share This Page