Updating a text file

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Updating a text file

    I have a text file with a records like:

    smith, James, Dr., 115 Fourth Street, Chicago, IL, 32012, $20.00: [email]james_naismith@aol.com[/email]

    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!

    __________________________________________________ ________________
    New! Unlimited Netscape Internet Service.
    Only $9.95 a month -- Sign up today at [url]http://isp.netscape.com/register[/url]
    Act now to get a personalized email address!

    Netscape. Just the Net You Need.
    Mercedissa1@netscape.net Guest

  2. Similar Questions and Discussions

    1. ILLUST CS UPDATING LEGACY TEXT
      MAC G4 - OS 10.3. Illustrator CS Hi, does anyone know how to fix this problem? If I open a file in Illustrator CS which had previously been created...
    2. Updating a text field in SQL Server 2000
      I am trying to write some code that inserts or updates a text field in my SQL Server 2000 database. The SQL statement is created using a submit...
    3. Error when updating a long text string
      Hi I have a windows form app which uses a webservice to update data using datasets. On one occasion the dataset contains a string value who's...
    4. Updating part of the text in an NTEXT field
      SQL Server Version - 7 Table - Pages Field - Content Hi, I need to update an ip address in an ntext field. I have looked at UPDATEXT, the...
    5. SQL - Updating Empty Text Field is enterring -1
      > "Update set userid = '" & REquest.form("userid") & Response.write this to the screen, instead of executing it, and examine the result.
  3. #2

    Default Re: Updating a text file

    Hi,

    [email]Mercedissa1@netscape.net[/email] wrote:
    >I have a text file with a records like:
    >
    >smith, James, Dr., 115 Fourth Street, Chicago, IL, 32012, $20.00:
    >james_naismith@aol.com
    >
    >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. A very simple solution (onceyou established the hash) could be a subroutine like this:

    my $build_page = sub {
    # takes a parameter hash as its argument
    my %param = ( @_, );
    open TEMPLATEFILE, $templatepath or die "could not open file '$templatepath': $!";
    my $template = join '', <TEMPLATEFILE>;
    # now replace %%placeholders%% in $template with values from %param hash
    $template =~ s{ %% ( .*? ) %% }{ exists( $param{$1} ) ? $param{$1} : ''}gex;
    return $template;
    }

    It is not a good idea to choose strings like $last_name as your placeholders, since such strings are considered variable names in Perl.

    This might not sound too helpful, but if you deliver a piece of code, one of the Perl wizards on this list will certainly help you.

    - Jan
    --
    If all else fails read the instructions. - Donald Knuth
    Jan Eden Guest

  4. #3

    Default Re: Updating a text file

    Jan Eden wrote:
     
    >
    > 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.[/ref]

    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

    R. Guest

  5. #4

    Default Re: Updating a text file

    Thanks, I will give it a try!

    "R. Joseph Newton" wrote: 
    > >
    > > 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.[/ref]
    >
    > 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: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>[/ref]
    Mame Guest

  6. #5

    Default Re: Updating a text file


    R. Joseph Newton wrote:
     
    >>
    >>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.[/ref]
    >
    >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.
    >[/ref]
    You are probably right and that's what I meant. My description was probablya bit misleading. The way I do it is create the hash, slurp the template file in a scalar and produce an output string for each data set in the hash (which can be written to a new file, named after the respective hash key.

    - Jan
    --
    These are my principles and if you don't like them... well, I have others. - Groucho Marx
    Jan Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139