Anees wrote:
Dear friends:

Hello,

I am a perl begineer. I have created an script which prints each 100th
record from the file as shown below:

#!/usr/bin/perl

use warnings;
use strict;

$maxRecords=20000;
$steps=100;

$index=1;
$recCounter=0;
while (<>){

That is short for:

while ( defined( $_ = <> ) ) {

$ perl -MO=Deparse -e'while (<>) {}'
while (defined($_ = <ARGV>)) {
    ();
}
-e syntax OK

    if(defined) {

So this test is superfluous because the while conditional already tests for definedness.

        if (($index % $steps) == 0) {

Perl provides the $. variable so you don't need $index:

          unless ( $. % $steps ) {

            print $_;
            $recCounter++;
        }
        if($recCounter > $maxRecords){ break; }
        $index++;
    }
}

The above script works fine. But when I modify the script as follows:

#!/usr/bin/perl

use warnings;
use strict;

$maxRecords=20000;
$steps=100;

$index=1;
$recCounter=0;
while (<> && defined){

When you add the "&& defined" the assignment to $_ is not done nor the test for definedness.

$ perl -MO=Deparse -e'while (<> && defined) {}'
while (<ARGV> and defined $_) {
    ();
}
-e syntax OK

        if (($index % $steps) == 0) {
            print $_;
            $recCounter++;
        }
        if($recCounter > $maxRecords){ break; }
        $index++;
}


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to