Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Uer Perl Variable in file

by moritz (Cardinal)
on Aug 06, 2012 at 13:59 UTC ( #985727=note: print w/replies, xml ) Need Help??

in reply to Re: Uer Perl Variable in file
in thread Uer Perl Variable in txt file

Or even

#!/usr/bin/perl use warnings; use strict; my %replace = (name => 'Joe', fun => 'a game'); my $search_for = join '|', map quotemeta, keys %replace; while (<DATA>) { s/\$($search_for)/$replace{$1}/g; print; }

I avoid nested loops where possible.

Replies are listed 'Best First'.
Re^3: Uer Perl Variable in file
by tinita (Parson) on Aug 06, 2012 at 14:24 UTC
    my $search_for = join '|', map quotemeta, keys %replace; ... s/\$($search_for)/$replace{$1}/g;
    when doing things like this and you have two hash keys "foo" and "foobar", either use \b in the regex if possible, or sort the hash keys for length before joining. otherwise $foobar could be replaced with the value of $replace{foo}.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985727]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2017-03-30 04:02 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.