Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Custom, Reusable Sort Subroutine for Hashes?

by QM (Parson)
on Sep 05, 2017 at 16:58 UTC ( [id://1198710]=note: print w/replies, xml ) Need Help??


in reply to Custom, Reusable Sort Subroutine for Hashes?

Combining some of these ideas, I came up with the following make_sort_sub:
#!/usr/bin/env perl use strict; use warnings; use feature qw{ say }; use Scalar::Util qw{ looks_like_number }; sub make_sort_sub { my $code = shift; my $hashref = shift; my $sort_sub = eval "sub { $code }"; die $@ if $@; return $sort_sub; } # simple code snippet my %hash = ( a => 5, b => 4, c => 3, d => 5, e => 4 ); my $keys_by_value = make_sort_sub( '$hashref->{$a} <=> $hashref->{$b}', \%hash); my @keys_sorted_by_value = sort $keys_by_value keys %hash; say @keys_sorted_by_value; # Try with curlies now $keys_by_value = make_sort_sub( '{$hashref->{$a} <=> $hashref->{$b}}', \%hash); @keys_sorted_by_value = sort $keys_by_value keys %hash; say @keys_sorted_by_value; # Naive compare by values as numbers, keys as strings my $keys_by_value_or_keys = make_sort_sub( '$hashref->{$a} <=> $hashref->{$b} or $a cmp $b', \%hash); my @keys_by_value_or_keys = sort $keys_by_value_or_keys keys %hash; say @keys_by_value_or_keys; # Compare as numbers then strings, values then keys my %hash2 = (a => 'a', b => 'b', c => 3, d => 4); my $keys_by_value_or_keys_mixed = make_sort_sub( 'return $hashref->{$a} <=> $hashref->{$b} if ((looks_like_number($hashref->{$a}) and looks_like_number( +$hashref->{$b})) and ($hashref->{$a} <=> $hashref->{$b})); return $hashref->{$a} cmp $hashref->{$b} if ($hashref->{$a} cmp $hashref->{$b}); return $a <=> $b if ((looks_like_number($a) and looks_like_number($b)) and ($a +<=> $b)); return $a cmp $b;', \%hash2); my @keys_by_value_or_keys_mixed = sort $keys_by_value_or_keys_mixed ke +ys %hash2; say @keys_by_value_or_keys_mixed; exit;

Output:

cbeda cbeda cbead cdab

I couldn't figure out a good way to include a real code block, because it couldn't reference the keys and values. Choroba's suggestion is nice, but can't really handle anything other than $a/$b (AFAIK). Chicken and egg.

As an aside, I can't remember if there's a nice CPAN way to do the last sub ("keys by value or by keys, numbers or strings").

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: Custom, Reusable Sort Subroutine for Hashes?
by karlgoethebier (Abbot) on Sep 05, 2017 at 18:58 UTC

    Sort::Maker?

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      ++ for trying, but that doesn't seem to be easier.

      In fact, I think the doc page is one of those "well formed, but not very informative" examples. I was looking for an example in the synopsis, and it has this:

      use Sort::Maker ; my $sorter = make_sorter( ... );

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

        "...doesn't seem to be easier"

        Indeed, unfortunately. Below in the docs it's getting just slightly more informative. I guess you need to guess ;-) BTW, I stumbled over it by chance.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198710]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found