develooper Front page | perl.perl5.porters | Postings from January 2001

[ID 20010130.002] pointer in pattern matching

Thread Next
From:
sebastien
Date:
January 30, 2001 00:10
Subject:
[ID 20010130.002] pointer in pattern matching
Message ID:
200101300810.f0U8A3T32345@chthon.perl.com
Here is a small script :
<snip>
#!/usr/bin/perl

$str = <<EOL;
<name>A12345</name>
<ip>B444444</ip>
<ip1>B55555</ip1>
EOL

$data{'name'} = $1 if ($str =~ /\<name\>(.*?)\<\/name\>/gs);
$data{'ip'} = $1 if ($str =~ /\<ip\>(.*?)\<\/ip\>/gs);
$data{'ip1'} = $1 if ($str =~ /\<ip1\>(.*?)\<\/ip1\>/gs);
foreach (keys %data) {
	print "$_ => $data{$_}\n";
}
</snip>

It gives the following output :
ip => B444444
ip1 => B55555
name => A12345

If I change the code and switch the 2 following lines :
$data{'ip'} = $1 if ($str =~ /\<ip\>(.*?)\<\/ip\>/gs);
$data{'name'} = $1 if ($str =~ /\<name\>(.*?)\<\/name\>/gs);

giving the following code :
<snip>
#!/usr/bin/perl

$str = <<EOL;
<name>A12345</name>
<ip>B444444</ip>
<ip1>B55555</ip1>
EOL

$data{'ip'} = $1 if ($str =~ /\<ip\>(.*?)\<\/ip\>/gs);
$data{'name'} = $1 if ($str =~ /\<name\>(.*?)\<\/name\>/gs);
$data{'ip1'} = $1 if ($str =~ /\<ip1\>(.*?)\<\/ip1\>/gs);
foreach (keys %data) {
	print "$_ => $data{$_}\n";
}
</snip>

here's the new result :

ip => B444444
ip1 => B55555

which makes me think that one of the pointers used by the pattern matching
doesn't go back at the beginning of the string after each occurrence.

Hope this report will help

Bye
Sebastien Delneste
Better Access n.v.


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About