Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Conditional Map?

by George_Sherston (Vicar)
on Oct 07, 2002 at 11:26 UTC ( #203288=perlquestion: print w/replies, xml ) Need Help??
George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:

I'm just starting to get to grips with map and wondering what more I can do with it. Is it possible to do conditional mapping? To take the example in map, say I have an array
my @array = qw/ foo bar baz bodkin /;
and I want to make a hash using all but words beginning in 'f' as values,
my %hash2 = (); foreach $_ (@array) { $hash2{getkey($_)} = $_ unless /^f/; }
works, but is there an equivalent usage with map?
my %hash1 = map {getkey($_) => $_ unless /^f/} @array;
doesn't work.

Am I barking up the wrong tree? Which tree would you recommend?

George Sherston

Replies are listed 'Best First'.
Re: Conditional Map?
by broquaint (Abbot) on Oct 07, 2002 at 11:33 UTC
    This sort of situation suits for just fine and doesn't really fit with a sole map as it's really designed towards applying a transform to a list, not iterating over one. You could however achieve your goal using map and grep in conjunction e.g
    my %hash1 = map { getkey($_) => $_ } grep { !/^f/ } @array;

    HTH

    _________
    broquaint

Re: Conditional Map?
by blakem (Monsignor) on Oct 07, 2002 at 11:45 UTC
    To "zero out" an element using map, simply return an empty list for that value...
    my @evens = map { $_%2 ? () : $_ } 1..10;
    So, you'd need to do something like this:
    my %hash = map { /^f/ ? () : (getkey($_) => $_) } @array;

    -Blake

Re: Conditional Map?
by seattlejohn (Deacon) on Oct 07, 2002 at 14:04 UTC
    map's best friend is grep, which returns the subset of list elements matching a condition. So you could start by selecting just the array elements you want: grep {substr($_,0,1) ne 'f'} @array

    This returns a list consisting of qw(bar baz bodkin), which should be just what you need for your map: my %hash1 = map {getkey($_) => $_} grep {substr($_,0,1) ne 'f'} @array;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://203288]
Approved by vagnerr
help
Chatterbox?
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree
[choroba]: Playing in a punk rock band for 20 years... my hearing is quite bad
[Corion]: I still have hopes to turn my godson and his two siblings into a punk band ;)
[Corion]: Their older sister just started piano but has been interested in drumming, which she should be able to start with 8 years or so)
[Discipulus]: ah was Kink of Bongo 1992..
[1nickt]: choroba name of band? youtube link? MySpace link?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-03-24 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (301 votes). Check out past polls.