Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sort keys in a hash numerically

Reply
Thread Tools

Sort keys in a hash numerically

 
 
mickey
Guest
Posts: n/a
 
      08-08-2006
Hi,

I have a has with keys like
Char. 123
Char. 721
Char. 234

and so on, that is a char followed by an integer.

I want to iterate over the whole hash in numerical order. At present using

foreach my $key(sort (keys %char))

sorts them but in lexicographic order. Is there a way in Perl around that?

Thanks,
-M
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-08-2006
mickey <(E-Mail Removed)> wrote:
> Hi,
>
> I have a has with keys like
> Char. 123
> Char. 721
> Char. 234
>
> and so on, that is a char followed by an integer.


If all the keys start with "Char. ", that seems pretty uninteresting.
Why not just strip that off and store only the number?

>
> I want to iterate over the whole hash in numerical order. At present
> using
>
> foreach my $key(sort (keys %char))


foreach my $key (sort {sfrn($a) <=> sfrn($b)} keys %char)

where sfrn is Some Function that takes a key and Returns the Numberic
portion.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      08-08-2006
mickey wrote:
> sorts them but in lexicographic order. Is there a way in Perl around that?


perldoc -q sort

Your question is answered in the topic:

How do I sort an array by (anything)?

--
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
Xicheng Jia
Guest
Posts: n/a
 
      08-08-2006
mickey wrote:
> Hi,
>
> I have a has with keys like
> Char. 123
> Char. 721
> Char. 234
>
> and so on, that is a char followed by an integer.
>
> I want to iterate over the whole hash in numerical order. At present using
>
> foreach my $key(sort (keys %char))
>
> sorts them but in lexicographic order. Is there a way in Perl around that?
>


A solution with the Schwartzian Transform:

my %hash = (
"Char.123" => "part1",
"Char.721" => "part2",
"Char.234" => "part3",
);

print map { "$_->[0] => $hash{$_->[0]}\n" }
sort { $a->[1][1] <=> $b->[1][1] }
map { [ $_, [ split/\./ ] ] }
keys %hash;

Xicheng

 
Reply With Quote
 
mickey
Guest
Posts: n/a
 
      08-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> mickey <(E-Mail Removed)> wrote:
>
>>Hi,
>>
>>I have a has with keys like
>>Char. 123
>>Char. 721
>>Char. 234
>>
>>and so on, that is a char followed by an integer.

>
>
> If all the keys start with "Char. ", that seems pretty uninteresting.
> Why not just strip that off and store only the number?
>


Thanks everyone. I just stripped off the chars which made everything
simpler.

-M
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      08-11-2006
Xicheng Jia <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> mickey wrote:
> > Hi,
> >
> > I have a has with keys like
> > Char. 123
> > Char. 721
> > Char. 234
> >
> > and so on, that is a char followed by an integer.
> >
> > I want to iterate over the whole hash in numerical order. At present using
> >
> > foreach my $key(sort (keys %char))
> >
> > sorts them but in lexicographic order. Is there a way in Perl around that?
> >

>
> A solution with the Schwartzian Transform:
>
> my %hash = (
> "Char.123" => "part1",
> "Char.721" => "part2",
> "Char.234" => "part3",
> );
>
> print map { "$_->[0] => $hash{$_->[0]}\n" }
> sort { $a->[1][1] <=> $b->[1][1] }
> map { [ $_, [ split/\./ ] ] }
> keys %hash;


A regex capture instead of split is a little simpler:

print map { "$_->[0] => $hash{$_->[0]}\n" }
sort { $a->[1] <=> $b->[1] }
map { [ $_, /(\d+)/ ] }
keys %hash;

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Numerically sort a file on a given column where column is a $var joemacbusiness@yahoo.com Perl Misc 4 07-18-2008 01:48 PM
Sorting alphabetically & numerically A.T. XML 3 07-29-2004 07:08 AM
Sort Hash o Hash accordint to two keys Malik Yousef Perl Misc 9 05-07-2004 06:22 PM
Sort Hash o Hash accordint to two keys Malik Yousef Perl Misc 0 05-06-2004 01:42 AM



Advertisments