How can I sort Perl hashes whose values ​​are array references?

Hey, I'm just wondering if there is a cool "one liner" that would sort my hash massive links. So I have a bunch of keys / values ​​in my hash, like this:

$DataBase{$key} = \@value;

However, I would like to sort the hash by an element array[0]. Then scroll them. I had this for starters:

foreach my $key (sort {$DataBase{$a} cmp $DataBase{$b} } keys %DataBase)

But this, obviously, just sorts my hash by the value of the array pointer. This does not have to be a “single line”, but I was hoping for a solution that is not related to restoring the hash.

+3
source share
3 answers
foreach my $key (sort {$DataBase{$a}->[0] cmp $DataBase{$b}->[0] } keys %DataBase)
+8
source

(, , C) Perl , :

Perl [...] "", C. , C , , Perl (, ) , , typeglobs . C, .

, .

.

+2

, , - Perl?. , FAQ Perl, , , .

+1

Source: https://habr.com/ru/post/1707720/


All Articles