modify file contents

Y

yeti349

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";
}
 
J

Josef Moellers

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
 
R

Ron Savage

On Thu, 13 Oct 2005 23:44:27 +1000, (e-mail address removed) 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.
 
D

Dave Weaver

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:

{
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top