From: Jack Dempsey Date: 2002-01-06T18:22:25+09:00 Subject: [ruby-talk:30338] Re: OT: Re: Sorting a Hash by value of integer stored in the Hash This is a multi-part message in MIME format. ------=_NextPart_000_000F_01C19669.5D231FC0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable also, try doing something like h =3D Hash[*array] not sure if i got the syntax correct, and nobu's to_hash is more = elegant, but it was kinda cool to find out this worked as well when i = asked this same question about 3 months ago Jack Hello, Yohanes Santoso wrote: > irb(main):011:0> myh =3D {"z"=3D>5,"x"=3D>3,"y"=3D>7} > irb(main):012:0> myh.sort{|a,b| a[1] <=3D> b[1]}=20 > ary =3D [["x", 3], ["z", 5], ["y", 7]] ^^^^^^^ Is there any "automatic" way to create hash from ary (above). I didn't find any. (I know hash =3D Hash[[1,2,3,4]], but I doesn't work for this) Don't you think that it is strange is hash.to_a produces ary (above), but there is now way to create hash back from this ary. I think we should define new method class Hash def Hash::new_from_array(*ary) if ary.size =3D=3D 1 then ary =3D ary[0] end hash =3D {} ary.each do |i| hash[i[0]] =3D i[1] end hash end end Or something like this, but with achieving compatibility with 'old' style Hash[]. Michal --=20 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Michal Rokos Czech Technical University, Prague E-mail: m.rokos@sh.cvut.cz Jabber: majkl@jabber.cz, ICQ: 36118339 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- ------=_NextPart_000_000F_01C19669.5D231FC0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable

also, try doing something like

h =3D Hash[*array]

not sure if i got the syntax correct, and nobu's = to_hash is=20 more elegant, but it was kinda cool to find out this worked as well when = i asked=20 this same question about 3 months ago

Jack

 

Hello,

Yohanes Santoso <ysantoso@jenny-gnome.dyndns.org> wrote:

> irb(main):011:0> myh =3D = {"z"=3D>5,"x"=3D>3,"y"=3D>7}

> irb(main):012:0> myh.sort{|a,b| a[1] <=3D> b[1]}

> ary =3D [["x", 3], ["z", 5], ["y", 7]]

^^^^^^^

Is there any "automatic" way to create hash from ary (above). I

didn't find any. (I know hash =3D Hash[[1,2,3,4]], but I doesn't

work for this)

Don't you think that it is strange is hash.to_a produces ary

(above), but there is now way to create hash back from this ary.

I think we should define new method

class Hash

def Hash::new_from_array(*ary)

if ary.size =3D=3D 1 then ary =3D ary[0] end

hash =3D {}

ary.each do |i|

hash[i[0]] =3D i[1]

end

hash

end

end

Or something like this, but with achieving compatibility with

'old' style Hash[].

Michal

--

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D= -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=

Michal Rokos Czech Technical University, Prague

E-mail: m.rokos@sh.cvut.cz Jabber: majkl@jabber.cz, ICQ: 36118339

-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D= -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=

 

 
------=_NextPart_000_000F_01C19669.5D231FC0--