Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How to store matched $1 values one by one to array?

by johngg (Abbot)
on Feb 02, 2013 at 13:40 UTC ( #1016713=note: print w/ replies, xml ) Need Help??


in reply to How to store matched $1 values one by one to array?

Note that another, perhaps safer, way of capturing everything after "agent_id=>" up to but not including a comma is to use use a negated character class ([^,]+) meaning one or more of anything that isn't a comma rather than a non-greedy match of anything (.+?). You can also populate your array, let's call it @agents, in one fell swoop by using a global match (g flag) and a map within which we substitute away the double-quotes.

$ perl -Mstrict -Mwarnings -MData::Dumper -e ' > my $line = > q{xxx:agent_id=>foo,yyy:agent_id=>b"a"r,zzz:agent_id=>"baz",qqq}; > > my @agents = > map { s{"}{}g; $_ } > $line =~ m{ :agent_id=> ( [^,]+ ) }xg; > > print Data::Dumper->Dumpxs( [ \ @agents ], [ qw{ *agents } ] );' @agents = ( 'foo', 'bar', 'baz' ); $

I hope this is helpful.

Cheers,

JohnGG


Comment on Re: How to store matched $1 values one by one to array?
Select or Download Code
Re^2: How to store matched $1 values one by one to array?
by AnomalousMonk (Abbot) on Feb 02, 2013 at 14:01 UTC

    A minor variation using the  \K (5.10+) and  s///r (5.14+) regex operators, and no capture groups. (Note: I use  \x22 instead of  " (double-quote) in the  s///r substitution because the Windoze CLI gets a little freaky over unpaired double-quotes.)

    >perl -wMstrict -MData::Dump -le "my $line = q{xxx:agent_id=>foo,yyy:agent_id=>b\"a\"r,zzz:agent_id=>\"baz\",qqq +}; ;; my @agents = map s{\x22}{}xmsgr, $line =~ m{ :agent_id=> \K [^,]+ }xmsg; ;; dd \@agents; " ["foo", "bar", "baz"]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-12-18 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (61 votes), past polls