Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backreferencing Question

Status
Not open for further replies.

zyphon08

Programmer
Joined
Jan 1, 2004
Messages
8
Location
US
I'm fairly new to Perl (I know other languages), but I am extremely new to regular expressions. While writing a script, I ran into a little problem while trying to backreference to a match in a regular expression. The part of the code was similar to the following:

if(/(hello)/)
{
$foo = $1;
if(/(blah)/)
{
$bar = $1;
}
}

print "$foo and $bar";

What I thought before I ran into this problem was that the output would look like..

hello and blah

But I found out that the output is actually (assuming that my problem isn't due to some logical error that I've yet to notice)..

blah and blah

I don't really understand why this happens, and what I think right now is that the backreference variables ($1, $2, $3, etc..) are some sort of special reference variables that automatically dereference themselves when you use them. I'm not even sure if that makes sense, so I was wondering why the above situation occurs and if there is anyway to bypass that problem (without rewriting the regular expression, which I'm sure would be much easier :)

 
Scoping might be an issue here
nowadays, context variables are cheap, and it makes your code easier to read
HTH
--Paul
 
try

my ($foo, $bar);

at the start of your script

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
I already have the variables defined like that. Also, if it is a scoping issue, wouldn't the output more likely be..

hello and hello

?
 
I can't reproduce the behavior. Putting this alone into a file works as expected:
Code:
$_ = 'this is me saying hello, which is really just blah blah blah';
my($foo, $bar);
if(/(hello)/)
{
  $foo = $1;
  if(/(blah)/)
  {
     $bar = $1;
  }
}
print "$foo and $bar";
And if it were a scoping issue, they'd likely take the value of the last assignment. Unless somewhere above you defined both variables to be the same, I don't see how it happened.

Even without strict and my it works. Are $foo and $bar junk variables you use often in the script? Declare once at the beginning and use over and over again? That's a very dangerous practice.

________________________________________
Andrew - Perl Monkey
 
Well, $foor and $bar have specific purposes, but I just tried your snippet of code, and you're right... it works as expected. So I fiddled around with the code I was having problems with, and I got it so $foo and $bar aren't equal at the end anymore (my initial prob). There is still one problem, but I'm pretty sure it has to do with my regular expressions and I think I can figure it out from here. Anyways, thanks for you help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top