itshardtogetone wrote:
Hi,

Hello,

Looking at the script below, why does the while function loop
6 times instead of 3 times when @data have only 3 elements and I thought the output should be:- 1 $_ = aaa 1 2 $_ = bbb 2
3 $_ = ccc    3

Thanks

#############################

#!/usr/bin/perl
use strict;
use warnings;

my @data = ('aaa 1','bbb 2','ccc 3');
my $ctr = 0;

print "There are ",scalar @data," elements in \...@data\n";

while (<@data>){
 $ctr ++;
 print "$ctr \$_ = $_\n";
}

$ perl -MO=Deparse -le'
my @data = ( "aaa 1", "bbb 2", "ccc 3" );
my $ctr = 0;

print "There are ", scalar @data, " elements in \...@data";

while ( <@data> ) {
    $ctr++;
    print "$ctr \$_ = $_";
    }
'
BEGIN { $/ = "\n"; $\ = "\n"; }
my(@data) = ('aaa 1', 'bbb 2', 'ccc 3');
my $ctr = 0;
print 'There are ', scalar @data, ' elements in @data';
use File::Glob ();
while (defined($_ = glob(join($", @data)))) {
    ++$ctr;
    print "$ctr \$_ = $_";
}
-e syntax OK


So the array @data is first converted to the string 'aaa 1 bbb 2 ccc 3' and then each non-whitespace element is assigned to $_ in turn.




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to