Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: How can I use $1 in an assigned string

by Wonko the sane (Deacon)
on May 28, 2003 at 16:01 UTC ( #261346=note: print w/replies, xml ) Need Help??

in reply to How can I use $1 in an assigned string

A more robust way to do this would be something like this,
that uses a reusable generic function for the actual replacements.
Especially if your template string is going to be dynamic.

#!/usr/local/bin/perl use strict; my $filter = q{user ([^\s]+) has ([^\n\r]+)$}; my $log_string = q{user bob has logged on}; my $template = q{user $1 has accessed the system and is $2}; if ( $log_string =~ /$filter/ ) { # would be better to use something else rather # than '$1' as a replacement mark my %vars = ( '$1' => $1, '$2' => $2, ); my $t = interpolate( $template, \%vars ); print qq{LINE: [$t]\n}; } sub interpolate { my ( $text, $vars ) = @_; study $text; $text =~ s/\Q$_\E/$vars->{$_}/g for ( keys %{$vars} ); return $text; } # END interpolate

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://261346]
[erix]: they should lean to gnash their teeth a bit instead of writing more ^Re:NN-nodes :)
[erix]: *learn
[erix]: comic relief: how to start a riot and a fortune at the same time
[beech]: :) thats too generous to jdporter, don't start none won't be none -- there is about two decades (rounding up) worth of "ineffective" node titles -- leave the pope alone :p

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-01-17 23:50 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (159 votes). Check out past polls.