It's a brand new year and I'm a brand new member! I want to thank you all for your help here.

I've used perl mostly with regex to do text modifications on files for digital publishing. Most of this is simple search/replace phrasing. So that's most of the extent of my knowledge.

I've started in on a new project and have found myself in a quandry. I've got sections in this text file that I need to resort. They look like this:

[section start marker (shift-opt-5)] [some line of text] entry \(frequency data\) entry \(frequency data\) … [section start marker (shift-opt-5)] [and the pattern repeats]

I need to re-sort the entries (not the first line of text) in two ways, first by descending frequency and then within equal frequencies, by ascending alphabet.

I know that if I create an array of data I can do this kind of sort. Here is what I have in that regard:

sub two_way_sort { ($b =~ /\((\d+)/)[0] <=> ($a =~ /\((\d+)/)[0] || lc($a) cmp lc($b) } my @unsorted_input; while (my $line = <Input>) { [section here of some text edits] push @unsorted_input, $line; } my @sorted = sort two_way_sort @unsorted_input;

This code will sort the entire text file as I've placed the whole thing in an array. What I can't seem to figure out is how to create an another array into which I can place each section, do the sort, return the sorted text to the original file, and move on to the next section.

Any help you guys/gals could provide would be amazing. I'm really looking forward to growing in my understanding of Perl.


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