[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Convert a Hash into an Array

Eustaquio Rangel de Oliveira Jr.

1/24/2005 9:03:00 PM

-----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
eustaquiorangel@yahoo.com
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFB9WJib6UiZnhJiLsRAlZJAKC3n+pmVAsm3qLxWG5li9BDOh2IGQCff8m3
LsYXA0CuhQt/zejSKvSJbmM=
=b4wV
-----END PGP SIGNATURE-----


20 Answers

Michael Neumann

1/24/2005 9:05:00 PM

0

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


Eustaquio Rangel de Oliveira Jr.

1/24/2005 9:15:00 PM

0

-----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
eustaquiorangel@yahoo.com
http://b...
Usuário GNU/Linux no. 224050
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFB9WU/b6UiZnhJiLsRAqZ2AKCVcApOvbi8qTBh3+H9L8PcmJ8duQCgoSsa
ogE6MAn8QflWy8ZJnqufNkg=
=iJ3K
-----END PGP SIGNATURE-----


dblack

1/25/2005 1:54:00 AM

0

Robert Klemme

1/25/2005 8:10:00 AM

0


"David A. Black" <dblack@wobblini.net> 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

Richard Turner

1/25/2005 9:15:00 AM

0

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.



Pit Capitain

1/25/2005 9:32:00 AM

0

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


Richard Turner

1/25/2005 11:17:00 AM

0

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.



georgesawyer

1/28/2005 12:20:00 PM

0

"Robert Klemme" <bob.news@gmx.net> Jan 25, 2005 at 09:09 AM wrote:

>did I mention that I love #inject? :-)

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

linus sellberg

1/28/2005 1:12:00 PM

0

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)

georgesawyer

1/28/2005 5:59:00 PM

0

linus sellberg <sellberg@google.com> Jan 28, 2005 at 02:12 PM wrote:
>georgesawyer wrote:
>>"Robert Klemme" <bob.news@gmx.net> Jan 25, 2005 at 09:09 AM 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)

More fundamentally, I suggest 'collapse', because the most general concept
seems of collapsing away one dimension from a multi-dimensional thing.
Notwithstanding that the method should allow, perhaps, a scalar argument,
or no arguments. Pushing the concept to all dimensions would be done
instead first by flatten'ing, then by inject'ing (collapse'ing). Reducing
the dimensionality by one seems useful: here and there, I seem to have
heard of it.

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.