in reply to Sort string according to numbers in it
You aren't very clear on precisely what your output should be. It sounds like you want an ascending sort based on the number next to the name. This will do that:
use strict; use warnings; my $str = 'Lee Morgan : 20 Clifford Brown : 3 Freddie Hubbard : 6 '; my @data = map {[ split /\s+:\s+/, $_]} split /\n/, $str; for (sort { $a->[1] <=> $b->[1] } @data) { print join( ' : ', @$_ ), $/; }
You can break the string up with split into a 2D array and then sorting by that number becomes much easier.
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sort string according to numbers in it
by Alexander75 (Novice) on Apr 30, 2015 at 18:19 UTC | |
by jeffa (Bishop) on Apr 30, 2015 at 19:40 UTC | |
by AnomalousMonk (Archbishop) on Apr 30, 2015 at 22:09 UTC | |
by jeffa (Bishop) on Apr 30, 2015 at 22:18 UTC |