in reply to sorting hash by a certain value (that it is in an array)
In complement to Corion's answer: Re: sorting hash by a certain value (that it is in an array)
Here is an example. You can use more complexe sorting method (see commented line: lc(...) cmp lc(...)) and optimized it with The Orcish Maneuver.#!/usr/bin/env perl use strict; use warnings; my %books=( '34' => ['enlgish', 'dani M. rod', 54], '24' => ['spanish', 'ramk rovale', 41], '54' => ['enlgish', 'bob falicas', 17], ); my @sorted_books = sort { $books{$a}->[1] cmp $books{$b}->[1] #lc($books{$a}->[1]) cmp lc($books{$b}->[1]) } keys %books; print "@sorted_books";
Update: print sorted books
for my $book_id (@sorted_books) { print "$book_id: ",join ", ",@{ $books{$book_id} }; print "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sorting hash by a certain value (that it is in an array)
by gooyava (Novice) on Jul 17, 2012 at 13:27 UTC | |
by Corion (Patriarch) on Jul 17, 2012 at 13:34 UTC | |
by jethro (Monsignor) on Jul 17, 2012 at 13:35 UTC | |
by clueless newbie (Curate) on Jul 17, 2012 at 16:06 UTC | |
by brx (Pilgrim) on Jul 17, 2012 at 15:50 UTC |