Wednesday 13 November 2019

perl sort keys - hash alphabetically


%hashmap=('awk','perl','code','things','sed','need');

Method 1:[alphabetically]


foreach $keys (sort keys %hashmap) {

    print "$food is $hashmap{$keys}.\n";

}


Method 2:[Sort Based on Value]
foreach $keys (sort { $hashmap{$a} cmp $hashmap{$b} }

                keys %hashmap)

{

    print "$keys is $hashmap{$keys}.\n";

}

Method 3:[sort Based on value Length]
@length = sort { length($hashmap{$a}) <=> length($hashmap{$b}) } keys %hashmap;

foreach $keys (@length) {

    print "$keys is $hashmap{$keys}.\n";

}

kaavannan perl blogspot

Labels: , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home