"Mohan L" wrote in message news:cadihtmt4rqntknjlgsimpgqv9xuc89dryhtx00ctwbknxwd...@mail.gmail.com...

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.

Thanks & Rg
Mohan L


Well, if Dr Ruud is right, and you shouldn't replace the undefs in the original arrays, then the following program wouldn't be correct. It changes the original array (data).

Chris

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $aref = [
       [ "fred", "barney",undef,"pebbles", "bambam", "dino", ],
       [ "homer",undef,"bart",undef, "marge", "maggie", ],
       [ "george", "jane",undef, "elroy",undef,"judy", ],
   ];

print Dumper $aref;

for my $array (@$aref) {
   for my $index (reverse 0 .. $#$array) {
       ! defined $array->[$index] && splice @$array, $index, 1, 'foo';
   }
}


print Dumper $aref;

***prints


C:\Old_Data\perlp>perl t6.pl
$VAR1 = [
         [
           'fred',
           'barney',
           undef,
           'pebbles',
           'bambam',
           'dino'
         ],
         [
           'homer',
           undef,
           'bart',
           undef,
           'marge',
           'maggie'
         ],
         [
           'george',
           'jane',
           undef,
           'elroy',
           undef,
           'judy'
         ]
       ];
$VAR1 = [
         [
           'fred',
           'barney',
           'foo',
           'pebbles',
           'bambam',
           'dino'
         ],
         [
           'homer',
           'foo',
           'bart',
           'foo',
           'marge',
           'maggie'
         ],
         [
           'george',
           'jane',
           'foo',
           'elroy',
           'foo',
           'judy'
         ]
       ];




--
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