UPDATE: Thanks to AnomalousMonk for catching my mistake in forgetting that map needed to return a reference to the new array. Corrected in the code below. Also, he has a very nice way to handle the sorting choice; check that out in his reply.

1. In split you have specified ' ' which means it will split on single space, but in reality it splits for any number of space.

Right, that's a special case for split, which splits on any whitespace. That usually works well unless you need something more specific -- say, if your fields are separated by tabs but can include spaces. If you need to split on a specific type or amount of whitespace, adjust the first argument to split accordingly.

2. How can I do it using 'Schwartzian transform', I am still novice in perl, please do not mind.

I wouldn't call that a novice-level technique; I probably used Perl for several years before creating a ST myself. You can find plenty of examples and tutorials on it. But basically, it goes something like this:

# in pseudo-code: for each element calculate the sorting value for that element put the original element and the sorting value into a 2-element list pass these 2-element lists to your sorting routine, which sorts based on the sorting value of each element for each element in the sorted list of 2-element lists pull out the original element # in perl, an example using the typical map/sort/map layout # to sort a list of numbers based on the return value of a # complex subroutine that calculates the number of primes # less than each number's 25th power: my @newarray = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_ => n_primes_less_than_25th_power($_) ] } @oldarray;

The essence is that each element of @oldarray is passed to the map which does the complex calculation on that element and creates a 2-element array containing the original element and the calculated value. References to those are passed to the sort, which can then sort on the calculated values without needed to recalculate them for every comparison. The sorted references then go to the map which just passes on the original values.

Your case would look much like this, except that instead of calling my n_primes...() subroutine, you'd have some code (or a call to a subroutine) in that location that parses out the value on which you want to sort. The first element $_->[0] would be the line, and the second element $_->[1] would be the parsed-out value for sorting.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re^6: Sorting based on any column by aaron_baugher
in thread Sorting based on any column by Anonymous Monk

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.