http://www.perlmonks.org?node_id=368850

NovMonk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Kind Monks,

First, I have a confession-- about a month ago I received some great clues from graff, Belgarion, cLive;-), revdiablo, and punkish concerning this node. I usually thank people who help me out, but this time, illness, life and other misfortunes delayed me. So, I say now, thanks very much, you fellows are a credit to the community, and I'm really sorry for not saying it sooner.

Now for my question. I've written a script that works interactively with vi to add some code to a text file. The text file starts out something like this:

l ban n10total n01first line n01second line --- (more lines) n01fifteenth line (approx) l ban2 ... (variations on above)

When I get done, I get this:

l ban1 tstat propmean;elms=ABC (etc); n10 n01first line;ID=A n01second line;ID=B --- (etc) n01fifteenth line;ID=Q l ban2 tstat (line as above) n10total n01first line;ID=A (etc.)

What I need help with is this-- I'd like each tstat line to only show the letters I'm using in the n01 lines below it, for the stuff between one l ban line and the next. I'd also like it to error out if the number of n01 lines in any section (defined as one line beginning with l and the next such line-- there can be any number of such sections in the file, though probably, not more than 3 or 4) exceeds the number of available letters in my array @letters. And I'd like it to do this without munching my text file.

Here's my code:

#!/usr/bin/perl use warnings; use strict; use diagnostics; #array @letters is all letters except O,I,L (not used in this process) my @letters = qw/A B C D E F G H J K M N P Q R S T U V W X Y Z/; my $count = 0; while (<>){ if (/^[Ll]/){ s/\n/\ntstat propmean;elms=AB,CDEFGHJKMNPQRSTUVWXYZ;clevel=95,90;p +ropcorr\n/g; $count = 0; } if (/^[Nn]01/){ s/\n/;ID=$letters[$count]\n/; $count++; } print $_; }

It's very simple stuff, but I am still fairly new and clueless. I assume I need to completely revamp my thinking, if my two enhancements are possible at all. I would appreciate not only ideas but explanations of what I could be doing better. And this time, I'll be more prompt in thanking you for the wisdom.

Pax,

NovMonk