On Thu, Apr 12, 2007 at 03:57:07PM +0200, Marek Stepanek wrote:
> My actual script is calculating the differences only one time and replacing
> the result to all following lines ... Why?
> undef $/;
> while (<IN>)
> {
> my ($km1_dif, $km2_dif, $stiche_dif, $zschl_dif, $sum_dif);
> if
> (m/[.\d]+\t&\t([&\d]+)\t&\t([&\d]+)\t&\t([\d]+)\t&\t([&\d]+)\t&\t([&\d]+)\t&\t&\t&\t&\t[A-Z]+\t\\\\\n[.\d]+\t&\t([&\d]+)\t&\t([&\d]+)\t&\t([\d]+)\t&\t([&\d]+)\t&\t([&\d]+)\t.+/gi)
> {
> ...
> }
>
> s/TOTAL\t.+/TOTAL\t&\t$km1_dif\t&\t$km2_dif\t&\t$stiche_dif\t&\t$zschl_dif\t
&\t$sum_dif\t/g;
> ...
If you set $/ to undef, then you're asking Perl to read in the entire input
all at once. Therefore, your loop only executes once, your m//g only
matches once, and your s///g replaces every entry with the results
from that one match.
Now, I see you're setting $/ to undef because you need to operate on two
lines at a time. What you want to do then is change how you have set up
the loop, for example:
#!perl
undef $/;
$_ = <IN>;
1 while s{...}{
# calculate the total, and create the string to replace
# everything that was matched
}gie;
print OUT;
__END__
Another, possibly clearer, way would be to read in two lines each time
through the loop:
#!perl
# don't undef $/
while (<IN>) {
$_ .= <IN>; # read in the next line as well
if (m/.../i) {
# calculate the total
s/.../.../;
}
print OUT;
}
__END__
Ronald
--
------------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_script.shtml>
List archives: <http://www.listsearch.com/bbeditscripting.lasso>
To unsubscribe, send mail to: <[EMAIL PROTECTED]>