Thanks, I will give it a try!
"R. Joseph Newton" wrote: > > Jan Eden wrote: > > > Hi, > > > > [EMAIL PROTECTED] wrote: > > > > >I have a text file with a records like: > > > > > >smith, James, Dr., 115 Fourth Street, Chicago, IL, 32012, $20.00: > > >[EMAIL PROTECTED] > > > > > >and another text file with placeholders like: > > > > > >Hello $last_name, $first_name: > > > > > >I want to read the first textfile and update the second one. > > > > > >Any ideas! > > > > This is a very general question. Have you already tried something? Have your read > > an Introduction to Perl (O'Reilly's "Learning Perl" is highly recommended)? > > > > Basically, what you have is a data file and a template file. You should open the > > data file and read the data into a hash (using pattern matching), then merge the > > hash and the template file (replacing your placeholders), putting out a scalar > > variable for each set of data. > > I would do it the other way around. I am guessing here that the OP wants to use the > second file as a tameplate, as you suggest also, but consider the use of a template. > He probably does not want to actually update the original, but to spawn > personalized copies for each customer/recipient record in the data file. > > Since the text of the template is the material that would be repeated each time > through the loop, the text is what he should slurp. Then he can go through the > data-file record by record, making a personalized version from the template for each > one. > > Of course I could be wrong--the OP might really want to modify the template itself. > In that case, he would first have to decide which record he would want to take the > values from > > For the time being, though, I am assuming that he simply misstated the procdure. If > he has the template in memory as a concatenated string, he can substitute in the > proper variables for the place holders with each record > > my $template_string = get_file_contents(); > my $contact = {}; > my $count = 0; > while (<DATA_FILE>) { > chomp; > ($contact->{'last name'}, $contact->{'first name'} ...$conatact->{'current > balance'}) = split /,\s+/, $_; > (my $inerpolated_string = $template_string) =~ s/%(.*?)%/$contact->{$1}/g; > open(OUT_FILE, 'boiler_plate' . ++$count . '.txt' > print OUT_FILE $inerpolated_string; > } > > This should work alright. Took a few rounds to get the typos out, but the following > worked from the command line: > my $template_string = 'Hello, %first name% %last name%, how are you'; > > my $contact = {}; > my $count = 0; > while (<DATA>) { > chomp; > ($contact->{'last name'}, $contact->{'first name'}) = split /,\s+/, $_; > (my $interpolated_string = $template_string) =~ s/%(.*?)%/$contact->{$1}/g; > open(OUT_FILE, '>boiler_plate' . ++$count . '.txt'); > print OUT_FILE $interpolated_string; > } > __DATA__ > Smith, John > Doe, Jack > > The file rendered were: > boiler_plate1.txt > Hello, John Smith, how are you > boiler_plate2.txt > Hello, Jack Doe, how are you > > Joseph > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response>
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>