On Wed, Aug 19, 2009 at 05:02:18PM +0200, Marek Stepanek wrote:
> I have a question about a perl filter. I have a complicate table with a
> frame around, which consists of a <td with a rowspan until the bottom of
> the table. Each time I insert new rows, I have to count the table rows
> <tr again to adapt the rowspan to my table.
>
> My idea of a perl filter is not working: first I am reading the selected
> table in my file counting the table rows. Second I want to insert the
> number I counted into the rowspan=" ... "
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $rowcount=0;
>
> while (<>) {
> if (/<tr/g) {
> $rowcount++;
> }
> }
>
> while (<>) {
> s/(rowspan=")\d*"/$1$rowcount"/g if $rowcount;
> print;
> }
>
> But apparently this is not working. Probably it is not possible to read
> in two times the lines with while (<>) in Perl???
<> in scalar context simply reads in the next line from the file handle.
Your first loop reads in all the lines, and when you get to the second
loop, that <> has nothing to read.
You can rewind a filehandle back to the beginning with seek:
seek FH, 0, 0;
However, that won't work here, because of the special way that Perl uses
the ARGV filehandle.
Instead, I would suggest just reading in the input and saving it in memory,
either line-by-line:
#!/usr/bin/perl
use strict;
use warnings;
my $rowcount = 0;
my @lines = <>;
foreach (@lines) {
$rowcount++ while /<tr\b/g;
}
foreach (@lines) {
s/(rowspan=")\d*"/$1$rowcount"/g if $rowcount;
print;
}
__END__
... or as a single string:
#!/usr/bin/perl
use strict;
use warnings;
my $rowcount = 0;
local $/;
my $text = <>;
$rowcount++ while $text =~ /<tr\b/g;
$text =~ s/(rowspan=")\d*"/$1$rowcount"/g if $rowcount;
print $text;
__END__
The thing to keep in mind is that the filter doesn't need to read one line
at a time printing as it goes. It just reads some input and produces some
output, in whatever way works.
Ronald
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "BBEdit Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a specific feature request or would like to report a suspected (or
confirmed) problem with the software, please email to "[email protected]"
rather than posting to the group.
-~----------~----~----~----~------~----~------~--~---