Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Using grep in a scalar context

by Anonymous Monk
on Feb 07, 2013 at 10:35 UTC ( [id://1017609]=note: print w/replies, xml ) Need Help??


in reply to Re: Using grep in a scalar context
in thread Using grep in a scalar context

That expansive map statement is rather extravagant, given ...

my $count = scalar map { /AA/g } @dipept;

... would produce the same result. Am I missing something?

Replies are listed 'Best First'.
Re^3: Using grep in a scalar context (brains)
by Anonymous Monk on Feb 07, 2013 at 12:14 UTC

    is rather extravagant ... would produce the same result. Am I missing something?

    Not really, although, a clarity argument could be made

    map is mostly used for generating lists, so sometimes it feels like the wrong tool for counting ( is the use of map in a void context deprecated ? , What's wrong with using grep or map in a void context? )

    tobyink improves clarity by generating a list of counts and adding/summing them (makes an array of matches, scalar array is count), map made a list, feels good :)

    you rely on generating a list of matches ( m//g) , and that map in scalar context returns a count

    Is there a performance advantage? Penalty? Was map-in-scalar as expensive as map-in-void (before perlv5.8.1)?
    It doesn't really matter as the reason for using map over foreach is clarity/brevity/tradition. The basic intent echoed in all the manuals and books, map for transforming lists, grep for filtering lists, foreach(for) for counting (iterating).

    Compare the "three"

    my @dipept = qw( AABBCC AABBAA ); use v5.12; use List::Util qw(sum); my $count1 = sum map { scalar(my @r = /AA/g) } @dipept; my $count2 = scalar map { /AA/g } @dipept; my $count3 = 0; $count3 += /AA/g for @dipept; my $count5 = 0; map { $count5 += /AA/g } @dipept; my $count6 = 0; grep { $count6 += /AA/g } @dipept;

    If you're thinking in terms of map and grep, thinking perlishly, then you write it like it makes the most sense to you, feels most natural, reads instantly and effortlessly (reads like breathing), needs no thought ...

    so what you're missing, is tobyink's brains

Re^3: Using grep in a scalar context
by tobyink (Canon) on Feb 07, 2013 at 12:44 UTC

    I simply didn't think about doing it that way. I could probably count the number of times I've used map in a scalar context on my fingers.

    That said, given that the original question was about gene sequences, which can be pretty long strings, it seems preferable to avoid the creation of a temporary array containing all matches. Whether that's an important concern or not depends on many factors (typical number of matches expected; typical length of matched strings; etc).

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-03-19 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found