Convert a Hash into an Array

Discussion in 'Ruby' started by Eustaquio Rangel de Oliveira Jr., Jan 24, 2005.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi!

    If I have:

    h = {"a"=>"111","b"=>"222"}
    h.to_a
    => [["a", "111"], ["b", "222"]]

    is there a method to convert a Hash into a "plain" Array like

    ["a","111","b","222"]

    Thanks!

    - ----------------------------
    Eustáquio "TaQ" Rangel

    http://beam.to/taq
    Usuário GNU/Linux no. 224050
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFB9WJib6UiZnhJiLsRAlZJAKC3n+pmVAsm3qLxWG5li9BDOh2IGQCff8m3
    LsYXA0CuhQt/zejSKvSJbmM=
    =b4wV
    -----END PGP SIGNATURE-----
     
    Eustaquio Rangel de Oliveira Jr., Jan 24, 2005
    #1
    1. Advertising

  2. Eustaquio Rangel de Oliveira Jr. wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Hi!
    >
    > If I have:
    >
    > h = {"a"=>"111","b"=>"222"}
    > h.to_a
    > => [["a", "111"], ["b", "222"]]
    >
    > is there a method to convert a Hash into a "plain" Array like


    h.to_a.flatten will do the trick.

    Regards,

    Michael
     
    Michael Neumann, Jan 24, 2005
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Michael Neumann wrote:

    Hi Michael!

    | h.to_a.flatten will do the trick.

    THANK you very much! :)

    - ----------------------------
    Eustáquio "TaQ" Rangel

    http://beam.to/taq
    Usuário GNU/Linux no. 224050
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFB9WU/b6UiZnhJiLsRAqZ2AKCVcApOvbi8qTBh3+H9L8PcmJ8duQCgoSsa
    ogE6MAn8QflWy8ZJnqufNkg=
    =iJ3K
    -----END PGP SIGNATURE-----
     
    Eustaquio Rangel de Oliveira Jr., Jan 24, 2005
    #3
  4. Hi --

    On Tue, 25 Jan 2005, Michael Neumann wrote:

    > Eustaquio Rangel de Oliveira Jr. wrote:
    >> -----BEGIN PGP SIGNED MESSAGE-----
    >> Hash: SHA1
    >>
    >> Hi!
    >>
    >> If I have:
    >>
    >> h = {"a"=>"111","b"=>"222"}
    >> h.to_a
    >> => [["a", "111"], ["b", "222"]]
    >>
    >> is there a method to convert a Hash into a "plain" Array like

    >
    > h.to_a.flatten will do the trick.


    However, if any of the hash values are arrays, they'll get flattened
    too:

    { "a" => [1,2,3] }.to_a.flatten # => ["a",1,2,3]

    unless of course you do:

    require 'flattenx' # from RAA
    { "a" => [1,2,3] }.to_a.flatten_by(1) # => ["a",[1,2,3]]

    :)


    David

    --
    David A. Black
     
    David A. Black, Jan 25, 2005
    #4
  5. "David A. Black" <> schrieb im Newsbeitrag
    news:pine.LNX.4.61.0501241746590.31348@wobblini...
    > Hi --
    >
    > On Tue, 25 Jan 2005, Michael Neumann wrote:
    >
    > > Eustaquio Rangel de Oliveira Jr. wrote:
    > >> -----BEGIN PGP SIGNED MESSAGE-----
    > >> Hash: SHA1
    > >>
    > >> Hi!
    > >>
    > >> If I have:
    > >>
    > >> h = {"a"=>"111","b"=>"222"}
    > >> h.to_a
    > >> => [["a", "111"], ["b", "222"]]
    > >>
    > >> is there a method to convert a Hash into a "plain" Array like

    > >
    > > h.to_a.flatten will do the trick.

    >
    > However, if any of the hash values are arrays, they'll get flattened
    > too:
    >
    > { "a" => [1,2,3] }.to_a.flatten # => ["a",1,2,3]
    >
    > unless of course you do:
    >
    > require 'flattenx' # from RAA
    > { "a" => [1,2,3] }.to_a.flatten_by(1) # => ["a",[1,2,3]]
    >
    > :)


    .... or use #inject which also has the advantage of saving the intermediate
    array which is created by Hash#to_a:

    >> h = {"a"=>"111","b"=>"222"}

    => {"a"=>"111", "b"=>"222"}
    >> h.inject([]){|a,(k,v)| a << k << v}

    => ["a", "111", "b", "222"]

    Btw, did I mention that I love #inject? :)

    Kind regards

    robert
     
    Robert Klemme, Jan 25, 2005
    #5
  6. On Tue, 2005-01-25 at 17:10 +0900, Robert Klemme wrote:

    > >> h = {"a"=>"111","b"=>"222"}

    > => {"a"=>"111", "b"=>"222"}
    > >> h.inject([]){|a,(k,v)| a << k << v}

    > => ["a", "111", "b", "222"]
    >


    Cool! I was wondering how to convert a hash to an array, dropping the
    keys in the process. I see that inject does the job nicely. Is there
    another way too?

    >> h.inject([]){|a, (k, v)| a << v}

    => ["111", "222"]

    Cheers,

    Richard.
     
    Richard Turner, Jan 25, 2005
    #6
  7. Eustaquio Rangel de Oliveira Jr.

    Pit Capitain Guest

    Richard Turner schrieb:
    > Cool! I was wondering how to convert a hash to an array, dropping the
    > keys in the process. I see that inject does the job nicely. Is there
    > another way too?
    >
    >
    >>>h.inject([]){|a, (k, v)| a << v}

    >
    > => ["111", "222"]


    h = {"a"=>"111", "b"=>"222"}
    p h.values # => ["111", "222"]

    Regards,
    Pit
     
    Pit Capitain, Jan 25, 2005
    #7
  8. On Tue, 2005-01-25 at 18:31 +0900, Pit Capitain wrote:
    > h = {"a"=>"111", "b"=>"222"}
    > p h.values # => ["111", "222"]
    >


    Even better! I thought there must be a method for that but my (very
    quick) search last night didn't find it. Thanks

    Richard.
     
    Richard Turner, Jan 25, 2005
    #8
  9. georgesawyer wrote:
    >>did I mention that I love #inject? :)

    > (Wistfully) Even though 'inject' is the name in Smalltalk, I feel it were
    > better named, 'consolidate'.


    I don't have anything against #inject, but I could see the case for an
    alias #accumulate, much like SICP's accumulate which afaik is the same
    thing.

    (I wouldn't mind #reduce as well, though that would be slightly incorrect)
     
    linus sellberg, Jan 28, 2005
    #9
  10. linus sellberg ha scritto:
    > georgesawyer wrote:
    >
    >>> did I mention that I love #inject? :)

    >>
    >> (Wistfully) Even though 'inject' is the name in Smalltalk, I feel it were
    >> better named, 'consolidate'.

    >
    >
    > I don't have anything against #inject, but I could see the case for an
    > alias #accumulate, much like SICP's accumulate which afaik is the same
    > thing.
    >
    > (I wouldn't mind #reduce as well, though that would be slightly incorrect)


    #fold would be better imho (even if slightly incorrect)
     
    gabriele renzi, Jan 28, 2005
    #10
  11. On Jan 28, 2005, at 7:00 PM, georgesawyer wrote:
    > I lack some comfort with 'inject', even after time: perhaps because it
    > asks us to pass the code for collapsing (an array of) objects, instead
    > of
    > asking the objects to know how to collapse themselves: a known
    > object-programming principle. Collapse perhaps should be a module
    > method
    > of no arguments, mix-inable to any class of objects having the '[]'
    > method.


    the main problem i've had with inject is the naming.
    to me it says 'destructive'. and thusly i'd expect
    the receiver and the argument to be exactly the other
    way around. however swapping them makes no sense as
    it forces the use of another temporary. thusly a
    change in name should be made :)

    haven't heard any better alternative yet however :/

    Alex
     
    Alexander Kellett, Jan 28, 2005
    #11
  12. "Alexander Kellett" <> schrieb im Newsbeitrag
    news:...
    > On Jan 28, 2005, at 7:00 PM, georgesawyer wrote:
    >> I lack some comfort with 'inject', even after time: perhaps because it
    >> asks us to pass the code for collapsing (an array of) objects, instead of
    >> asking the objects to know how to collapse themselves: a known
    >> object-programming principle. Collapse perhaps should be a module method
    >> of no arguments, mix-inable to any class of objects having the '[]'
    >> method.

    >
    > the main problem i've had with inject is the naming.
    > to me it says 'destructive'. and thusly i'd expect
    > the receiver and the argument to be exactly the other
    > way around. however swapping them makes no sense as
    > it forces the use of another temporary. thusly a
    > change in name should be made :)
    >
    > haven't heard any better alternative yet however :/


    #accum is the only name that fits slightly better than #inject although I'm
    sure one can come up with misinterpretations of that name also. Anyway:
    #inject is quite complex so it takes some time to get used to the behavior
    regardless of how it's named.

    <rant>
    But: the search for a better name is wasted time IMHO. Things are like they
    are and changes to base classes generally do far more harm than good (if
    something is seriously flawed that's another story of course).
    </rant>

    Kind regards

    robert
     
    Robert Klemme, Jan 30, 2005
    #12
  13. Eustaquio Rangel de Oliveira Jr.

    Phrogz Guest

    Robert Klemme wrote:
    > "Alexander Kellett" <> schrieb im Newsbeitrag
    > news:...
    > > the main problem i've had with inject is the naming.
    > > to me it says 'destructive'. [...]

    > #accum is the only name that fits slightly better than #inject

    although I'm
    > sure one can come up with misinterpretations of that name also.

    Anyway:
    > #inject is quite complex so it takes some time to get used to the

    behavior
    > regardless of how it's named.
    >
    > <rant>
    > But: the search for a better name is wasted time IMHO. Things are

    like they
    > are and changes to base classes generally do far more harm than good

    (if
    > something is seriously flawed that's another story of course).
    > </rant>


    I respectfully disagree. I was SURE from the name that it must be some
    function to modify the original array, inserting new entries. It took
    me a long time to understand what it was for.

    Obviously we can't ditch the old name (for backwards-compatibility
    reasons) but a better alias that is promoted to new users would be a
    good idea, IMHO.

    How about #each_with_state for a name? #accumulate (or #accum) is not a
    bad name, but also implies to me that the result will be the
    sum/product/concatenation of values. How would #accum fit for the ri
    example of using #inject to find the longest item in a list, for
    example? Some form of accumulation is one of the more common uses of
    #inject (I gather) but by no means the only use.


    Aside - whatever the name, what an excellent, versatile function! For
    example, the linked-to geometric series sum at
    http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt can be
    written in a more terse and (IMO) more ruby-esque way as simply:

    def geometric_series_sum( x, n )
    (0..n).inject(0){ |sum,i| sum+x**i }
    end

    --
    (-, /\ \/ / /\/
     
    Phrogz, Mar 18, 2005
    #13
  14. Eustaquio Rangel de Oliveira Jr.

    ES Guest

    Derek Wyatt wrote:
    > | I respectfully disagree. I was SURE from the name that it must be some
    > | function to modify the original array, inserting new entries. It took
    > | me a long time to understand what it was for.
    > |
    > | Obviously we can't ditch the old name (for backwards-compatibility
    > | reasons) but a better alias that is promoted to new users would be a
    > | good idea, IMHO.
    > |
    > | How about #each_with_state for a name? #accumulate (or #accum) is not a
    > | bad name, but also implies to me that the result will be the
    > | sum/product/concatenation of values. How would #accum fit for the ri
    > | example of using #inject to find the longest item in a list, for
    > | example? Some form of accumulation is one of the more common uses of
    > | #inject (I gather) but by no means the only use.
    >
    > Is not #reduce the name we're looking for? Or is it not faithful enough
    > to lisp to rip off the name?


    I'm convinced there were two main creators for Lisp. One designed the
    language, and the other one (who, incidentally, was an insane monkey),
    came up with the function names.

    There's really no perfect name for this method...I'm partial to #fold
    after Haskell & Co. but #accum (without the -ate) may describe it's
    function the best. Then again, this is just wishful thinking.

    > Regs,
    > D


    E
     
    ES, Mar 18, 2005
    #14
  15. "ES" <> schrieb im Newsbeitrag
    news:...
    > Derek Wyatt wrote:
    > > | I respectfully disagree. I was SURE from the name that it must be

    some
    > > | function to modify the original array, inserting new entries. It

    took
    > > | me a long time to understand what it was for.
    > > |
    > > | Obviously we can't ditch the old name (for backwards-compatibility
    > > | reasons) but a better alias that is promoted to new users would be a
    > > | good idea, IMHO.
    > > |
    > > | How about #each_with_state for a name? #accumulate (or #accum) is

    not a
    > > | bad name, but also implies to me that the result will be the
    > > | sum/product/concatenation of values. How would #accum fit for the ri
    > > | example of using #inject to find the longest item in a list, for
    > > | example? Some form of accumulation is one of the more common uses of
    > > | #inject (I gather) but by no means the only use.
    > >
    > > Is not #reduce the name we're looking for? Or is it not faithful

    enough
    > > to lisp to rip off the name?

    >
    > I'm convinced there were two main creators for Lisp. One designed the
    > language, and the other one (who, incidentally, was an insane monkey),
    > came up with the function names.
    >
    > There's really no perfect name for this method...I'm partial to #fold
    > after Haskell & Co. but #accum (without the -ate) may describe it's
    > function the best. Then again, this is just wishful thinking.


    #aggregate might be a good choice, too - reminds me of SQL aggregation
    functions like SUM, AVG, MAX etc.

    Kind regards

    robert
     
    Robert Klemme, Mar 18, 2005
    #15
  16. On Fri, 18 Mar 2005 17:59:53 +0900, Robert Klemme <> wrote:

    > #aggregate might be a good choice, too - reminds me of SQL aggregation
    > functions like SUM, AVG, MAX etc.


    Perfect! I'm surprised no-one thought of this before!

    +1 from me

    --
    Gavri
    http://gavri.blogspot.com
     
    Gavri Fernandez, Mar 19, 2005
    #16
  17. On Fri, 18 Mar 2005, Phrogz wrote:
    > Aside - whatever the name, what an excellent, versatile function! For
    > example, the linked-to geometric series sum at
    > http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt can be
    > written in a more terse and (IMO) more ruby-esque way as simply:
    > def geometric_series_sum( x, n )
    > (0..n).inject(0){ |sum,i| sum+x**i }
    > end


    And even shorter,

    def geometric_series_sum(x,n) (x**(n+1)-1)/(x-1) end

    _____________________________________________________________________
    Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
     
    Mathieu Bouchard, Mar 27, 2005
    #17
  18. "Mathieu Bouchard" <> schrieb im Newsbeitrag
    news:p...
    >
    > On Fri, 18 Mar 2005, Phrogz wrote:
    >> Aside - whatever the name, what an excellent, versatile function! For
    >> example, the linked-to geometric series sum at
    >> http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt can be
    >> written in a more terse and (IMO) more ruby-esque way as simply:
    >> def geometric_series_sum( x, n )
    >> (0..n).inject(0){ |sum,i| sum+x**i }
    >> end

    >
    > And even shorter,
    >
    > def geometric_series_sum(x,n) (x**(n+1)-1)/(x-1) end


    LOL ... and more efficient also. Know your math. :)

    Cheers

    robert
     
    Robert Klemme, Mar 27, 2005
    #18
  19. "Mathieu Bouchard" <> schrieb im Newsbeitrag
    news:p...
    >
    > On Fri, 18 Mar 2005, Phrogz wrote:
    >> Aside - whatever the name, what an excellent, versatile function! For
    >> example, the linked-to geometric series sum at
    >> http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt can be
    >> written in a more terse and (IMO) more ruby-esque way as simply:
    >> def geometric_series_sum( x, n )
    >> (0..n).inject(0){ |sum,i| sum+x**i }
    >> end

    >
    > And even shorter,
    >
    > def geometric_series_sum(x,n) (x**(n+1)-1)/(x-1) end
    >


    To be very picky:

    def geometric_series_sum(x,n) (x==1) ? n+1 : (x**(n+1)-1)/(x-1) end

    But given how good the original implementation is (it calculates the
    power with a loop, restarting from 1 for every n...), almost anything
    is an improvement.

    Guillaume.
     
    Guillaume Marcais, Mar 28, 2005
    #19
    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:
    580
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    295
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    256
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    668
    David A. Black
    Jul 2, 2008
  5. dt

    convert string to hash of hash?

    dt, Mar 1, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    497
    ~greg
    Mar 2, 2007
Loading...

Share This Page