Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Request for comments/ clues on simple text manipulation script

by NovMonk (Chaplain)
on Jun 22, 2004 at 20:49 UTC ( #368850=perlquestion: print w/replies, xml ) Need Help??

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.



Replies are listed 'Best First'.
Re: Request for comments/ clues on simple text manipulation script
by Roy Johnson (Monsignor) on Jun 22, 2004 at 21:16 UTC
    You're going to need to accumulate the lines so that you can print the summary before the details. Something like this (untested):
    # Your count and letters declarations here my $accum; my $banner; while (<>) { if (/^L/i) { if ($banner) { print $banner, 'tstat propmean;elms=', @letters[0..$count- +1], "\n", $accum; } $banner = $_; $accum = ''; $count = 0; } elsif (/^N01/i) { s/\n/;ID=$letters[$count]\n/; ++$count; $accum .= $_; } else { $accum .= $_ } } print $banner, 'tstat propmean;elms=', @letters[0..$count-1], "\n", $a +ccum;

    We're not really tightening our belts, it just feels that way because we're getting fatter.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://368850]
Approved by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-10-03 23:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found