a question about for and foreach

[email protected] (rainman)
Newsgroups perl.beginners
Organization http://groups.google.com
Message-ID <2b11e939-75f7-41c4-b25d-83726b6dbd9b@x35g2000hsb.googlegroups.com>
Hi,

I got a very strange problem about for and foreach.  I don't
understand why.

The following is my program:

===
#!/usr/bin/perl

use strict;

my @array = (1, 2, 3);
print @array;

foreach (@array) {
        print $array[0];
        print $array[1];
        print $array[2];
        open FILE, "<bbb";
        while (<FILE>) {
                print "hello\n";
        }
        close FILE;
}

print @array;
print $array[1];
===

"bbb" just a file contains only one line like "kdfjkdfjlsjfslkf".

My question is, why @array are changed after this foreach loop!!!

If I change it to for loop, then everything is fine. Really don't
understand.

Thanks.

Yours,
Yan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.