sub pushSection { my ( $hashref, @secNames ) = @_; my $base = (sort @secNames)[0]; $baselen = length $base; my $newBgn = "AA"; my $grex = join( '|', @secNames ); @pushees = sort grep( /^($grex)/, keys %$hashref ); foreach $i (0 .. $#pushees) { $_ = $pushees[$i]; my $txt = $hashref->{$_}; delete $hashref->{$_}; s/^($grex)(.*)/$base$newBgn$2/; $hashref->{$_} = $txt; $newBgn++ if ( $i < @pushees && length($pushees[$i+1]) == $baselen ); } } chomp( @sections = ); foreach (@sections) { next unless (/\S/); # not sure why I needed this $outline{$_} = "some text for section $_"; } print "\nbefore:\n"; map {print "$_\t$outline{$_}\n"} sort keys %outline; pushSection( \%outline, "AAAB", "AAAC" ); print "\nafter:\n"; map {print "$_\t$outline{$_}\n"} sort keys %outline; __DATA__ AA AAAA AAAAAA AAAB AAAC AAACAA AAAD AAAE #### #before :: after AA :: AA AAAA :: AAAA AAAAAA :: AAAAAA AAAB :: AAABAA # need to insert "AAAB" above this line AAAC :: AAABAB AAACAA :: AAABABAA AAAD :: AAAD # need to rename this to "AAAC" AAAE :: AAAE # need to rename this to "AAAD"