Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > $name getting changed to $_

Reply
Thread Tools

$name getting changed to $_

 
 
Brian
Guest
Posts: n/a
 
      08-05-2003
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) {
if ($files[$x] ne "") {

open SETI, $files[$x] or die "Can not open $files[$x] :$!";
print "name after open = @name\n";
while (<SETI>) {
### $name actually changes here - moved it for readability
if ($. eq 39) {
print "Default Input = $_\n";
print "name after if = @name\n";
chomp;

/(\d.) hr (\d.)/;
$Avg_Time[$x] = $1*60+$2;

}
}
$x++;
close SETI;
}
}

Please let me know if you need any more information.
Thx - Brian.
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      08-05-2003
[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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
changed state to up changed state to down FastEthernet LINEPROTO-5-UPDOWN surrealarmada@gmail.com Cisco 3 03-07-2007 06:06 PM
scroll position is changed when style is changed? mxbrunet Javascript 1 11-03-2006 03:40 AM
xmlDocument.Save "&#10;" getting changed changed to "&amp;#10" st@jpa.co.jp ASP .Net 1 10-11-2005 01:30 PM
FTP File Transfer; file contents getting changed ? Sriram Java 14 09-13-2004 10:45 AM
getting notification when file(s) have changed in directory Sam Su Java 0 05-17-2004 05:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57