Hello again monks...

Yesterday I received some great advice about sorting in this thread: Sort array according to a value in each element?

Out of the box, the Guttman-Rosler Tranform from BrowserUKdid almost exactly what I needed. (I've been trying to understand the ST and GRT since then.)

I've got the following code which is working fine:

my @sorted = map{ substr $_, 5; } sort { $b cmp $a } map{ sprintf '%05d%s', $_ =~ m[,\s+(\d+)], $_; } @msgs; #SOME STUFF foreach my $warning(@sorted) { print OUT $warning unless ($warning =~ "<stuff I don't want>"); }


but sprintf reports the following warning on every line of a 7253-line LOG file (line 43 is the "sort..." line):

Use of uninitialized value in sprintf at script.pl line 43, <LOG> line + 7253. Argument <line from LOG> isn't numeric in sprintf at script.pl line 43 +, <LOG> line 7253


Oddly enough, this code works just as well:

my @sorted = map{ substr $_, 125; } sort { $b cmp $a } map{ sprintf '%0125d%s', $_ =~ m[,\s+(\d+)], $_; } @msgs;


I just have a feeling that if I could figure out the sprintf/substr interaction, it would be easier to figure out the rest of the code. I'm pretty sure I know what substr is doing here, but I'm not getting the relation to sprintf.

Again, I've got working code, now I'm just trying to make sure I understand why it works.

In reply to 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.