Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: do $n exist in grep?

by ikegami (Pope)
on May 12, 2021 at 15:50 UTC ( #11132498=note: print w/replies, xml ) Need Help??


in reply to do $n exist in grep?

map { /^..(A|B)./ ? [ $_, $1 ] : () } @a

I got the sense that you didn't want just the captured parts. But if you only need the $1 from each match the above simplifies to

map { /^..(A|B)./ ? $1 : () } @a

but you could also go a step further and simply use

map /^..(A|B)./, @a

Update: Added what's after the break.

Seeking work! You can reach me at ikegami@adaelis.com

Replies are listed 'Best First'.
Re^2: do $n exist in grep?
by Marshall (Canon) on May 12, 2021 at 16:19 UTC
    ikegami you are definitely a "blackbelt".

    I expanded your code to show relevance to the OP's question.
    Your map shows something that took me a long time to figure out,
    how to return "nothing" from a map{}!

    use strict; use warnings; my @x = ("aaBxyz", "..Bxys", "bbxyzzy", "xxAx"); my @result = map { /^..(A|B)./ ? [ $1, $_ ] : () } @x; # @result is a "2-d array", references to "rows of array" # I swapped $1 and $_ because I thought the print out looked better # Of course every map{} can be expressed as a for loop... foreach my $row_ref (@result) { print "@$row_ref\n"; } =PRINTS: B aaBxyz B ..Bxys A xxAx =cut
    I think this code is well past what the OP wanted.

    ADDED: If the OP wants a subset of something, think GREP. If the OP wants to transform an input into something else, think MAP.

      I use a similar idiom in formulating a return responses or external calls, sometimes:

      my ($stdout, $stderr, $ret) = capture { system('foo', ($somearg ? ('--somearg' => $somearg ) : ()), ($otherarg ? ('--otherarg' => $otherarg) : ()), ); };

      ...or...

      return { status => $s, ($optional_field ? (optional_field => $optional_field) : ()), };

      Caveat emptor. The need to do either of these may constitute code smell, but sometimes purity has to give way to get it done-ery.


      Dave

        It's also possible to shorten the lines with
        ('--somearg' => $somearg ) x !! $somearg, ('--otherarg' => $otherarg) x !! $otherarg,

        but only if the left hand side of x doesn't have any side-effects (because unlike the ternary operator, foo() x 0 still calls foo).

        See also PerlX::Maybe.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Interesting:
        return { status => $s, ($optional_field ? (optional_field => $optional_field) : ()), };
        I personally would not make a hash key optional like that.
        I would always return the hash key and then an undef value for that key if it is not valid.
        But as is often said "there is more than one way to do it".
        It could also be that I haven't thought this completely through and haven't considered what
        the calling code looks like and how it processes this returned info.
Re^2: do $n exist in grep?
by ikegami (Pope) on May 12, 2021 at 17:11 UTC

    I got the sense that you didn't want just the captured parts. But if you only need the $1 from each match,

    map { /^..(A|B)./ ? [ $_, $1 ] : () } @a

    simplifies to

    map { /^..(A|B)./ ? $1 : () } @a

    but you could also go a step further and simply use

    map /^..(A|B)./, @a

    (I added this to the original post to show the options in one place.)

    Seeking work! You can reach me at ikegami@adaelis.com

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2021-08-04 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (41 votes). Check out past polls.

    Notices?