Greetings, Esteemed Monks,

I have been playing around today with a little program that will read in a series of lines from an input file (using __DATA__ below for simplicity), use some things on those lines to create some new lines, and write the whole thing back out to a new file. So for an input file like this:

line1 line2 n10total n01chocoloate;more stuff n01vanilla;more stuff etc...

I can get this:

line1 line2 n10total n01chocolate;more stuff n01vanilla;more stuff g total choco vanil g ----- ----- ----- p x x x

Suppose I now want it to be a little smarter. I want it to look at how many letters/ spaces are before the ; on each n01 line, and make the g and p lines conform to that, so I have instead:

g total chocolate vanilla g ----- --------- ------- p x x x

and I want to keep the whole thing from getting over a page width of 142 characters. Here's my code thus far. I'd appreciate comments on how I could make what I'm currently doing better, and on whether my thoughts below about how I want to tackle my new objective are anywhere near the right track. Note, I'm not looking for solutions, but feedback on what I've got and where I might look next to build on my understanding.

#!/usr/bin/perl use warnings; use strict; my (@text, @gcard, @pcard); my ($ntext, $ftext, $gtext, $ptext); open (OUT, ">ban.test") or die "Can't open ban.test for write: $!\n"; while (<DATA>){ if (/^n10/) { @text = "g total"; @gcard = "g -----"; @pcard = "p x "; print OUT "$_"; } elsif (/^n01/) { $ntext = (split (/;/)) [0]; $ntext =~ s/n01//g; $ftext = substr($ntext,0,5); $gtext = "-----"; $ptext = " x "; push(@text, $ftext); push(@gcard, $gtext); push(@pcard, $ptext); print OUT "$_"; } else { print OUT "$_"; } } print OUT "\n"; print OUT "@text\n"; print OUT "@gcard\n"; print OUT "@pcard\n"; __DATA__ line1 line2 n10total n01chocoloate;more stuff n01vanilla;more stuff

I'm thinking first I want to know how many n01 lines there are (if over a certain number, I probably want to restrict each ----- to something like 5 or 6 characters). To get that, I'm thinking I might read the input file into an array and count the n01 lines as they go by, then have my while loop above run through that array.

Otherwise, I want to know how many characters are in each $ntext as they are read, and adjust the number of characters for $gtext and $ptext accordingly. I think getting the number of characters would be something like

$x = length ($gtext);
and then using $x as the length of my strings for all 3 variables, which would then look something like this:

$ftext = substr($ntext,0,$x);

I realize that will require me to change how I'm dealing with $gtext and $ptext, but that doesn't seem too difficult. I imagine if the $ntext is over a certain length, I'll want to truncate it, same as if there are too many n01 lines. I suspect that will be putting some subroutines in my future, if nothing else.

I'm sure there are dozens of nifty modules that could do all this for me, but my aim is to do a bit of learning as I reinvent the wheel.

I'm sure it's obvious that I'm very new to this-- but I hope it's also apparent that I'm progressing. Thanks as always for the wise admonishments.

NovMonk


In reply to Feedback request for text file manipulator by NovMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.