Hi Monks,
I've been trying to get back to Perl and was working on a sorting method.
The goal was, to sort a list of numbers. These numbers will be in decimal/hex/octal/binary format, negative and positive, and duplicates. Duplicates should not be removed.
After browsing the PerlMonks forum, I came across some interesting ideas, and came up with the following script:
Note:- kcott helpfully directed me to the inbuilt sort function, but what I was trying to do was create my own sorting function.
use strict; use warnings; my @array = (-10,30,5,0b1011,7,9,8, -01111,-10,1,3,9,-30,5,-100,0,-1,0 +xFF,-3,30,0b1011,3,2,-300,-0xFF,15); sub sortie { my @sorted; my @sortit = @_; my @biglist; my @uniq; my $biggest_so_far = 0; my $smallest_so_far = 0; foreach my $num(@sortit) { if ($num > $biggest_so_far) { $biggest_so_far = $num; } if ($num < $smallest_so_far) { $smallest_so_far = $num; } } @biglist = ($smallest_so_far..$biggest_so_far); foreach my $bignum ( @biglist) { foreach my $sortnum (@sortit) { if ($bignum == $sortnum) { push @sorted, $bignum; } } } print "\@array with ", scalar @array, " elements = @array\n +"; print "\@sorted with ", scalar @sorted, " elements = @sorted\n" +; } sortie(@array);
And here is the output:
PS C:\perl_practice> perl .\best_attempt_sort.pl @array with 26 elements = -10 30 5 11 7 9 8 -585 -10 1 3 9 -30 5 - +100 0 -1 255 -3 30 11 3 2 -300 -255 15 @sorted with 26 elements = -585 -300 -255 -100 -30 -10 -10 -3 -1 0 +1 2 3 3 5 5 7 8 9 9 11 11 15 30 30 255
Kindly suggest improvements/modifications.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |