Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Transfering Varriables

by dvergin (Monsignor)
on Feb 25, 2002 at 23:38 UTC ( [id://147428]=note: print w/replies, xml ) Need Help??


in reply to Transfering Varriables

Use a hash to store the substitution values from the one source. Then just sub 'em in. You don't give any information about the source of the values to use for replacements. So I made some assumptions for this quick demo.
#!/usr/bin/perl -w use strict; my %values; # Pretend we have an input file w/values while (<DATA>) { last if /TEMPLATE/; # needed here to separate demo data my ($key, $val) = split; $values{$key} = $val; } # Pretend we have an input file w/template my $template = join '', <DATA>; # Process the fields for my $key (keys %values) { $template =~ s/%$key%/$values{$key}/g; } # Show result print $template; __DATA__ name Roadrunner job running TEMPLATE Hello Mr. %name%. We hope you are good at %job%.
You will need to adapt this to make it more robust and to make it fit your need but this should give you the general idea.

For a more elegant approach to the substitution, replace entire process fields "for" loop with this: $template =~ s/%(\w+)%/$values{$1}/g;

If you are going to get serious about this, I suggest you look into one of the template modules.

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2024-04-23 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found