On 19/11/2011 18:21, Mohan L wrote:
Dear all,

#!/usr/bin/env perl
#dummy.pl
use strict;
use warnings;
use Data::Dumper;

my $ref_to_AoA = [
         [ "fred", "barney",undef,"pebbles", "bambam", "dino", ],
         [ "homer",undef,"bart",undef, "marge", "maggie", ],
         [ "george", "jane",undef, "elroy",undef,"judy", ],
     ];
     print Dumper $ref_to_AoA;
     print $ref_to_AoA->[0]->[0];


[mohan@localhost ~]$ ./dymmy.pl
$VAR1 = [
           [
             'fred',
             'barney',
             undef,
             'pebbles',
             'bambam',
             'dino'
           ],
           [
             'homer',
             undef,
             'bart',
             undef,
             'marge',
             'maggie'
           ],
           [
             'george',
             'jane',
             undef,
             'elroy',
             undef,
             'judy'
           ]
         ];

To print 'fred' , I can use like : print $ref_to_AoA->[0]->[0];

But What I want is, I want to replace all 'undef' to a string 'foo'.  What
is the efficient way I will replace it (the array is larger then what I am
showing above). Any help and explanation will be appreciated.

If you want just to print 'foo' in place of any undefined element, then
write

  print $ref_to_AoA->[0][0] // 'foo';

If you want to modify the array in-place so that all existing undefined
elements are set to 'foo' then

  foreach my $row (@$ref_to_AoA) {
    $_ //= 'foo' foreach @$row;
  }

HTH,

Rob


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to