How to clear $1 's values ?

S

sonet

===========================================================================
use strict;
my $a;
my $from;
my $to;
my $id;
my $node;
$a="<info id='id1' to='test\@se6.program.com.tw' type='get'
from='luke\@se6.program.com.tw'/>";

$a =~/ from\=['|"](.*?)['|"][ |>|\/>]/;
$from=$1;
$a =~/ id\=['|"](.*?)['|"][ |>|\/>]/;
$id=$1;
$a =~/ node\=['|"](.*?)['|"][ |>|\/>]/;
$node=$1;

print $node;
 
G

Graham Wood

sonet said:
===========================================================================
use strict;
my $a;
my $from;
my $to;
my $id;
my $node;
$a="<info id='id1' to='test\@se6.program.com.tw' type='get'
from='luke\@se6.program.com.tw'/>";

$a =~/ from\=['|"](.*?)['|"][ |>|\/>]/;
$from=$1;
$a =~/ id\=['|"](.*?)['|"][ |>|\/>]/;
$id=$1;
$a =~/ node\=['|"](.*?)['|"][ |>|\/>]/;
$node=$1;

print $node;

I'm assuming that your problem is that $node gets set to the id value
because there is nothing matched for the node. I'd suggest using an if
statement before assigning $1.

if($a =~/ node\=['|"](.*?)['|"][ |>|\/>]/){
$node = $1;
}

Graham
 
B

Brian McCauley

Graham said:
sonet said:
$a =~/ node\=['|"](.*?)['|"][ |>|\/>]/;
$node=$1;
I'd suggest using an if statement before assigning $1.

if($a =~/ node\=['|"](.*?)['|"][ |>|\/>]/){
$node = $1;
}

Or, a better idiom IMNSO...

my ($node) = $a =~/ node\=['|"](.*?)['|"][ |>|\/>]/;
 
M

Mark Clements

sonet said:
===========================================================================
use strict;
my $a;
my $from;
my $to;
my $id;
my $node;
$a="<info id='id1' to='test\@se6.program.com.tw' type='get'
from='luke\@se6.program.com.tw'/>";

$a =~/ from\=['|"](.*?)['|"][ |>|\/>]/;
$from=$1;
$a =~/ id\=['|"](.*?)['|"][ |>|\/>]/;
$id=$1;
$a =~/ node\=['|"](.*?)['|"][ |>|\/>]/;
$node=$1;

print $node;

Just a thought (this doesn't answer your question):

If you are trying to parse XML, you are better off using a parser.

XML::Simple may be adequate for your needs, if not try XML::LibXML or
similar.

regards,

Mark
 
J

Joe Smith

sonet said:
Question: How to clear $1 's values ?

Answer: Don't use $1 when the previous regex did not match.
Use 'if(){}' or 'and' to avoid the non-matches.
$a =~/ from\=['|"](.*?)['|"][ |>|\/>]/;
$from=$1;
$a =~/ id\=['|"](.*?)['|"][ |>|\/>]/;
$id=$1;
$a =~/ node\=['|"](.*?)['|"][ |>|\/>]/;
$node=$1;

$a =~/ from=['|"](.*?)['|"][ |>|\/>]/ and $from=$1;
$a =~/ id=['|"](.*?)['|"][ |>|\/>]/ and $id=$1;
$a =~/ node=['|"](.*?)['|"][ |>|\/>]/ and $node=$1;

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top