Update: Added "-w" and "use strict" and declared variables after rereading TomDLux's update.

Thanks for the hints-- I got something that works And learned a lot in the process. Which is always the goal. In case anyone ever is interested in calling a Perl script from VI, here's my solution (does several things besides those ennumerated in OP):

#!/usr/bin/perl -w use strict; my ($num,$v,$text,$qnum,$qtext,$stext,$spec,$x,$qspec); while (<>){ s/ \t/\t/g; s/9[0-9][0-9][0-9] \t/9999\t/g; s/9[0-9][0-9][0-9] /9999\t/g; if (/^[SCQD]/){ ($num,$v,$text) = split("\t"); chop ($num); $qnum=substr($_,0,index($_,".")); $qtext = substr($text,0,index($text,"?")+1); print "n03;coltxt=___\nn05TOTAL\nL $qnum\nttl$qnum. $qtext\nn1 +0TOTAL BASE\n"; } else { ($stext,$spec,$x) = split("[\t]"); chomp ($spec); $qspec = substr($spec,1,1); unless (/^$/){ print "n01$stext;c=c{$qnum}'$qspec'\n"} } } print "n03;coltxt=___\nn05TOTAL\n";

Basically, the key is to clear out some of the less predictable junk Before the if statement, and then use substr to control the more predictable junk in the scalar variables. And then if you want to add a line or two to the end of the section you're editing, print it in after the while loop and it adds the line(s) to the file.

Thanks again for the help.

Pax,

NovMonk

ps TomDLux: You're right about the chomp ($qspec); -- it's there because it Should be the last thing on the line, but wasn't always. I knew I didn't need anything after that field, so I named an extra one to make sure that junk didn't become part of $qspec.


In reply to Re: Using perl interactively with VI by NovMonk
in thread Using perl interactively with VI 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.