I'm wondering if there is a way to search and replace in a scalar in a "for" kind of basis. For an example, say you have an HTML page which gets parsed and printed to the browser, and the HTML page can <include> other HTML pages:
The only way I would know to parse this, is to have a while() loop to see if the page contains an <include> tag in a regexp, and then process its data and then substitute the <include> regexp out one time, so that each <include> tag on the page is checked:
This method seem to be inefficient to me. Sometimes when I'd program this for more complicated regular expressions, the s/// part doesn't seem to work and the program gets caught on an infinite loop. Is there a way to have a "for" kind of loop which would check each occurrence of the regexp and then quit and not cycle back through?
I.E. if I didn't s/// the regexp out, the while loop would loop forever, is there a way for it to loop through the scalar once for each time the regexp matched and then quit?
I tried something like:
But that block was only called once for the first <include> tag.
Thanks.
-------------
Kirsle.net | Kirsle's Programs and Projects
Code:
<html>
<head>
<title>My Website</title>
</head>
<body>
<include file="top.html">
<include file="leftnav.html">
main page content goes here,
oh, and we wanted a web poll!<p>
<include file="poll.html">
<include file="footer.html">
</body>
</html>
The only way I would know to parse this, is to have a while() loop to see if the page contains an <include> tag in a regexp, and then process its data and then substitute the <include> regexp out one time, so that each <include> tag on the page is checked:
Code:
open (PAGE, "index.html");
my @html = <PAGE>;
close (PAGE);
chomp @html;
my $src = join ("\n",@html);
while ($src =~ /<include file="(.+?)">/i) {
my $file = $1;
open (INC, "$file");
my @data = <INC>;
close (INC);
chomp @data;
my $include = join ("\n",@data);
$src =~ s/<include file="(.+?)">/$include/i;
}
This method seem to be inefficient to me. Sometimes when I'd program this for more complicated regular expressions, the s/// part doesn't seem to work and the program gets caught on an infinite loop. Is there a way to have a "for" kind of loop which would check each occurrence of the regexp and then quit and not cycle back through?
I.E. if I didn't s/// the regexp out, the while loop would loop forever, is there a way for it to loop through the scalar once for each time the regexp matched and then quit?
I tried something like:
Code:
for ($src =~ /<include file="(.+?)">/) {
}
But that block was only called once for the first <include> tag.
Thanks.
-------------
Kirsle.net | Kirsle's Programs and Projects