On Tue, Nov 01, 2011 at 10:26:41PM +0100, John Delacour wrote:
> >What is the purpose of $switch_on here? You set it, but you never do
> >anything with its value, so you might as well just do this:
> >
> >#!/usr/bin/perl
> >use strict;
> >while (<>) {
> > s/td>/th>/g;
> > print;
> >}
> >__END__
>
> You clearly have not followed the thread, and besides you have not
> even read the script if you say I never do anything with the value of
> $switch_on.
>
> If a line contains "<caption>" $switch_on becomes true, so that the
> succeeding line containing "td>"s has these replaced with "th>" and
> the switch is consequently turned off until the next line containing
> "<caption>" is encountered, and so forth.
>
> What you are proposing is of no use to the original poster, as he has
> already pointed out in his reply to Miraz Jordan.
>
> Please read a thread before you repeat suggestions that have already
> been rejected and make false statemnts about a solution that actually
> works.
I did read your program. I can see quite clearly that although your
program sets the value of $switch_on, it never *reads* the value. The
substitution happens whether $switch_on is true or false.
Your program:
#! /usr/bin/perl
use strict;
my $switch_on;
while (<>) {
$switch_on = 1 if /<caption>/;
$switch_on = 0 if s/td>/th>/g;
print;
}
__END__
Input:
<code>
<tr><td>something 1</td><td>something 2</td>É<td>something n</td></tr>
<caption>irrelevant</caption>
<tr><td>something 1</td><td>something 2</td>É<td>something n</td></tr>
<tr><td>something 1</td><td>something 2</td>É<td>something n</td></tr>
</code>
Output:
<code>
<tr><th>something 1</th><th>something 2</th>É<th>something n</th></tr>
<caption>irrelevant</caption>
<tr><th>something 1</th><th>something 2</th>É<th>something n</th></tr>
<tr><th>something 1</th><th>something 2</th>É<th>something n</th></tr>
</code>
Perhaps you meant something like this:
#! /usr/bin/perl
use strict;
my $switch_on;
while (<>) {
$switch_on = 1 if /<caption>/;
$switch_on = 0 if $switch_on && s/td>/th>/g;
print;
}
__END__
You could implement that much more succinctly using Perl's flip-flop
operator:
#! /usr/bin/perl
use strict;
while (<>) {
/<caption>/ .. s/td>/th>/g;
print;
}
Note that I actually tested all of these programs. I suggest you do the
same in the future.
Ronald
--
You received this message because you are subscribed to the
"BBEdit Talk" discussion group on Google Groups.
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 feature request or would like to report a problem,
please email "[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>