grahambuck has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|