develooper Front page | perl.beginners | Postings from January 2004

Re: How to put a variable value into a text file

Thread Previous
From:
stuart_clemons
Date:
January 25, 2004 13:16
Subject:
Re: How to put a variable value into a text file
Message ID:
OF520E332F.19D16F9C-ON85256E26.0073DB29-85256E26.007541F2@lotus.com
Thanks for the help.  I just got back on-line.  I'll give these a try. 
Thanks again !




"Randy W. Sims" <RandyS@ThePierianSpring.org> 
01/25/2004 03:19 PM

To
Jan Eden <lists@jan-eden.de>
cc
stuart_clemons@us.ibm.com, Perl Lists <beginners@perl.org>
Subject
Re: How to put a variable value into a text file






On 1/25/2004 3:02 PM, Jan Eden wrote:
> Hi Stuart,
> 
> @testarray gets the content of testmessage.txt, which contains the 
string '$name'. You cannot manipulate this string by setting the variable 
$name. You could do:
> 
> @testarray =~ s/\$name/$name/g;
> 
> which will replace the literal string '$name' using your variable's 
content.

or more generally

#!perl

use strict;
use warnings;

my $name = "Randy";

while (my $line = <DATA>) {
   $line =~ s/[^\\](\$\w+)\W/eval $1/eg;
   print $line;
}

__DATA__
My name is $name.

For each line we read in, we look for any occurence of a unescaped 
dollar sign (i.e. not preceeded by a backslash) followed by any 
characters valid in a variable name up to any non-valid character. We 
then substitute that for the current value of that variable within our 
program.

Regards,
Randy.




Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About