<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Using Sort in PERL in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979540#M419503</link>
    <description>I'm trying to sort an alphanumeric text in perl and it's sorting like this:&lt;BR /&gt;&lt;BR /&gt;boo1&lt;BR /&gt;boo10&lt;BR /&gt;boo2&lt;BR /&gt;foo1&lt;BR /&gt;foo2&lt;BR /&gt;foo20&lt;BR /&gt;foo3&lt;BR /&gt;foo4&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;Is there a way to get perl to sort alphanumically so it sorts like&lt;BR /&gt;boo1&lt;BR /&gt;boo2&lt;BR /&gt;...&lt;BR /&gt;boo10&lt;BR /&gt;??&lt;BR /&gt;&lt;BR /&gt;I tried looking at some sort man pages but I can't find options like the unix sort.&lt;BR /&gt;</description>
    <pubDate>Wed, 17 May 2006 14:47:38 GMT</pubDate>
    <dc:creator>Jeff Colyer</dc:creator>
    <dc:date>2006-05-17T14:47:38Z</dc:date>
    <item>
      <title>Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979540#M419503</link>
      <description>I'm trying to sort an alphanumeric text in perl and it's sorting like this:&lt;BR /&gt;&lt;BR /&gt;boo1&lt;BR /&gt;boo10&lt;BR /&gt;boo2&lt;BR /&gt;foo1&lt;BR /&gt;foo2&lt;BR /&gt;foo20&lt;BR /&gt;foo3&lt;BR /&gt;foo4&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;Is there a way to get perl to sort alphanumically so it sorts like&lt;BR /&gt;boo1&lt;BR /&gt;boo2&lt;BR /&gt;...&lt;BR /&gt;boo10&lt;BR /&gt;??&lt;BR /&gt;&lt;BR /&gt;I tried looking at some sort man pages but I can't find options like the unix sort.&lt;BR /&gt;</description>
      <pubDate>Wed, 17 May 2006 14:47:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979540#M419503</guid>
      <dc:creator>Jeff Colyer</dc:creator>
      <dc:date>2006-05-17T14:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979541#M419504</link>
      <description>Hi Jeff:&lt;BR /&gt;&lt;BR /&gt;Assuming data as shown (3-letters followed by digits), this would work:&lt;BR /&gt;&lt;BR /&gt;# perl -le '@a=qw(boo2 foo1 boo10 foo2 foo20 foo3 foo4);@a=sort{substr($a,0,3) cmp substr($b,0,3)||substr($a,3) &amp;lt;=&amp;gt; substr($b,3)} @a;print "@a"'&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 17 May 2006 15:14:39 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979541#M419504</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-05-17T15:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979542#M419505</link>
      <description>Ideal for a so-called Swarzian Transform:&lt;BR /&gt;&lt;BR /&gt;my @sorted = map { $_-&amp;gt;[0] }&lt;BR /&gt;sort { $a-&amp;gt;[1] cmp $b-&amp;gt;[1] || $a-&amp;gt;[2] &amp;lt;=&amp;gt; $b=&amp;gt;[2] }&lt;BR /&gt;map { [ $_, ($_ =~ m/^([a-z]+)(\d+)/), "", 0 ]&lt;BR /&gt;@unsorted;&lt;BR /&gt;&lt;BR /&gt;Enjoy, Have FUN! H.Merijn</description>
      <pubDate>Wed, 17 May 2006 15:49:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979542#M419505</guid>
      <dc:creator>H.Merijn Brand (procura</dc:creator>
      <dc:date>2006-05-17T15:49:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979543#M419506</link>
      <description>procura,&lt;BR /&gt;If I use the approach you mentioned, how would I fit it in here:&lt;BR /&gt;my $temp;                          &lt;BR /&gt;for $temp ( sort keys %scalelist) {&lt;BR /&gt;  my $rec = $scalelist{$temp};     &lt;BR /&gt;  do {                             &lt;BR /&gt;&lt;BR /&gt;I'm trying to sort the keys which can be any length numeric string followed by a numberic string. &lt;BR /&gt;Ex: &lt;BR /&gt;xxxxx1&lt;BR /&gt;xxxxx10&lt;BR /&gt;xxxxx2&lt;BR /&gt;yyy1&lt;BR /&gt;yyy2&lt;BR /&gt;yyy20&lt;BR /&gt;yyy3&lt;BR /&gt;&lt;BR /&gt;I'm just not sure how to put it in this statement.  Maybe create a sub called NewSort and put that sub in place of the sort above???&lt;BR /&gt;&lt;BR /&gt;Thanks in advance Jeff.</description>
      <pubDate>Wed, 17 May 2006 18:20:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979543#M419506</guid>
      <dc:creator>Jeff Colyer</dc:creator>
      <dc:date>2006-05-17T18:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979544#M419507</link>
      <description>Hi (again) Jeff:&lt;BR /&gt;&lt;BR /&gt;Merijn's solution is by far more generalized for your data than mine, let alone much more efficient.&lt;BR /&gt;&lt;BR /&gt;Assuming that you have collected your data in a hash called 'scalelist' with keys that look like your original post, do something like:&lt;BR /&gt;&lt;BR /&gt;@sorted = map { $_-&amp;gt;[0] }&lt;BR /&gt;sort { $a-&amp;gt;[1] cmp $b-&amp;gt;[1] || $a-&amp;gt;[2] &amp;lt;=&amp;gt; $b-&amp;gt;[2] }&lt;BR /&gt;map { [ $_, ($_ =~ m/^([a-z]+)(\d+)/), "", 0 ] }&lt;BR /&gt;keys %scalelist;&lt;BR /&gt;&lt;BR /&gt;foreach $key (@sorted) {&lt;BR /&gt;    print "$key =&amp;gt; $scalelist{$key}\n";&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 17 May 2006 19:02:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979544#M419507</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-05-17T19:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979545#M419508</link>
      <description>James,&lt;BR /&gt;I got this error when trying that:&lt;BR /&gt;&lt;BR /&gt;Useless use of spaceship operator in void context at line 183. &lt;BR /&gt;                                            Sort subroutine didn't return a numeric value at line 185.     &lt;BR /&gt;                                &lt;BR /&gt;The lines correspond with the use of "map" syntax.&lt;BR /&gt;</description>
      <pubDate>Wed, 17 May 2006 19:31:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979545#M419508</guid>
      <dc:creator>Jeff Colyer</dc:creator>
      <dc:date>2006-05-17T19:31:26Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979546#M419509</link>
      <description>I not only forgot the last brace, but also typoed in the sort (=&amp;gt; should be -&amp;gt;) :/&lt;BR /&gt;&lt;BR /&gt;my @sorted = map { $_-&amp;gt;[0] }&lt;BR /&gt;sort { $a-&amp;gt;[1] cmp $b-&amp;gt;[1] || $a-&amp;gt;[2] &amp;lt;=&amp;gt; $b-&amp;gt;[2] }&lt;BR /&gt;map { [ $_, ($_ =~ m/^([a-z]+)(\d+)/), "", 0 ] }&lt;BR /&gt;@unsorted;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Mea culpa.&lt;BR /&gt;&lt;BR /&gt;lt09:/home/merijn 109 &amp;gt; cat test.txt&lt;BR /&gt;boo1&lt;BR /&gt;boo10&lt;BR /&gt;boo2&lt;BR /&gt;foo1&lt;BR /&gt;foo2&lt;BR /&gt;foo20&lt;BR /&gt;foo3&lt;BR /&gt;foo4&lt;BR /&gt;lt09:/home/merijn 110 &amp;gt; cat test.pl&lt;BR /&gt;#!/pro/bin/perl&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;my @unsorted = &amp;lt;&amp;gt;;&lt;BR /&gt;&lt;BR /&gt;my @sorted =&lt;BR /&gt;    map  { $_-&amp;gt;[0] }&lt;BR /&gt;    sort { $a-&amp;gt;[1] cmp $b-&amp;gt;[1] || $a-&amp;gt;[2] &amp;lt;=&amp;gt; $b-&amp;gt;[2] }&lt;BR /&gt;    map  { [ $_, ($_ =~ m/^([a-z]+)(\d+)/), "", 0 ] }&lt;BR /&gt;    @unsorted;&lt;BR /&gt;&lt;BR /&gt;print @sorted;&lt;BR /&gt;lt09:/home/merijn 111 &amp;gt; perl test.pl test.txt&lt;BR /&gt;boo1&lt;BR /&gt;boo2&lt;BR /&gt;boo10&lt;BR /&gt;foo1&lt;BR /&gt;foo2&lt;BR /&gt;foo3&lt;BR /&gt;foo4&lt;BR /&gt;foo20&lt;BR /&gt;lt09:/home/merijn 112 &amp;gt;&lt;BR /&gt;&lt;BR /&gt;OK, let's also explain.&lt;BR /&gt;&lt;BR /&gt;@unsorted is your array&lt;BR /&gt;&lt;BR /&gt;perl's 'map' transforms each element of the list passed to something else (that can be anything):&lt;BR /&gt;&lt;BR /&gt;@high = map { $_ + 10 } 1..4;&lt;BR /&gt;&lt;BR /&gt;In above case we pass the list (1, 2, 3, 4) to map. It gets each element in turn (in $_), and adds 10 to it, passing it along. So now @high is (11, 12, 13, 14)&lt;BR /&gt;&lt;BR /&gt;For a S-T sort, the first map converts to an anonymous array&lt;BR /&gt;&lt;BR /&gt;@both = map { [ $_, $_ + 10 ] } 1..4;&lt;BR /&gt;&lt;BR /&gt;will transform the list 1,2,3,4 to ([1,11],[2,12],[3,13],[4,14])&lt;BR /&gt;It is common to put the original value in the first element of the list, and the values to sort on in the rest&lt;BR /&gt;&lt;BR /&gt;Now the sort doesn't ket the original values, but a list of lists, where it can efficiently sort on the precalculated elements in the list, so the calculations only have to be done once, instead of on every comparison&lt;BR /&gt;&lt;BR /&gt; map  { [ $_, ($_ =~ m/^([a-z]+)(\d+)/), "", 0 ] }&lt;BR /&gt;&lt;BR /&gt;$_ is your value&lt;BR /&gt;($_ =~ m/..../) is evaluated in list context, and thust returns the caught elements in $1 and $2. In even terser mode, I could have left out $_ =~, since that is the default for m//.&lt;BR /&gt;If that fails, it catches nothing, causing errors in the compare part, which is why I also added "", and 0 to the list. For good matches, these two are ignored in the sort phase, but are a catch-net for failed matches. So&lt;BR /&gt;&lt;BR /&gt;map  { [ $_, ($_ =~ m/^([a-z]+)(\d+)/), "", 0 ] } qw( boo1 boo12 frubble4 blast99 );&lt;BR /&gt;&lt;BR /&gt;would return&lt;BR /&gt;&lt;BR /&gt;[ "boo1", "boo", 1 ], [ "boo12", "boo", 12 ], [ "frubble4", "frubble", 4 ], [ "blast99", "blast", 99 ]&lt;BR /&gt;&lt;BR /&gt;As you see this list contains all you need for a proper sort&lt;BR /&gt;&lt;BR /&gt;the sort part sorts the anonymous lists on the second element (the letter part) with cmp, and if that is equal, on the numeric part with &amp;lt;=&amp;gt;&lt;BR /&gt;&lt;BR /&gt;([ "boo1", "boo", 1 ], [ "boo12", "boo", 12 ], [ "blast99", "blast", 99 ], [ "frubble4", "frubble", 4 ])&lt;BR /&gt;&lt;BR /&gt;That last map, just gets you the first element of the list out again, throwing the rest away&lt;BR /&gt;&lt;BR /&gt;("boo1", "boo12", "blast99", "frubble4")&lt;BR /&gt;&lt;BR /&gt;The Swartzian Transform is also described in the perl FAQ:&lt;BR /&gt;&lt;BR /&gt;# perldoc -q 'How do I sort an array by (anything)?'&lt;BR /&gt;&lt;BR /&gt;Enjoy, Have FUN! H.Merijn</description>
      <pubDate>Thu, 18 May 2006 01:32:06 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979546#M419509</guid>
      <dc:creator>H.Merijn Brand (procura</dc:creator>
      <dc:date>2006-05-18T01:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Using Sort in PERL</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979547#M419510</link>
      <description>Thanks Merijn.  Your explanation really helped me out.  I was able to get it to work with your explanation and code snippet.</description>
      <pubDate>Thu, 18 May 2006 09:23:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sort-in-perl/m-p/4979547#M419510</guid>
      <dc:creator>Jeff Colyer</dc:creator>
      <dc:date>2006-05-18T09:23:04Z</dc:date>
    </item>
  </channel>
</rss>

