Sort keys in a hash numerically

Discussion in 'Perl Misc' started by mickey, Aug 8, 2006.

  1. mickey

    mickey Guest

    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
     
    mickey, Aug 8, 2006
    #1
    1. Advertising

  2. mickey

    Guest

    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.


    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
     
    , Aug 8, 2006
    #2
    1. Advertising

  3. mickey

    Guest

    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)
     
    , Aug 8, 2006
    #3
  4. mickey

    Xicheng Jia Guest

    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
     
    Xicheng Jia, Aug 8, 2006
    #4
  5. mickey

    mickey Guest

    wrote:
    > 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.

    >
    >
    > 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
     
    mickey, Aug 8, 2006
    #5
  6. mickey

    -berlin.de Guest

    Xicheng Jia <> 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
     
    -berlin.de, Aug 11, 2006
    #6
    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. A.T.
    Replies:
    3
    Views:
    781
    Richard Light
    Jul 29, 2004
  2. rp
    Replies:
    1
    Views:
    530
    red floyd
    Nov 10, 2011
  3. Malik Yousef

    Sort Hash o Hash accordint to two keys

    Malik Yousef, May 6, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    197
    Uri Guttman
    May 7, 2004
  4. Malik Yousef

    Sort Hash o Hash accordint to two keys

    Malik Yousef, May 6, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    103
    Malik Yousef
    May 6, 2004
  5. Replies:
    4
    Views:
    139
    Ted Zlatanov
    Jul 18, 2008
Loading...

Share This Page