I have written some code, and I am not sure what the error is. I am getting the error:
Use of uninitialized value in concatenation (.) or string at mksmksmks.pl line 63
My code is as follows:
for(my $j = 0; $j < $num2; $j++) {
print {$out} "$destination[$j]|$IP_one_1[$j]|$IP_one_2[$j]|$reached[$j]|$IP_two_1[$j]|$IP_two_2[$j]\n";`
}
What it means is that one of the elements of either @destination
, @IP_one_1
, @IP_one_2
, or @reached
has not been defined (has not been assigned a value), or has been assigned a value of undef
. You either need to detect (and prevent) undefined values at the source, or expect and deal with them later on. Since you have warnings
enabled (which is a good thing), Perl is reminding you that your code is trying to concatenate a string where one of the values being concatenated is undefined.
Consider the following example:
perl -wE 'my @x = (); $x[0] = "Hello "; $x[2] = "world!"; say "@x"'
In this example, $x[0]
has a value, and $x[2]
has a value, but $x[1]
does not. When we interpolate @x
into a double-quoted construct, it is expanded as [element 0 (Hello )]<space>[element 1 (undef)]<space>[element 2 (world!)]
. The undef
elements interpolates as an empty string, and spews a warning. And of course by default array interpolation injects a space character between each element. So in the above example we see Hello <interpolation-space>(undef upgrades to empty string here)<interpolation-space>world!
An example of where you might investigate is one or more of the arrays is of a different total size than the others. For example, if @IP_one_2
has fewer elements than the others, or if $num2
is a value greater than the number of elements in any of the arrays.
Place the following near the top of your script and run it again:
use diagnostics;
When I run the following one-liner under warnings and diagnostics:
$ perl -wMdiagnostics -e '$a=$a; print "$a\n"'
I get the following output, and you will get something similar if you add use diagnostics;
... a very helpful tool when you're first learning Perl's warnings.
Use of uninitialized value $a in concatenation (.) or string at -e line 1 (#1)
(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program anid the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With