Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

uninitialized value $_ in pattern match (m//)

by gvinu4u (Acolyte)
on Jun 23, 2011 at 03:11 UTC ( #911001=perlquestion: print w/ replies, xml ) Need Help??
gvinu4u has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, this is the program that I have and working fine except one issue

my $xml_file = "Modfile.xml"; open my $list, $xml_file or die "Could not open $file: $!"; my $count = 1; while( my $line = <$list> ) { my ( $subfamily ) = $line =~ /suite subfamily="(.*?)"/; print "The Sub Family is $subfamily \n "; if ($subfamily =~/[a-z]/){ my ( $name ) = $line =~ /" name="(.*?)"/; print "The Name is $name \n "; } if ( $name = ~/[a-z]/) { my ( $family ) = $line =~ /" family="(.*?)"/; print "The Family is $family \n "; } last if ++$count == 3; $count ++ ; } close File;
In addition to the expected output I get the below for every iteration, not sure as why I'm getting it.

Use of uninitialized value $subfamily in pattern match (m//) at Test.pl line 34, <$list> line 59.

Use of uninitialized value $_ in pattern match (m//) at Test.pl line 39, <$list> line 59.

Use of uninitialized value $family in concatenation (.) or string at Test.pl line 41, <$list> line 59.

line 34 is-->if ($subfamily =~/a-z/)

line 39 is--> if ( $name = ~/a-z/)

Comment on uninitialized value $_ in pattern match (m//)
Download Code
Re: uninitialized value $_ in pattern match (m//)
by ikegami (Pope) on Jun 23, 2011 at 03:17 UTC

    [ The OP has since been changed ]

    $subfamily = /[a-z]/ $name = /[a-z]/
    is the same as
    $subfamily = $_ =~ /[a-z]/ $name = $_ =~ /[a-z]/

    Did you perhaps mean

    $subfamily =~ /[a-z]/ $name =~ /[a-z]/

    Also, make sure to use use strict;; it'll find another error.

      ok I'll use it, mean while I've just updated the code output again.

      When I use strict, I get error like this which does not seem to be an issue for me but blocks the execution :(

      Global symbol "$file" requires explicit package name at Test.pl line 25.

      Execution of Test.pl aborted due to compilation errors.

        You read a variable named $file, but you never declared such a variable, much less assigned a value to it.

        I hadn't even seen that one! Goes to show how important use strict; is.

Re: uninitialized value $_ in pattern match (m//)
by 7stud (Deacon) on Jun 23, 2011 at 04:27 UTC
    use strict; use warnings; use 5.010; my $line = 'hello world'; my ($subfamily) = $line =~ /goodbye/; #no match, and list context on the left, so m// returns () say "-->$subfamily<--"; if ($subfamily =~ /x/) { say 'yes'; } --output:-- Use of uninitialized value $subfamily in concatenation (.) or string a +t perl.pl line 10. --><-- Use of uninitialized value $subfamily in pattern match (m//) at perl.p +l line 11.
    Whenever you do a match, you need to check whether it succeeded or not. You never checked whether the first $subfamily match was true--like you did with the second match.
      Thanks for replies, The basic thing that I'm intending to do is egt Database Connect - SQL, DBMgrTest and Databases from the below line of XML. <suite subfamily="Database Connect - SQL" name="DBMgrTest" family="Databases"> Any way of using a better regular expression to get all the three values into $1,$2 and $3 please let me know.
Re: uninitialized value $_ in pattern match (m//)
by choroba (Abbot) on Jun 23, 2011 at 08:44 UTC
    if ( $name = ~/[a-z]/)
    is not the same as
    if ( $name =~ /[a-z]/)
Re: uninitialized value $_ in pattern match (m//)
by 7stud (Deacon) on Jun 23, 2011 at 09:04 UTC
    use strict; use warnings; use 5.010; use XML::Simple; use Data::Dumper; my $str = q{ <suite subfamily="Database Connect - SQL" name="DBMgrTest +" family="Databases" /> }; my $ref = XMLin($str); say Dumper($ref); say $ref->{name}; say $ref->{family}; say $ref->{subfamily}; --output:-- $VAR1 = { 'subfamily' => 'Database Connect - SQL', 'name' => 'DBMgrTest', 'family' => 'Databases' }; DBMgrTest Databases Database Connect - SQL
      Thanks for the reply....its wroking but as a learner I wanted to use regex.. especially to extract substrings b/w a line as in this case.
      my $line = '<suite subfamily="Database Connect - SQL" name="DBMgrTest +family="Databases">'; my $pattern = '<suite subfamily="(*.?)+name=(\S+)\s+family=(\S+)'; if ($line =~ /$pattern/)
Re: uninitialized value $_ in pattern match (m//)
by 7stud (Deacon) on Jun 23, 2011 at 18:34 UTC
    use strict; use warnings; use 5.010; my $line = '<suite subfamily="Database Connect - SQL" name="DBMgrTest" + family="Databases">'; my $pattern = q{ <suite \s* subfamily="(.*?)" \s* name="(.*?)" \s* family="(.*?)" }; if ( my ($subfamily, $name, $family) = ($line =~ /$pattern/xms) ) { #Successful match, and the left hand side of the equals sign #is a list, and in list context m// returns a list of the #matches for the parenthesized groups say $subfamily; say $name; say $family; } --output:-- Database Connect - SQL DBMgrTest Databases
    But that pattern is very brittle. What happens if the attributes are in a different order? Or if there are spaces before or after the = sign? Or if there are single quotes instead of double quotes? HTML and XML are hard to parse, so if there is already a well known module like XML::Simple available, learn how to use that instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://911001]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2014-09-18 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls