R. Geoffrey Avery on 4 May 2005 13:34:31 -0000

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Not understanding this behavior

Did you set @template just once at some point before the block of code you sent?

If so it looks like you made the substitutions to @template for the first file, and then continued to use it for each other the others although you have already replaced ZCITY, ZSTATE, and ZZIP. So for all files other than the first one no substitutions are happening.

This might fix it...

     my $line = join ("\n", @template);
     $line =~ s/ZCITY/$city/sg ;
     $line =~ s/ZSTATE/$state/sg ;
     $line =~ s/ZZIP/$zip/sg ;
     printf OF $line ;

Now that it is all in one line you need the /s to get around the newlines and /g to get all of the changes.

- Geoff

PS. You can also get more interesting messages from die if you do not end the message with "\n".

At 09:01 AM 5/4/2005, you wrote:
I've written a short perl program for a friend to substitute
city, state, and zip in a template file and write each file out
with a new name.

My problem is that each file contains the city, state, and zip
of the first row in the zips.csv file even though the output file
name changes.

Anybody have an idea why the $city $state or $zip appear to not be
re-evaluated in the s///?  I thought I had to add a trailing "o" to
force the PATTERN or REPLACEMENT regular expression to compile only
once.  Looks to me like I get that behavior by default.  I've read
and re-read pages 72-74 of "camel" but don't see what my error is.

Here is the relevant section...
open ZIPS, "zips.csv" or die "ERROR: Must have file \"zips.csv\" in current directory!\n" ;
while (<ZIPS>) {
chomp ;
($city,$state,$zip) = split (/,/) ;
$outfilename = "welcome_" . substr($city,0,5) . $zip . ".txt" ;
$outfilename =~ s/ /_/g ;
open OF, ">../$outfilename" or die "ERROR: cannot open ../$outfilename for some reason!\n" ;
foreach $line (@template) {
$line =~ s/ZCITY/$city/ ;
$line =~ s/ZSTATE/$state/ ;
$line =~ s/ZZIP/$zip/ ;
printf OF $line ;
close OF ;
close ZIPS ;


Thanks Eric -- ------------------------------------------------------------------------ # Eric A Lucas | __|__ # "Oh, I have slipped the surly bond | *---oOo---* # of earth and danced the skies on | __|__ __|__ # laughter-silvered wings... | *---oOo---* *---oOo---* # -- John Gillespie Magee Jr. | - **Majordomo list services provided by PANIX <URL:http://www.panix.com>** **To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**

**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**