So, if I'm understanding correctly, you need $x to reset to 0 on each iteration of the for loop, is that right? You may actually just need to make better use of the iterator variable that is associated with your for loop.
for my $x ( 1 .. 10 ) { my @keys; open my $file_handle, '<', "$filename$x" or die $!; while( my $line = <$file_handle> ) { if( $x == 1 ) { print $header, "\n"; @keys = split /\t/, $line; } else { # Do something with each line read in. } } }
I'm making some assumptions here: I'm assuming that the reason you're opening the file within the for-loop is so that you can open a different file on each iteration (and I'm specifying that the files are differentiated by a number from 1 through 10). You'll have to modify that as you see fit.
I'm also assuming that you want to retain the contents of @keys through all iterations of the while loop, but allow them to reset upon the next iteration of the for loop.
Update: I see now that you asked a nearly identical question here: Need to reset a variable to zero ??, three days ago. You should probably read perlintro, perlsyn, and perlsub to gain a better understanding and appreciation of Perl's scoping rules for lexical variables. After reading the documents I've linked to, your next questions will be ones that show progress above and beyond your previous questions (unlike the current situation with this question).
Dave
In reply to Re: Reset a vairable inside a for loop inside a while loop
by davido
in thread Reset a vairable inside a for loop inside a while loop
by dkhalfe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |