Hello,
Hello,
I have an array of names and would like to sort the array by the length
of the name but am having difficulty.
Any suggestions ?
$ perl -le' my @names = qw/ zero one two three four five six seven eight nine /; print "@names";
my @sorted_names = sort @names; print "@sorted_names";
my @by_length = sort { length $a <=> length $b } @names; print "@by_length"; ' zero one two three four five six seven eight nine eight five four nine one seven six three two zero one two six zero four five nine three seven eight
I tried creating two arrays, one with the names and another with the length values, then sorting the length values but found it difficult to print them out.
If you want to keep the by-length index in another array:
$ perl -le' my @names = qw/ zero one two three four five six seven eight nine /; print "@names";
my @sorted_names = sort @names; print "@sorted_names";
my @index_by_length = sort { length $names[$a] <=> length $names[$b] } 0 .. $#names;
print "@[EMAIL PROTECTED]";
'
zero one two three four five six seven eight nine
eight five four nine one seven six three two zero
one two six zero four five nine three seven eight
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>