[posted & mailed]
On 5 Aug 2003, Brian wrote:
>I am stumped here - Can someone look at my code and tell me why my
>$name variable is being changed to what is stored in $_? Thx.
>
>foreach(@name) {
[snip]
> while (<SETI>) {
That's why.
When you do a for-loop on an array like that, the variable you use to
iterate over the array (by default, $_) is *aliased* to the element you're
working on:
@stuff = (1, 2, 3);
for (@stuff) { $_ += 5 }
print "@stuff"; # 6 7 8
That is, changes done to $_ effect the element in the array.
Additionally, when you use a while loop on a filehandle, if you don't
specify a variable, $_ is used. The problem is that it's the SAME $_
you happen to be using to iterate over your array.
while (<FILE>)
# is actually
while (defined($_ = <FILE>))
So, change at least ONE of those loops.
for my $n (@name) { ... }
while (my $line = <FILE>) { ... }
like so.
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
|