Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Preventing lines from printing

Reply
Thread Tools

Preventing lines from printing

 
 
Diamond, Mark
Guest
Posts: n/a
 
      08-23-2009
Hello (I posted this in alt.comp.lang.perl but I think it should be here)
....

I am processing multiple text files. There is a lot of pre-processing before
the file-reading commences, then each file gets read (but I don't want to
print any blank line) and then there is a lot of post-processing of each
file. In AWK I had a BEGIN block, no print lines in the middle, and a very
long END block. In Perl, I have

pre-code
while (<>) {
lots of processing of file lines with only one wanted "print"
}
post-processing

but I get a blank line output on any line that I don't actually do an
explicit print for. I thought "perl -n" would give me what I need but it
doesn't (of course) because of the wrapped "while(<>) { }" .

Please ... what should I be doing?

Mark



 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      08-23-2009
Diamond, Mark wrote:
> In Perl, I have
>
> pre-code
> while (<>) {
> lots of processing of file lines with only one wanted "print"
> }
> post-processing
>
> but I get a blank line output on any line that I don't actually do an
> explicit print for. I thought "perl -n" would give me what I need but it
> doesn't (of course) because of the wrapped "while(<>) { }" .


Unless you start perl with option -p the output can only come from
statements in your script. Since you neither posted the exact command
line nor the exact script nobody can help you.

--
Brüder, in die Tonne die Freiheit,
Brüder, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
 
Reply With Quote
 
 
 
 
Diamond, Mark
Guest
Posts: n/a
 
      08-23-2009
Thank you Alexander. I thought I was saving unnecessary space by NOT posting
the code. As it turns out, your comment has led me to the source of the
problem.

Instead of asking myself "how can I suppress printing that I don't want?",
your comment made me look at the problems differently, and ask "how is it
that I am accidentally printing something I didn't intend?" ... and now I
have found my mistake!

Thank you again.

Mark




"Alexander Bartolich" <(E-Mail Removed)> wrote in message
news:h6r5pi$u7e$(E-Mail Removed)...
> Diamond, Mark wrote:
>> In Perl, I have
>>
>> pre-code
>> while (<>) {
>> lots of processing of file lines with only one wanted "print"
>> }
>> post-processing
>>
>> but I get a blank line output on any line that I don't actually do an
>> explicit print for. I thought "perl -n" would give me what I need but it
>> doesn't (of course) because of the wrapped "while(<>) { }" .

>
> Unless you start perl with option -p the output can only come from
> statements in your script. Since you neither posted the exact command
> line nor the exact script nobody can help you.
>
> --
> Brüder, in die Tonne die Freiheit,
> Brüder, ein Stoppschild davor.
> Egal was die Schwarzen Verlangen
> Rufen wir: Ja! Brav im Chor.



 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-23-2009
"Diamond, Mark" <(E-Mail Removed)> wrote:
[,,,]
>pre-code
>while (<>) {
> lots of processing of file lines with only one wanted "print"
>}
>post-processing
>
>but I get a blank line output on any line that I don't actually do an
>explicit print for.[...]


So you are saying Perl is executing a print() without a print() being in
your code? I find that hard to believe.
Could you please provide a minimal(!) sample program that exhibits this
problem and that we can run?

jue
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      08-23-2009
In article <h6qobl$6is$(E-Mail Removed)>,
Diamond, Mark <(E-Mail Removed)> wrote:
>In AWK I had a BEGIN block, no print lines in the middle, and a very
>long END block.


Just to pick a nit: you *could* do the same thing in Perl.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~

075.awk:

#! /usr/bin/awk -f
BEGIN { sum = 0; count = 0; }
{ sum += $2 * 100; ++count; }
END { print "The average of column 2 is this percent: ", sum / count;
}

075.in:

chase .0225
citi .0110
uhcu .03

075.awk < 075.in:

The average of column 2 is this percent: 2.11667

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~

075.pl:

#! /usr/bin/perl -wan
BEGIN { $sum = 0; $count = 0; }
{ $sum += $F[1] * 100; ++$count; }
END { print "The average of column 2 is this percent: ", ($sum / $count), "\n"; }

075.pl < 075.in:

The average of column 2 is this percent: 2.11666666666667

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~

Mind you, I don't recommend this. I always do "use strict;", which
requires me (in general) to declare variables with "my".

But the scope of "my $sum" and "my $count" in any of those blocks ends
with its block, and I don't know how to "my" a variable across a BEGIN
block boundary.

So I think you're right to rewrite a large complicated piece of code
to be more Perly and not rely on "-n -a", and for short pieces of
code, you can just use awk as before.

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      08-23-2009
In article <(E-Mail Removed)>,
Tad J McClellan <(E-Mail Removed)> wrote:
>Tim McDaniel <(E-Mail Removed)> wrote:
>
>> I don't know how to "my" a variable across a BEGIN
>> block boundary.

>
>You can have a file-scoped lexical declared before the BEGIN block:
>
> my($sum, $count);
> BEGIN { $sum = 0; $count = 0; }
>
>Note this excerpt from "Private Variables via my()" in perlsub.pod:
>
> A C<my> has both a compile-time and a run-time effect. At compile
> time, the compiler takes notice of it. The principal usefulness
> of this is to quiet C<use strict 'vars'>


But it interacts non-intuitively with "perl -n". My test program,
transmogrified:

#! /usr/bin/perl -wan
use strict;
my ($sum, $count);
BEGIN { $sum = 0; my $count = 0; }
$sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }

Data:

chase .0225
citi .0110
uhcu .03

Output:

After 1 lines, sum is 2.25
After 1 lines, sum is 1.1
After 1 lines, sum is 3
The average of column 2 is this percent: 2.25/1=2.25

$count staying 1 on each iteration is easy to see. As "man perlrun"
says, the program is effectively

LINE:
while (<>) {
use strict;
my ($sum, $count);
BEGIN { $sum = 0; my $count = 0; }
$sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }

}

that is, $sum and $count are redeclared and zorched on every loop
iteration.

But I don't know why $sum keeps its value from the FIRST iteration
.... maybe $sum being used in BEGIN somehow squirrels away a reference
to that iteration's value of $sum, and that's somehow what END sees,
instead of the last iteration's value of $sum, or $::sum, or sum such?

--
Tim McDaniel, (E-Mail Removed)
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-23-2009
>>>>> "TM" == Tim McDaniel <(E-Mail Removed)> writes:

TM> #! /usr/bin/perl -wan
TM> BEGIN { $sum = 0; $count = 0; }

no need to initialize those to 0 as += won't warn when adding to
undef. same is true for ++ and .= .

now you can declare those vars without the BEGIN

TM> { $sum += $F[1] * 100; ++$count; }
TM> END { print "The average of column 2 is this percent: ", ($sum / $count), "\n"; }

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-23-2009
On Sun, 23 Aug 2009 20:51:45 +0000 (UTC), (E-Mail Removed) (Tim McDaniel) wrote:

>In article <(E-Mail Removed)>,
>Tad J McClellan <(E-Mail Removed)> wrote:
>>Tim McDaniel <(E-Mail Removed)> wrote:
>>
>>> I don't know how to "my" a variable across a BEGIN
>>> block boundary.

>>
>>You can have a file-scoped lexical declared before the BEGIN block:
>>
>> my($sum, $count);
>> BEGIN { $sum = 0; $count = 0; }
>>
>>Note this excerpt from "Private Variables via my()" in perlsub.pod:
>>
>> A C<my> has both a compile-time and a run-time effect. At compile
>> time, the compiler takes notice of it. The principal usefulness
>> of this is to quiet C<use strict 'vars'>

>
>But it interacts non-intuitively with "perl -n". My test program,
>transmogrified:
>
>#! /usr/bin/perl -wan
>use strict;
>my ($sum, $count);
>BEGIN { $sum = 0; my $count = 0; }
>$sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
>END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
>
>Data:
>
>chase .0225
>citi .0110
>uhcu .03
>
>Output:
>
>After 1 lines, sum is 2.25
>After 1 lines, sum is 1.1
>After 1 lines, sum is 3
>The average of column 2 is this percent: 2.25/1=2.25
>
>$count staying 1 on each iteration is easy to see. As "man perlrun"
>says, the program is effectively
>
> LINE:
> while (<>) {
> use strict;
> my ($sum, $count);
> BEGIN { $sum = 0; my $count = 0; }
> $sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
> END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
>
> }
>
>that is, $sum and $count are redeclared and zorched on every loop
>iteration.
>
>But I don't know why $sum keeps its value from the FIRST iteration
>... maybe $sum being used in BEGIN somehow squirrels away a reference
>to that iteration's value of $sum, and that's somehow what END sees,
>instead of the last iteration's value of $sum, or $::sum, or sum such?


BEGIN, END, labels and goto's are crutches that should not be part of
scoped languages with if,then,else blocks.
And now somebody is implementing a := operator ala Pascal.
Perl 6 is going to do // operator...
Its just rediculous and totally out of control.
If Perl could have done ANSI, they would have. Its too much.
-sln

 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      08-24-2009
In article <(E-Mail Removed)>,
Uri Guttman <(E-Mail Removed)> wrote:
>>>>>> "TM" == Tim McDaniel <(E-Mail Removed)> writes:

>
> TM> #! /usr/bin/perl -wan
> TM> BEGIN { $sum = 0; $count = 0; }
>
>no need to initialize those to 0 as += won't warn when adding to
>undef. same is true for ++ and .= .


I like explicit initialization to the correct value, even if the
default works the same way. I don't like touching undef (except with
defined, which doesn't really touch it), even in contexts where it
works.

--
Tim McDaniel, (E-Mail Removed)
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-24-2009
>>>>> "TM" == Tim McDaniel <(E-Mail Removed)> writes:

TM> In article <(E-Mail Removed)>,
TM> Uri Guttman <(E-Mail Removed)> wrote:
>>>>>>> "TM" == Tim McDaniel <(E-Mail Removed)> writes:

>>

TM> #! /usr/bin/perl -wan
TM> BEGIN { $sum = 0; $count = 0; }
>>
>> no need to initialize those to 0 as += won't warn when adding to
>> undef. same is true for ++ and .= .


TM> I like explicit initialization to the correct value, even if the
TM> default works the same way. I don't like touching undef (except with
TM> defined, which doesn't really touch it), even in contexts where it
TM> works.

do you also assign hash/array refs before you add things to them? the
above is the same thing, autovivification. this saves lots of dumb
boring newbie code that does it like other langs, check if initialized
to something and if not initialize, then mung it.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
How to break the lines in printing to html lines? Ciba LO Perl Misc 4 07-08-2006 02:58 AM



Advertisments