Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Using pattern match

by tceng (Novice)
on Aug 10, 2007 at 18:30 UTC ( [id://631864]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-25 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found