How to clear $1 's values ?

Discussion in 'Perl Misc' started by sonet, Sep 20, 2005.

  1. sonet

    sonet Guest

    ===========================================================================
    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;
    sonet, Sep 20, 2005
    #1
    1. Advertising

  2. sonet

    Graham Wood Guest

    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
    Graham Wood, Sep 20, 2005
    #2
    1. Advertising

  3. 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\=['|"](.*?)['|"][ |>|\/>]/;
    Brian McCauley, Sep 20, 2005
    #3
  4. 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
    Mark Clements, Sep 20, 2005
    #4
  5. sonet

    Damian James Guest

    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
    Damian James, Sep 21, 2005
    #5
  6. sonet

    Joe Smith Guest

    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
    Joe Smith, Sep 23, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorgen Gustafsson
    Replies:
    4
    Views:
    582
    Jorgen Gustafsson
    Dec 12, 2003
  2. S. Justin Gengo
    Replies:
    2
    Views:
    362
    S. Justin Gengo
    Aug 20, 2003
  3. Richard Dixson
    Replies:
    1
    Views:
    508
    Joe Fallon
    May 18, 2004
  4. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,018
    Mark Fitzpatrick
    Jan 31, 2008
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    957
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page