Ronald J Kimball wrote:
>
> <> 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
>
Great lesson! And so quick! I will stick to the second solution, which
is more elegant ...
Greetings from Munich
marek
--~--~---------~--~----~------------~-------~--~----~
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.
-~----------~----~----~----~------~----~------~--~---