in reply to Index sorting

use strict; use warnings; my %ie; my @id; while (<>) { if ( m#<indexEntry># ) # open { print; %ie = (); @id = (); } elsif ( m#</indexEntry># ) # close { # sort the primaries, secondaries, and tertiaries: for my $l ( keys %ie ) { @{$ie{$l}} = sort @{$ie{$l}}; } # output the munged record: for ( @id ) { my( $id, $level, $iei ) = @$_; print qq(<${level}IE id="$id">$ie{$level}[$iei] </${level} +IE>\n); } print; } elsif ( m#<(\w+)IE id="(\d+)">(.*)</\1[I]E># ) { # collect the row of data: my( $level, $id, $text ) = ( $1, $2, $3 ); push @{$ie{$level}}, $text; push @id, [ $id, $level, $#{$ie{$level}} ]; } # else, invalid line of input. }
Assumes the data can be munged through a filter. If not, alter for input and output as appropriate.