NovMonk has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using perl interactively with VI
by TomDLux (Vicar) on Mar 11, 2004 at 16:54 UTC | |
by pbeckingham (Parson) on Mar 11, 2004 at 21:36 UTC | |
|
Re: Using perl interactively with VI
by jeffa (Bishop) on Mar 11, 2004 at 16:57 UTC | |
by ambrus (Abbot) on Mar 14, 2004 at 13:14 UTC | |
by jeffa (Bishop) on Mar 15, 2004 at 19:33 UTC | |
|
Re: Using perl interactively with VI
by ambrus (Abbot) on Mar 11, 2004 at 18:01 UTC | |
|
Re: Using perl interactively with VI
by NovMonk (Chaplain) on Mar 11, 2004 at 21:36 UTC |