On Fri, Feb 20, 2009 at 22:56, itshardtogetone
<itshardtoget...@hotmail.com> wrote:
> 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";
> }

Wow, that is really odd.  I read that as the same as

while (glob(@data)) {

but that is obviously not what it is doing.  The construct you are
looking for is the for loop:

for my $element (@data) {
    $ctr++;
    print "$ctr \$element is $element\n";
}

I will try to figure out what is going on in your code,


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
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