miller has asked for the wisdom of the Perl Monks concerning the following question:

ok here goes i have the following code and $3 is equal to 0001
$_=~ /^' PJOB='(.*)' PSET='(.*)' PJNO='(.*)' WORKDAY='/; $predlnr=$3; $predlnr =~ s/^00//; $jobstation=$jobsetstation{$2}; print "follows $jobstation\#$2.${1}_$predlnr\n\n\n";
if I do the print without changing anything it works but when i try to remove the first 2 digits if fails and the output i get is :
follows #._01
and it should be : follows n00b#foo.baa_01
I hope u can help me

Edited 2003-03-05 by mirod: changed title

Replies are listed 'Best First'.
Re: stupid $1
by diotalevi (Canon) on Mar 04, 2003 at 17:02 UTC

    The numbered variables were clobbered by your use of the substitution regex. You should copy them immediately if you intend to use them. In your usage the $2 and $1 variables values are allowed to contain random garbage. So don't do it that way. Here's an alternate example with a better regex as well. (you were probably encountering bugs from that as well)

    Added You'll want to read Death to Dot Star for some explanations why your original regex which used things like PJOB='.*' is incorrect and buggy.

    /^' PJOB='([^']*)' PSET='([^']*)' PJNO='([^']*)' WORKDAY='/; $pjob = $1; $pset = $2; $pjno = $3; $pjno =~ s/^00//; $jobstation = $jobsetstation{$pset}; print "follows $jobstation\#$pset.${pjob}_$pjno\n\n\n";

    Seeking Green geeks in Minnesota

Re: stupid $1
by pfaut (Priest) on Mar 04, 2003 at 17:03 UTC

    Every time you use regular expression, you reset the variables $1..$9. Try reordering your code so that you get everything out of the first regular expression before executing another one.

    You can also capture these variables by assigning the result of the bind to an array as in (untested):

    my ($job, $set, $predlnr) = $_=~ /^' PJOB='(.*)' PSET='(.*)' PJNO='(.* +)' WORKDAY='/; $predlnr =~ s/^00//; $jobstation=$jobstations{$jset}; print "follows $jobstation\#$set.${job}_$predlnr\n\n\n";
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
        Plus, I can't have a $10?

        Update Sorry, merlyn, to clarify, I meant that the previous poster seems to think you only get a maximum of nine dollar-n variables. --

        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D