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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.