back referencing

Hi. I have some text and I am trying to update two different parts of each line at the same time. I am trying to use what I think is called back referencing and parentheses.
I am getting stuck I think because when I try to use $1 to reference the first one it seems to collide with the second one. I want to increment patternMin and patternMax each by one but it does not seem to work. Can someone suggest how I can change the code to do this?
Thank you.
while (my $line = <FILE>) {
$patternMin = 'mind=(\d{2})';
$patternMax = 'maxd=(\d{2})';
if ($line =~ m/$patternMin/ && $line =~ m/$patternMax/ ) {

$tempMin = "$1";
$tempMin++;

$tempMax = "$1";

Open in new window

onyourmarkAsked:
Who is Participating?
 
wilcoxonCommented:
I think one of these does what you want.  If it does not, please let me know what you want different.

Will mind and maxd will always be in the same order (eg mind before maxd on the line)?  If so, it can be simplified into one statement.  If they will always be in the same order, will they always both be present if one is (eg if mind exists maxd will exist and vice-versa)?  If not, a single line is trickier but still possible.
# to grab the values from the line and increment the variable value
while (my $line = <FILE>) {
    chomp $line; # remove newline
    # extract mind
    if ($line =~ m{mind=(\d{2})}) {
        $tempMin = $1 + 1;
    }
    # extract maxd
    if ($line =~ m{maxd=(\d{2})}) {
        $tempMax = $1 + 1;
    }
    # XXX - do whatever else
}

Open in new window

# to update the actual line
while (my $line = <FILE>) {
    chomp $line; # remove newline
    # update mind to be one greater
    $line =~ s{mind=(\d{2})}{'mind='.($1+1)}e;
    # update maxd to be one greater
    $line =~ s{maxd=(\d{2})}{'maxd='.($1+1)}e;
    # XXX - do whatever else
}

Open in new window

0
 
käµfm³d 👽Commented:
I believe your problem is this line:

if ($line =~ m/$patternMin/ && $line =~ m/$patternMax/ ) {

Open in new window

I don't believe you can store the pattern in a string and pass that string to the regex operator. What your regex is attempting to match is:

$       -  End of line/string
patternMin  -  Literal string

-- AND --

$       -  End of line/string
patternMax  -  Literal string

Open in new window


which, in either case, will fail since you'll never match text after the end-of-string, based on what you constructed.

Here is my take on what you want to do:

while (my $line = <FILE>) {
    if ($line =~ m/(?=.*?maxd=(\d{2}))(?=.*?mind=(\d{2}))/) {

        $tempMin = "$1";
        $tempMin++;

        $tempMax = "$2";

Open in new window


I'm using lookahead [ (?= ... ) ] to find both strings in the same line. I'm still using parentheses to capture data, but now there are two sets of parentheses in the same pattern, so the index of the group increments to match (i.e. I have group 1 [ $1 ] and group 2 [ $2 ]).

And yes, they are called back references  = )
0
 
wilcoxonCommented:
kaufmed is correct on the line that is causing the problem.  However, he is incorrect on his second portion - you can certainly put patterns in variables and use those in the regex (I do it all the time).

I didn't know you could use captures in lookahead (guess I never thought to try it).
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
käµfm³d 👽Commented:
you can certainly put patterns in variables and use those in the regex (I do it all the time).
I'll take your word for it. My Perl-fu is not that strong  = )
0
 
käµfm³d 👽Commented:
Out of curiosity, how does Perl differentiate between the "pattern" being a variable or if you truly mean "end-of-string"?
0
 
wilcoxonCommented:
I believe it only treats it as end-of-string if it is at the end of the pattern (though I could be wrong on that).
0
 
käµfm³d 👽Commented:
So curiosity got the better of me and I decided to consult my Perl ref. book, and it does confirm what wilcoxon said regarding variables inside the match operator. ActivePerl must be broken then, because it sure doesn't like that syntax! Doing this:

$line = "hello123";
$pattern = "(\d+)";

if ($line =~ /$pattern/) {
    print "Value: $1\n";
}

Open in new window

I get nada. However, doing this:

$line = "hello123";
$pattern = "(\d+)";

if ($line =~ /(\d+)/) {
    print "Value: $1\n";
}

Open in new window


I get what I would expect. Curiouser and curiouser...
0
 
onyourmarkAuthor Commented:
THANKS!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.