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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I agree with the other monks, there’s nothing wrong with using map. However, it is possible to get an equivalent result without map by rearranging and adding the /g and /m modifiers to the regex:

my @online_services = `svcs` =~ /^online.*svc:(.*?):/gm;

Note that this works because the binding operator puts its left-hand operand into scalar context; and backticks (or qx//) in scalar context return “a single (potentially multi-line) string” (Quote Like Operators).

My benckmark tests suggest that this approach may also be more efficient:

#! perl use strict; use warnings; use Benchmark 'cmpthese'; use Data::Dump; use experimental 'smartmatch'; my (@x, @y); cmpthese ( 100, { MAP => sub { @x = map { /\d+_(\S*?)\.pl$/ } `ls -l` }, G_MOD => sub { @y = `ls -l` =~ /\d+_(\S*?)\.pl$/gm }, } ); if (@x ~~ @y) { printf "The arrays are equal; each has %d elements\n", scalar @x; } else { print "The arrays are different:"; dd \@x; print "----------\n"; dd \@y; }

Typical output:

13:56 >perl 720_SoPW.pl Rate MAP G_MOD MAP 47.1/s -- -32% G_MOD 68.9/s 46% -- The arrays are equal; each has 619 elements 13:59 >

Note: Although I’m on Windows, I ended up using Cygwin’s ls -l command because `dir \n` gives an error, and `dir` causes the comparison test to crash. I don’t know what’s going on there. :-(

But by all means stick with map if you’re more comfortable with it.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Capturing regex from map by Athanasius
in thread Capturing regex from map by trippledubs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-28 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found