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@reachedhas not been defined (has not been assigned a value), or has been assigned a value ofundef. You either need to detect (and prevent) undefined values at the source, or expect and deal with them later on. Since you havewarningsenabled (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:
In this example,
$x[0]has a value, and$x[2]has a value, but$x[1]does not. When we interpolate@xinto a double-quoted construct, it is expanded as[element 0 (Hello )]<space>[element 1 (undef)]<space>[element 2 (world!)]. Theundefelements 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 seeHello <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_2has fewer elements than the others, or if$num2is 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:
When I run the following one-liner under warnings and diagnostics:
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.