in reply to regex/perl question about uninitialized value

#!/usr/bin/perl -w use strict; my $mystring = 'ABC<ID>blahblah</ID>DEF'; if ( $mystring =~ /<ID>(.*?)<\/ID>/) { my $ID = $1; print "$ID\n"; }

Replies are listed 'Best First'.
Re^2: regex/perl question about uninitialized value
by TheBigAmbulance (Acolyte) on Nov 03, 2011 at 14:48 UTC
    thank you pvaldes. That worked great!!
Re^2: regex/perl question about uninitialized value
by pvaldes (Chaplain) on Nov 03, 2011 at 14:52 UTC

    This is an interesting hipothetic situation:

    #!/usr/bin/perl -w use strict; open <ID>, '<', 'textfile.txt'; # a lot of lines later... my $mystring = "ABC<ID>blahblah</ID>DEF"; if ( $mystring =~ /<ID>(.*?)<\/ID>/) { my $ID = $1; print "$ID\n"; # OOOPS.... }

    The moral of the history is... always check the ""

      I've no idea what problem you're describing here. Well, except that
      open <ID>, '<', 'textfile.txt';
      looks very strange. But that doesn't seem related to the line with the OOOPS.

        Oh, well... I see now, I'm stupid

        You're right, I'm over a false problem. The error message I had was for the over ID, extra "<>" stuff... I was thinking that I had another type of problem here, that in fact is not a problem at all to Perl; not unless you wrote:

        my $mystring = "ABC".<ID>."blahblah</ID>DEF";

        That is a very different situation to...

        my $mystring = "ABC<ID>blahblah</ID>DEF";

        So I was in the clouds again (what a zurprize)