Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: Re: Regex on array elements during push

by carric (Beadle)
on Mar 22, 2004 at 21:05 UTC ( [id://338771]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Regex on array elements during push
in thread Regex on array elements during push

That was the ticket!! Works perfectly. My understanding of "map" is a bit weak, so I need to disect what you did.. AWESOME!! Thank you!
  • Comment on Re: Re: Re: Regex on array elements during push

Replies are listed 'Best First'.
Re: Re: Re: Re: Regex on array elements during push
by QM (Parson) on Mar 22, 2004 at 23:12 UTC
    map is a looping construct, which works a lot like grep, except that map returns all elements, while grep only returns those evaluating to true in the given expression/code block. map is typically used to derive new elements from the given list, while grep is typically used to filter the list according to some criterion. map is also similar to foreach.

    For instance, these are equivalent:

    my @x; foreach ( 0..9 ) { push @x, $_ }; my @x; @x = grep 1, 0..9; my @x; @x = map $_, 0..9;
    When map is used for good, it makes the code easier to read, and the dataflow easier to follow. But it can be abused, just like anything else useful.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re^4: Regex on array elements during push
by Roy Johnson (Monsignor) on Mar 23, 2004 at 14:56 UTC
    I'm glad it works for you, but I should point out a few flaws in the solution I proposed:
    1. The "and" will prevent the value from being returned, if no substitution occurs.
    2. My replacing of "grep" with an "if" within the map causes blank values to be pushed when the /\S/ doesn't match
    I should have just fixed the problem I saw, instead of trying to be clever.
    push @array, map { $_->[1] =~ s/[\s?,]//g; $_->[1]; } grep { $_->[0] =~ /\S/ } @rows;

    The PerlMonk tr/// Advocate

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://338771]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-20 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found