Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to clear $1 's values ? (http://www.velocityreviews.com/forums/t894325-how-to-clear-1-s-values.html)

sonet 09-20-2005 08:20 AM

How to clear $1 's values ?
 


================================================== =========================
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;




Graham Wood 09-20-2005 09:32 AM

Re: How to clear $1 's values ?
 
sonet wrote:
> ================================================== =========================
> 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


Brian McCauley 09-20-2005 11:12 AM

Re: How to clear $1 's values ?
 


Graham Wood wrote:
> sonet wrote:
>
>> $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\=['|"](.*?)['|"][ |>|\/>]/;


Mark Clements 09-20-2005 11:42 AM

Re: How to clear $1 's values ?
 
sonet wrote:
> ================================================== =========================
> 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

Damian James 09-21-2005 06:42 AM

Re: How to clear $1 's values ?
 
On Tue, 20 Sep 2005 12:12:46 +0100, Brian McCauley said:
> Graham Wood wrote:
>> if($a =~/ node\=['|"](.*?)['|"][ |>|\/>]/){
>> $node = $1;
>> }

>
> Or, a better idiom IMNSO...

^
You're dropping your aitches :-).

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


or indeed:

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

--Damian

Joe Smith 09-23-2005 05:30 AM

Re: How to clear $1 's values ?
 
sonet wrote:
>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


All times are GMT. The time now is 07:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.