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.


In reply to My best attempt at sorting. Kindly suggest improvements/modifications. by pritesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.