modify file contents

Discussion in 'Perl Misc' started by yeti349@yahoo.com, Oct 13, 2005.

  1. Guest

    Hi, I am trying to modify a file by replacing a tag with the contents
    of an array of hashes. I can replace the tag with the first iteration
    only; the other array contents are not getting printed. What logic is
    missing from the following code? Thanks.

    open(FILE, $tmpl) or die "Can't open file: $!\n";
    my @html = <FILE>;
    foreach my $line (@html)
    {
    if($line=~/<!--(.*?)-->/)
    {
    for my $i (0...$#userInfo)
    {
    $line=qq($userInfo[$i]{uid} $userInfo[$i]{name}
    $userInfo[$i]{office}<br>);
    }
    }
    print "$line";
    }
    , Oct 13, 2005
    #1
    1. Advertising

  2. wrote:
    > Hi, I am trying to modify a file by replacing a tag with the contents
    > of an array of hashes. I can replace the tag with the first iteration
    > only; the other array contents are not getting printed. What logic is
    > missing from the following code? Thanks.
    >
    > open(FILE, $tmpl) or die "Can't open file: $!\n";
    > my @html = <FILE>;
    > foreach my $line (@html)
    > {
    > if($line=~/<!--(.*?)-->/)
    > {
    > for my $i (0...$#userInfo)
    > {
    > $line=qq($userInfo[$i]{uid} $userInfo[$i]{name}
    > $userInfo[$i]{office}<br>);
    > }
    > }
    > print "$line";
    > }


    It's more likely that you "can replace the tag with the _last_ iteration
    only", as you over-write the contents of $line with each iteration,
    leaving the last one in $line.
    Better clear $line just before the "for(...)", then write "$line .=
    qq(...);"

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 13, 2005
    #2
    1. Advertising

  3. Guest

    ack! that's it...thanks Josef
    , Oct 13, 2005
    #3
  4. wrote:
    > ack! that's it...thanks Josef
    >


    That's ... what?
    Please quote some context when replying.

    Thank you.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 13, 2005
    #4
  5. Ron Savage Guest

    On Thu, 13 Oct 2005 23:44:27 +1000, wrote:

    Hi yeti

    > Hi, I am trying to modify a file by replacing a tag with the


    You're much better IMHO using HTML::Template for this sort of thing.

    Despite the name, this superb module can be used for all sorts of tag
    replacements.

    For example I use it to generate read me files for my modules, and for
    announcements to comp.lang.perl.announce for those modules.
    Ron Savage, Oct 14, 2005
    #5
  6. Dave Weaver Guest

    On 13 Oct 2005 06:44:27 -0700, <> wrote:
    > Hi, I am trying to modify a file by replacing a tag with the contents
    > of an array of hashes. I can replace the tag with the first iteration
    > only; the other array contents are not getting printed. What logic is
    > missing from the following code? Thanks.
    >
    > open(FILE, $tmpl) or die "Can't open file: $!\n";


    Lexical file handles offer several benefits over the old sort (for
    example, automatic closing of the file when the file handle variable
    goes out of scope):

    open my $file, $tmpl or die ...


    > my @html = <FILE>;
    > foreach my $line (@html)


    There's not much point in loading the whole file into an array, if all
    you're going to do is iterate once over the lines. The larger the
    file, the more memory this consumes.

    It's much better and more scalable to read one line at a time and
    process that before moving on to the next. Replace the above 2 lines
    with:

    while ( my $line = <$file> ) {


    > {
    > if($line=~/<!--(.*?)-->/)
    > {
    > for my $i (0...$#userInfo)
    > {
    > $line=qq($userInfo[$i]{uid} $userInfo[$i]{name}
    > $userInfo[$i]{office}<br>);


    All you're doing here is iterating over the elements of @userInfo, so
    there's no need for an index and array access;

    for my $user ( @userInfo ) {
    $line .= qq($user->{uid} $user->{name} $user->{office}<br>)

    Note also the change to ".=" to fix your problem.

    > }
    > }
    > print "$line";
    > }
    Dave Weaver, Oct 14, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    2
    Views:
    331
    darrel
    Sep 8, 2004
  2. Don Adams
    Replies:
    1
    Views:
    572
    Martin Honnen
    Mar 5, 2004
  3. Venkat
    Replies:
    12
    Views:
    10,521
    Chris \( Val \)
    Jan 8, 2004
  4. Kamarulnizam Rahim
    Replies:
    4
    Views:
    200
    Robert Klemme
    Jan 28, 2011
  5. Replies:
    0
    Views:
    173
Loading...

Share This Page