programming-examples/perl/Array/ASCII and Numeric Sort Using Subroutine.pl

18 lines
439 B
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
@list=("A","B", "C","D" );
print "Original list: @list\n";
# ASCII sort using a subroutine
sub asc_sort{
$a cmp $b; # Sort ascending order
}
@sorted_list=sort asc_sort(@list);
print "Ascii sort: @sorted_list\n";
# Numeric sort using subroutine
sub numeric_sort {
$a <=> $b ;
} # $a and $b are compared numerically
@number_sort=sort numeric_sort 10, 0, 5, 9.5, 10, 1000;
print "Numeric sort: @number_sort.\n";