bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
Fellow monasterians,
I'm a little embarrassed to be this far into monkdom and still not understanding mapping (and grepping for that matter). I'm trying to alter a specific key's value in a AoH ref. I tried mapping a simple array and no problem. But I get the feeling I'm missing something really important as I'm getting zero results with my latter expressions.
#I get this my @array = qw(aaa-aaa bbb-bbb ccc-ccc); my @newarray = map { $_ =~ s/-/_/g } @array; print Dumper(@array); my $AoH_orig = [ { 'title' => 'aaa-aaa', 'name' => 'tom' }, { 'title' => 'bbb-bbb', 'name' => 'kathy' }, { 'title' => 'ccc-ccc', 'name' => 'bill' } ]; # how i would do it "long hand" for ( 0 .. $#$AoH_orig ) { $AoH_orig->[$_]{'title'} =~ s/-/_/g; } #try mapping my $AoH_new = map { $AoH_orig->[$_]{'title'} =~ s/-/_/g } @$AoH_orig; #or my $AoH_new = map { $_->{'title'} =~ s/-/_/g } @$AoH_orig; print Dumper($AoH_orig);
Comments? or can you point me to a good tutorial? Thanks in advance.
Update: I guess I was using map when I should have just been using the good olde foreach. But, hey, I'm a lot closer to getting it with everyone's help. Thanks.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Basic help with mapping
by pg (Canon) on Nov 22, 2005 at 04:57 UTC | |
Re: Basic help with mapping
by sk (Curate) on Nov 22, 2005 at 04:29 UTC | |
Re: Basic help with mapping
by Errto (Vicar) on Nov 22, 2005 at 04:57 UTC | |
Re: Basic help with mapping
by blokhead (Monsignor) on Nov 22, 2005 at 06:15 UTC | |
Re: Basic help with mapping
by steveAZ98 (Monk) on Nov 22, 2005 at 04:33 UTC | |
Re: Basic help with mapping
by NetWallah (Canon) on Nov 22, 2005 at 07:00 UTC | |
Re: Basic help with mapping
by tphyahoo (Vicar) on Nov 22, 2005 at 10:01 UTC | |
Re: Basic help with mapping
by Roy Johnson (Monsignor) on Nov 22, 2005 at 16:02 UTC | |
by bradcathey (Prior) on Nov 22, 2005 at 23:00 UTC |