in reply to need help to fix "use of uninitialized value..."

What I am trying to achieve is a set of Pairs which is then (in the script that this code fits in) fed into another script which performs some functions on each pairs defined.

I tried:

my $Person1 = defined($1) ? $1 : ''; my $Person2 = defined($2) ? $2 : '';

And the end results were the same.

So then I tried re-writing the regex to this:

if ($Line =~ m/^Pairing Start\s+(\w+)\s+(\w+)/) {

And I go the same results:

Use of uninitialized value within %PairsOf in concatenation (.) or str +ing at ./test_removing_pair_unitialized_value_error.pl line 26, <DATA +> line 14. Use of uninitialized value within %PairsOf in concatenation (.) or str +ing at ./test_removing_pair_unitialized_value_error.pl line 27, <DATA +> line 14. Use of uninitialized value within %PairsOf in concatenation (.) or str +ing at ./test_removing_pair_unitialized_value_error.pl line 27, <DATA +> line 14. Use of uninitialized value within %PairsOf in concatenation (.) or str +ing at ./test_removing_pair_unitialized_value_error.pl line 27, <DATA +> line 14. Use of uninitialized value within %PairsOf in concatenation (.) or str +ing at ./test_removing_pair_unitialized_value_error.pl line 27, <DATA +> line 14.

I also checked the __DATA__ to ensure there were no white spaces at the end of each and every line and I wasn't able to find any..

Any other Idea's?

Replies are listed 'Best First'.
Re^2: need help to fix "use of uninitialized value..."
by Anonymous Monk on Feb 17, 2012 at 00:32 UTC

    I tried: ... And the end results were the same.

    $Person1, $Person2 are not the problem

    The problem is %Pairs and %PairsOf

    $PairsOf{$Person1} is not defined (it is uninitialized), but you assign to it the value of $PairsOf{$Person1} (an uninitialized value)

    Its like writing

    my $foo = undef; my $bar = 'bar'; $foo = $foo . $bar; # UNITIALIZED!!!

    Get it?

      You are confirming what I suspected...And I understand what you are saying... The only problem is that I tried your code and it doesn't seem to give the expected result

      #!/usr/bin/perl use warnings; use strict; my $foo = undef; my $bar = 'bar'; $foo = $foo . $bar; # UNITIALIZED!!! print "foo = <$foo>\n";
      prints: foo = <bar>

      So thanks for the help though, It still help's me understand what is happening.

Re^2: need help to fix "use of uninitialized value..."
by Marshall (Canon) on Feb 17, 2012 at 05:36 UTC
    In Perl >5.10, there is a new operator //=. This checks for "definedness". $Pairs{$Person1}   //= $Person1; the hash value is assigned the value of $Person1 if that hash value wasn't already defined.

    You cannot use an undefined value in a string concatenation. So, $PairsOf{$Person1} //= ""; will define $PairsOf{$Person1} to be a null string if it isn't already defined. If the value is already defined, then this should have no effect. A null string can be used in a concatenation.

    $Pairs{$Person1} //= $Person1; $Pairs{$Person2} //= $Person2; $PairsOf{$Person1} //= ""; $PairsOf{$Person2} //= ""; $PairsOf{$Person1} = $PairsOf{$Person1} . " " . $Person2; $PairsOf{$Person2} = $PairsOf{$Person2} . " " . $Person1;

      Now this fixed my problem, thank you very much!, I now have more homework to do on learning the new features of perl :)

      thanks again!