in reply to Need help in sorting the array
You Could use a custom sort routine:
use warnings; use strict; my @arr = ('TEXT1','TEXT11','TEXT13','TEXT2','TEXT3'); print "BEFORE:\n"; print "\'$_\'\n" for @arr; ## pass code block to sort to control sort behaviour @arr = sort by_numbers @arr; ## look again print "AFTER:\n"; print "\'$_\'\n" for @arr; sub by_numbers{ my $a_num = $a =~ /^TEXT(\d+)$/; my $b_num = $b =~ /^TEXT(\d+)$/; ## maybe also include checking that $a_num and $b_num are defined return ($a_num <=> $b_num); }
Updated: Typo in code
|
|---|