in reply to Re^2: Possible bug with array pointer
in thread Possible bug with array pointer

BTW, you do not need the braces to access a simple scalar reference as an array:
foreach my $str (@$arrayref) { print "BEFORE: str = $str\n"; }
will work just as well.
The extra braces are needed when there is a subscript. @{$arrayref->[2]}

Replies are listed 'Best First'.
Re^4: Possible bug with array pointer
by jockel (Beadle) on Jan 11, 2017 at 14:43 UTC

    Thanks. That I actually knew, but in my opinion it's more clear to read when using braces.

    And in the real world case where I was hit by this unknown "oddity" the code looked like this, before the change.

    foreach my $var (@{$self->get('SAMPLEIDS')}) { $var =~ s/_//g; push(@temp_SAMPLEIDS, $var); }

      Hi jockel,

      If you've got Perl v5.14 or later, note that there's also s///r:

      use 5.014; my $sampleids = ['A','_B','C']; my @temp_SAMPLEIDS; foreach my $var (@$sampleids) { push(@temp_SAMPLEIDS, $var =~ s/_//gr); } # OR my @temp2_SAMPLEIDS = map {s/_//gr} @$sampleids; use Data::Dumper; print Dumper($sampleids, \@temp_SAMPLEIDS, \@temp2_SAMPLEIDS); __END__ $VAR1 = [ 'A', '_B', 'C' ]; $VAR2 = [ 'A', 'B', 'C' ]; $VAR3 = [ 'A', 'B', 'C' ];

      Hope this helps,
      -- Hauke D