Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: multiple matches per line *AND* multiple capture groups per match

by hdb (Monsignor)
on Dec 21, 2013 at 18:24 UTC ( #1068048=note: print w/replies, xml ) Need Help??


in reply to multiple matches per line *AND* multiple capture groups per match

In the assignment

(@complete_url, @filename) = ($_ =~ /$test_regexp/g)

all matches will be assigned to the array @complete_url. I would expect that you find the urls and filenames both in the array. So you have everything in one array and need to split it into two afterwards.

Replies are listed 'Best First'.
Re^2: multiple matches per line *AND* multiple capture groups per match
by educated_foo (Vicar) on Dec 21, 2013 at 23:25 UTC
    This. Your solution is fine, but you need to do a bit of postprocessing, e.g.
    if (@groups = /$test_regexp/g) { while (@groups) { ($url, $file) = splice @groups, 0, 2; # ... } }
    or use a loop:
    while (/$test_regexp/g) { ($url, $file) = ($1, $2); # ... }
      Thanks, that also solves my problem.
Re^2: multiple matches per line *AND* multiple capture groups per match
by Special_K (Monk) on Dec 22, 2013 at 01:39 UTC
    Thanks, this answers my original question. So in general, is it not possible to do a multi-array assignment in a single line, i.e.: (@a, @b) = <some_expression>
      "So in general, is it not possible to do a multi-array assignment in a single line, i.e.: (@a, @b) = <some_expression>"

      That's correct with the syntax you're using there; however, you can do it with references. Here's a rather contrived example to demonstrate.

      #!/usr/bin/env perl -l use strict; use warnings; my ($letters, $digits) = get_arrays(); print "Letters REF: $letters"; print "Letters: @$letters"; print for @$letters; print "Digits REF: $digits"; print "Digits: @$digits"; print for @$digits; sub get_arrays { my @three_letters = qw{A B C}; my @three_digits = qw{1 2 3}; return (\@three_letters, \@three_digits); }

      Output:

      Letters REF: ARRAY(0x7ff684047ad0) Letters: A B C A B C Digits REF: ARRAY(0x7ff684047938) Digits: 1 2 3 1 2 3

      If you're unfamiliar with references, a good place to start is "perlreftut - Mark's very short tutorial about references". In the "The Rest" section, you'll find links to more detailed documentation on this topic.

      -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2021-12-03 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (29 votes). Check out past polls.

    Notices?