in reply to Re^2: Using pattern match
in thread Using pattern match

Thank you for your contribute!
Unfortunately the idea was to be able to process regular expressions with more than one matching patter and be able to print out something that involves those matching patterns.
I think it would help if Perl generates a list whose elements are all the matching patterns in a s/// operation. Ie that list contains $1, $2, $3, and so on, and a foreach can be done on it, so that one can pick up all the defined variables in one go and then substitute them with the method I showed before (s/\$1/$1/g). Is there one such list, please?

Cheers,

tceng

Replies are listed 'Best First'.
Re^4: Using pattern match
by mreece (Friar) on Aug 11, 2007 at 05:50 UTC
    you can capture the list yourself, and then do substitutions from it.
    #!/usr/bin/perl -w my %triggers = ( 'You are (\\w+).' => 'Your status is: $1', ); my $buff = "You are fired!"; foreach my $trigger ( keys ( %triggers ) ) { if ( my @captures = $buff =~ /$trigger/ ) { # $1 == $captures[0], ..etc my $response = $triggers{$trigger}; $response =~ s/\$([0-9]+)/$captures[$1-1]/g; print $response; } } print "\n";
      Splendid!