Home > Back-end >  Uninitialized value in join or string and uninitialized value in numeric gt when using perl script
Uninitialized value in join or string and uninitialized value in numeric gt when using perl script

Time:10-31

I have some problems running the code:

enter image description here

When I run the above code it shows the following: enter image description here

Is it an error or not? How to make the code show only the results without the other lines.

CodePudding user response:

Please post actual code samples (see the "code" button above) rather than screen shots. With screen shots anyone who actually wants to run your code has to type it in again, making it that much harder for them to help you.

That said, your messages are, strictly speaking, not errors but warnings. In this case Perl is warning you that you did not initialize subscripts 10-19 of your array @b4. How to suppress the warning depends on whether it is important to you that your array contain uninitialized cells.

My preference would be to eliminate the uninitialized values by replacing $b4[20] = "last"; with push @b4, "last";.

But if you need the array to contain the uninitialized cells, you can suppress the warning by no warnings 'uninitialized';. I recommend enclosing the pragma and the lines that warn in curly brackets to limit the scope of the pragma:

{
    no warnings 'uninitialized';
    print "b4: @b4\n";
    my $z = reduce ...
    print "New max index: $z\n";
}

Because you are requiring at least Perl 5.10 you can replace print ... "\n"; with say ...;.

CodePudding user response:

Please don't post images of code. If we want to help you, it means we need to retype your code rather than copying and pasting.

Luckily, your problem is obvious without needing to run your code.

You create and populate your array, @b4 with these two lines of code:

my @b4 = qw( zero one two three four five six seven eight nine );

$b4[20] = "list";

This creates an array with some strings (in elements 0-9 and 20) and some undefined values (in elements 10-19).

You then display the contents of the array using print(). This accounts for the first ten warnings in your output - as Perl tries to print every element of the array and ten of them contain undef.

You then use the reduce() function on the array and that produces the rest of your warnings - as Perl tries to compare elements using > and many of the elements are undefined.

It's hard to suggest a good fix here without understanding a lot more about what your code is actually trying to do. One idea might be to replace the undefined elements with zeroes.

@b4 = map { $_ // 0 } @b4;

But that might have effects on code that you haven't shown us.

  • Related