http://qs321.pair.com?node_id=11122014


in reply to Need advice in for perl use as awk replacement

Each successful match sets the numbered capture groups so you are correct in thinking that the second match you perform clobbers the $3 from the first. You are also correct in that storing the capture groups from the first match in some other structure would help. Where you have gone wrong is that you are storing references (see perlreftut) to the capture groups so you are back at square one. Instead you should store the values, eg:

#!/usr/bin/env perl use strict; use warnings; my $string = 'foo bar baz quux'; $string =~ /(\w+) (\w+) (\w+) (\w+)/; print "Numbered groups: $1 $2 $3 $4\n"; my @matches = ($1, $2, $3, $4); print "\@matches has @matches\n"; $3 =~ /^baz/; print "Numbered groups after match 2: $1 $2 $3 $4\n"; print "\@matches after match 2: @matches\n";

🦛