in reply to "Use of uninitialized value in pattern match (m//)" warning, doing a grep on 2D array

Works fine for me on 5.34:

#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 1; use Test::NoWarnings; my @matrix = ( [1, 2], [3, 4] ); my $to_keep = 3; my @matrix_filtered = grep { $_->[0] =~ /$to_keep/ } @matrix;

Perhaps your data do actually have uninitialized values. See also How to ask better questions using Test::More and sample data.


🦛

  • Comment on Re: "Use of uninitialized value in pattern match (m//)" warning, doing a grep on 2D array
  • Download Code

Replies are listed 'Best First'.
Re^2: "Use of uninitialized value in pattern match (m//)" warning, doing a grep on 2D array
by soblanc (Acolyte) on Oct 14, 2022 at 14:39 UTC

    Thank you, you must be right, but argh I can't see them... I included elsewhere some "defined" in my script such as "if (defined($variable)..." that work fine, but here I have troubles pointing uninitialized values in my matrix. I suppressed the warning with "no warnings 'uninitialized'", but it's problematic for the other errors I could get.

    I tried : my @matrix_defined = grep {defined} @matrix; with no success.

      Your code as presented in the OP only filters on the first column, so that's the one to use in this pre-filter - if that's the way you want to go with this.

      #!/usr/bin/env perl use strict; use warnings; use Test::More tests => 3; use Test::NoWarnings; my @matrix = ( [1, 2], [undef, 9], [3, 4] ); my $to_keep = 3; my @matrix_defined = grep { defined $_->[0] } @matrix; is $#matrix_defined, 1, '2 rows after prefilter'; my @matrix_filtered = grep { $_->[0] =~ /$to_keep/ } @matrix_defined; is $#matrix_filtered, 0, '1 row after full filter';

      🦛