Hi Mohan,

On Sat, 19 Nov 2011 23:51:19 +0530
Mohan L <l.mohan...@gmail.com> 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.
> 

Well, the core-only way would be:

[CODE]

foreach my $array_ref (@{$ref_to_AoA})
{
        foreach my $elem (@$array_ref)
        {
                if (!defined($elem))
                {
                        $elem = 'foo';
                }
        }
}

[/CODE]

You may be able to find a CPAN module to make it more concise (maybe
Data::Visitor) - see http://metacpan.org/ and http://search.cpan.org/ .
Otherwise the inner conditional can be replaced with «$elem //= 'foo';» in
perl-5.10.x and above.

Hope it helps.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan

I’d do Windows-- , but this may result in an integer underflow.
    — an Israeli Linuxer.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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