When I used your code I added an open line called "Input" and exchanged my $line = <$fh> with my $line = <Input>. Ought I have done that?
No, you just have to supply an argument to the main subroutine.
The output, when I made that change, however looked exactly like the input file. If I gave a sample of the actual text would that help?
It seems your actual text is quite a bit different. Do the blocks of entries have something that can be recognized as a header? (even empty line should work).

Anyway, I added some headers to your sample and put it into file 'input.txt':

section start marker dear (13) dear friends (22) love (10) dear friend (10) loved (3) dearly loved (1) friends (1) section start marker competes in the games (1) contend (1) fight (2) fought (1) make every effort (1) strive (1) wrestling (1)
So the program:
use strict; use warnings; open my $file, '<', 'input.txt' or die $!; process_file($file); exit 0; sub process_file { my ($fh) = @_; while ( my $line = <$fh> ) { print $line; if ( $line =~ /section start marker/ ) { $line = handle_section($fh); redo if defined $line; } } } sub handle_section { my ($fh) = @_; my ( @entries, $line ); while ( $line = <$fh> ) { last unless $line =~ m{ ( [^(]+ ) # 1 anything except opening paren \s # space \( # opening paren ( \d+ ) # 2 number }x; push @entries, [ pack( 'Na*', $2, $1 ), $line ]; } print map { $_->[1] } sort { $a->[0] cmp $b->[0] } @entries; return $line; }
output:
section start marker dearly loved (1) friends (1) loved (3) dear friend (10) love (10) dear (13) dear friends (22) section start marker competes in the games (1) contend (1) fought (1) make every effort (1) strive (1) wrestling (1) fight (2)

In reply to Re^4: 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.