develooper Front page | perl.beginners | Postings from February 2009

why while loop 6 times when @data have only 3 elements

Thread Next
From:
itshardtogetone
Date:
February 20, 2009 19:55
Subject:
why while loop 6 times when @data have only 3 elements
Message ID:
COL0-DAV35ADE2624A58B9616A8251B7B00@phx.gbl
Hi,
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";
}
Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About