Eric Lucas on 4 May 2005 13:45:39 -0000


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

Re: Not understanding this behavior


Sorry if my info was insufficient...

@template is just a plain text file with a bunch of text and somewhere in
that text are the three strings "ZCITY" "ZSTATE" and "ZZIP".    

Here is a template file:

Lorem ipsum dolor sit ZCITY, ZSTATE adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis ZCITY ZSTATE, ZZIP in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.

Here is a zip file:


Adamstown,Pennsylvania,19501
Arcola,Pennsylvania,19420
Wynnewood,Pennsylvania,19096
Zieglerville,Pennsylvania,19492

And here is the entire program, not just the fragment.


#!/usr/bin/perl

# open the template and store it in the array
open TEMPLATE, "welcome.template" 
   or die "ERROR: Must have file \"welcome.template\" in current directory\n" ;
@template = <TEMPLATE> ;
close TEMPLATE ;

# now, open the zips file and process each city/zip
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 ;


__END__


The output is four files (in the parent directory!) with different names
but the same city state and zip inside.

HTH.

Thanks
Eric



On Wed, May 04, 2005 at 06:18:52AM -0700, Craig Pearlman wrote:
> What's @template set to?
> 
> -Craig.
> 
> --- Eric Lucas <eric@lucii.org> 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 ;
> > ------------------------------------------------------------
> > 
> > zips.csv:
> > Adamstown,Pennsylvania,19501
> > Arcola,Pennsylvania,19420
> > Ardmore,Pennsylvania,19003
> > Aston,Pennsylvania,19014
> > Atglen,Pennsylvania,19310
> > 
> > 
> > Thanks
> > Eric
> > -- 
> 
> craig@elegant-simplicity.net         cpearlma@yahoo.com
> http://craig.elegant-simplicity.net  http://www.openroleplaying.org
> ----------------------------------------------------------------------
> Y! : cpearlma                        AIM: craigpearlman
> MSN: cpearlma@yahoo.com              ICQ: 211121815
> ----------------------------------------------------------------------
> "Fantasy is hardly an escape from reality. It's a way of understanding
> it."                                                -- Lloyd Alexander


-- 
------------------------------------------------------------------------
#  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**