Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Error: Use of uninitialized value $item in concatenation (.) or string at...

by ig (Vicar)
on Jun 10, 2011 at 21:04 UTC ( #909204=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Error: Use of uninitialized value $item in concatenation (.) or string at...
in thread Error: Use of uninitialized value $item in concatenation (.) or string at...

if ($line =~ /^Parsing \[(sent. \d+) len. \d+\]: \[(.+)\] +/) { #$one_match_ref->[1] = $1; ##Multiple in file #$one_match_ref->[2] = $2; #$one_match_ref->[2] =~ s/,//g; my $nocomma_sent = $2; $nocomma_sent =~ s/,//g; push( @{$one_match_ref}, $1, $nocomma_sent); }

This might be the source of one of your uninitialized values.

See perlre for details of the match variables ($1, $2, etc.). The important thing here is that these variables are reset by the next successful match. If your substitution to remove commas finds any, then it is a successful match, after which $1 and $2 are reset to undef. Thus, if $nocomma_sent has any commas in it, when you push $1 onto your array you will be pushing undef.

One way to avoid this is to push $1 before performing the substituion:

if ($line =~ /^Parsing \[(sent. \d+) len. \d+\]: \[(.+)\] +/) { push( @{$one_match_ref}, $1); my $nocomma_sent = $2; $nocomma_sent =~ s/,//g; push( @{$one_match_ref}, $nocomma_sent); }


Comment on Re^3: Error: Use of uninitialized value $item in concatenation (.) or string at...
Select or Download Code
Re^4: Error: Use of uninitialized value $item in concatenation (.) or string at...
by jonc (Beadle) on Jun 11, 2011 at 02:31 UTC

    That did the trick! Thanks a lot! (Too bad I have to restructure this method... still I won't make the same mistake!)

    By the way, don't you mean if it _doesn't_ have any commas in it?

      One thing you will get good at is testing your ideas. Sometimes reading the documentation or posts here give you ideas, but you will not be certain about them until you try them. Try this:

      use strict; use warnings; my $text = 'axa bxb cxc dxd'; print "1 - \$1 = $1, \$2 = $2\n"; $text =~ m/(a\w*) (b\w*)/; print "2 - \$1 = $1, \$2 = $2\n"; my $x = $2; print "3 - \$1 = $1, \$2 = $2\n"; $x =~ s/,//g; print "4 - \$1 = $1, \$2 = $2\n"; $x =~ s/x//g; print "5 - \$1 = $1, \$2 = $2\n";
      By the way, don't you mean if it _doesn't_ have any commas in it?

      If the string had no commas, there would be no successful match in the regex 'search' phase of the substitution, and  $1 and  $2 and any other capture variables would not be undef-ed. See the substitution statement
          $x =~ s/x//g;
      that is the second to the last statement in ig's example code in Re^5: Error: Use of uninitialized value $item in concatenation (.) or string at...: the successful match against  $x causes all capture variables to be set to the values of their corresponding capture groups, and since there are no capture groups, these values are all undef.

        You guys are great. I'll try testing my own ideas before coming here next time....

        Basically, $1 and $2 take the value of inside the parentheses() of the last successful match (ie. the match is true). If there are no parentheses, they take the value of undef. If there is no match they don't get overwritten ...

        Edit credit: AnomalousMonk and ig (not sure if I took the right steps here, can I just delete mine and rely on AnomalousMonk 's?).

        Thanks a lot!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-10-25 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls