If your real data is not the same as the sample data that you posted, then you will need to adjust the code I offered to suit.

The basic problem is that you want to sort your strings according to a numeric field embeded part way through your strings. To do that, you need to extract the numeric value from each string in order to compare them. In the sample data you supplied, the numeric field followed a (unique) comma and a space, so extracting that using a regex was easy.

$string =~ m[ , # a comma \s+ # followed by at least one space ( # capture \d+ # one or more digits ) ];

In the following code, I've done two things that hopefully will clarify what is going on.

  1. I've added an extra map block before the sort that prints out the modified records so that you can see the effect of the sprintf.
  2. I've added a test before the sprintf to detect and die if any records are encountered that don't match the regex.
#! perl -slw use strict; my @array = split '\n', <<'EOA'; Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar EOA my @sorted = map{ # Strip the first five characters that we added earlier. substr $_, 5; } sort { # make the sort descending $b cmp $a } map { # print out the modified record to show what sprintf is doing print "before sort: '$_'"; $_; } map { ## try the regex and die displaying ## the failing record if it doesn't match die "Regex did not match: '$_'" unless m[,\s+(\d+)]; # It matched, so $1 contains the value to prepend. # pad to width 5. # use a bigger value here and above in the substr # if your numbers can be bigger than 99999. sprintf '%05d%s', $1, $_; } @array; print $/; print for @sorted; __END__ P:\test>test2 before sort: '00002Item1 - 2 foo, 2 bar' before sort: '00001Item2 - 0 foo, 1 bar' before sort: '00003Item3 - 1 foo, 3 bar' before sort: '00002Item4 - 1 foo, 2 bar' Item3 - 1 foo, 3 bar Item4 - 1 foo, 2 bar Item1 - 2 foo, 2 bar Item2 - 0 foo, 1 bar

Hopefully, that will clarify what the code does and also allow you to modify the regex to suit your data.

You can delete the extra map block once you've seen what the sprintf is doing, but I would leave the die...unless code in place so that you will detect and get a record of any malformed records in use.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: What is sprintf doing? by BrowserUk
in thread What is sprintf doing? by McMahon

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.