Esteemed Monks,

I am still working on some text editing utilities, written in Perl which are able to be called from VI thus:

:1,105! Perlscript

My aim is to convert a text file I’m editing in VI into code that can be read by an ancient market research software called Quantum Without doing a lot of manual editing. (Yes, I am ashamed to admit I am not a real programmer at all, but perhaps if I learn enough doing this, I might grow up to be one someday.)

I’m using split to break the lines up into fields I can rearrange into what I need, but I’ve got “junk” in some of my fields. My questions:

1. Is there a function that allows one to take the first character off a scalar variable in the same way you’d take the last character off with chop ? For example, can I make the string -3 become just 3?

2. Is it possible to look at a scalar variable containing a string, chop off all text after, say, a question mark, and make that be the new value of the scalar, thus:

$qtext = “question text? Lots of junk after that I don’t need”

becomes

$qtext = “question text?”

It might help to see what script I'm trying to incorporate the above ideas into, so here it is:

#!/usr/bin/perl while (<>){ if (/^[SCQD]/){ ($qnum,$v,$qtext) = split("\t"); chop ($qnum); print "L $qnum\nttl$qnum. $qtext\nn10TOTAL BASE\n"; } else { ($stext,$qspec,$x) = split("[\t]"); chomp ($qspec); unless (/^$/){ print "n01$stext;c=c{$qnum}'$qspec'\n"} } }

Am I just trying to do too much here, editing line by line? Is there a better but still simple way to structure this?

Any nudges in the right direction appreciated.

NovMonk

Edit by tye to remove lots of trailing space in CODE


In reply to 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.