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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with extract $1(have 2 same value) 1

Status
Not open for further replies.

PerlElvir

Technical User
Aug 23, 2005
68
Hi all I hope that I'll get quick response ;)

I have 2 var:

$ado = "Hi my name is Elvir and I have a problem";
$elva = " Can anyone help me";


$var = trans_type($ado);
$var1 = $var;
$var1 =~ /my\s*(.*)\s*Elvir/;
$var1 = $1;
print "First:".$var1."\n";


$xvar = trans_type($elva);
$xvar1 = $xvar;
$xvar1 =~ /Can\s*(.*)\s*sure/;
$xvar1 = $1;
print "Seconde:".$nb_jours3."\n";

So in First:var1 I have result:name is
and in Seconde:xvar1 I have result:name is

So whay Seconde:xvar1 take value of First:var1 whan is value in Seconde:xvar1 = 0(because there is no matchis to extract in Seconde:xvar1)
 
Maybe because you didn't clear $1 before the second regex?
If the second regex doesn't match, it doesn't write to $1 and therefore can leave things in it that were there before the match.

Also, you should really try to use the code tags to make things more readable and also use descriptive variable names.
var, var1, xvar and xvar1 are really not descriptive at all.


Trojan.
 
Thx, well Im beginer so Im not realy sure what you talking about ;( Can you change this my code about you talking about and I'll be greatefull. I just need to work ;)
 
$1 is set by the last successful pattern match. Since there's no match for your second pattern, $1 still contains the matched text from the first match. You can use code similar to this and not need to worry about $1 at all.

Code:
my ($var1) = $ado =~ /my\s*(.*)\s*Elvir/;
print "First: " ,($var1 || "Not Defined"), "\n";

my ($xvar1) = $elva =~ /Can\s*(.*)\s*sure/;
print "Second: ",($xvar1 || "Not Defined"), "\n";
 
rharsh

Wow. If a pattern doesn't match, $1..$n don't get cleared? I couldn't believe it but
Code:
use strict;
use warnings;

my $i = "A";
my $j = "B";

$i =~ /(A)/;
print "$1\n";

$j =~ /(A)/;
print "$1\n";
it's true! How rubbish is that? Perhaps someone could explain to me under what circumstances this could possibly be considered helpful behaviour? I know that 'dissing' Larry doesn't go down too well on this forum, but what was he thinking?

Granted, you normally do matching in a conditional context, so it's less of an issue than it might be, but to leave stuff lying around like that is positively criminal - I would have thought that strict would have cleaned this up.

Well, that's my useless perl idiosyncracy learned for today, and a star for rharsh.
 
On the concept of `helpful behaviour': from `perlre':
NOTE: failed matches in Perl do not reset the match variables, which makes easier to write code that tests for a series of more specific cases and remembers the best match.

 
Also there is an efficiency thing there. If you needed every backreference scalar to be cleared for every regex, that would hit performance. The engine would have to clear $1,$2,$3,$4,$5,$6,$7,$8 and $9 every time.
Also, it's very easy to test if a regex matched and take the appropriate action.
You may argue that you think this is a bad idea but at the end of the day, perl is what perl is and you have to accept it's functionality and work with it or around it.
If you have serious concerns about such issues then maybe you should join in with the development of perl6 and voice those opinions.


Trojan.
 
OK, I suppose I asked for it...

Ishnid - thanks for pasting in what I was unable to find in the docs!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top