Basically, when something seems too complex, your first instinct as a progammer should be to throw more functions at it. If it doesn't help, your second instinct should be to use objects (as in OOP) :) Or so I heard :)
use strict; use warnings; process_file( \*DATA ); exit 0; sub process_file { my ($fh) = @_; while ( my $line = <$fh> ) { print $line; if ( $line =~ /section start marker/ ) { handle_section($fh); } } } sub handle_section { my ($fh) = @_; my ( @entries, $line ); while ( $line = <$fh> ) { last unless $line =~ /^entry/; push @entries, $line; } print map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ get_cmp_key(), $_ ] } @entries; print $line if defined $line; } sub get_cmp_key { m{ \( (\d+) \s+ (.+) }x or die "Can't match '$_'!"; # inspired by johngg :) return pack 'Na*', $1, $2; } __DATA__ [section start marker (shift-opt-5)] [some line of text] 1 and no entries 0 none at all! [section start marker (shift-opt-5)] [some line of text] entry \(7 data\) entry \(5 data\) entry \(6 data\) [section start marker (shift-opt-5)] [some line of text] entry \(001 data\) entry \(1 data\) entry \(01 data\) ^ those are equivalent as numbers text C text B text A [section start marker (shift-opt-5)] [some line of text] entry \(001 dataC\) entry \(1 dataB\) entry \(01 dataA\)
output:
[section start marker (shift-opt-5)] [some line of text] 1 and no entries 0 none at all! [section start marker (shift-opt-5)] [some line of text] entry \(5 data\) entry \(6 data\) entry \(7 data\) [section start marker (shift-opt-5)] [some line of text] entry \(001 data\) entry \(1 data\) entry \(01 data\) ^ those are equivalent as numbers text C text B text A [section start marker (shift-opt-5)] [some line of text] entry \(01 dataA\) entry \(1 dataB\) entry \(001 dataC\)

In reply to Re: Use Perl's Sort to only sort certain lines in a file? by Anonymous Monk
in thread Use Perl's Sort to only sort certain lines in a file? by grahambuck

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.