New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unwanted clearing of array member when used as name of a file to be Read-opened (reach of EOF ???) #6034
Comments
From Jean-Luc.VERIT@turbomeca.frHello for(@myArray){ ... open FIL,$_ ... read it with while(<FIL>){ ......} } I don't modify the Array with a line of code, and when the loop finishes, all values of @myArray are null strings. I give you an example of this with 6 options : 'A','B', ... , 'F' with Here are the results I got (the sames on the 2 systems) case A @myArray = ( '' , '' , '' ) Comments in Perl script give more informations: Hoping that I don't waste your time; Faithfully #!/bin/Perl -w # Why the content of an array is (partially) cleared when it is used to # It seems that that the fact that the End Of File is reached or not is # This behaviour seems to me very strange.. # This was run on Unix and PC with identical behaviour # Unix: Sun ultra Sparc 5 with Solaris System # It is difficult for me to install new versions as 5.5, or 5.6 # HERE IS THE CODE SHOWING THE PROBLEM $choice = 'A'; %txtChoice =( unless($h = $txtChoice{$choice}){ &litDir(\@A); # reads a list of files according to some specifications open FOU,">toto.txt"; # Prints the content of @A before we use it $h = 'File000'; if($choice eq 'A'){ elsif($choice eq 'C'){ elsif($choice eq 'D'){ elsif($choice eq 'E'){ #=================== reads all files until EndOfFile is reached (as choice A) # Prints the content of @A after it was used ######################################################################## sub litDir{ # finds all files of current directory which own some criteria: |
From @schwern[Jean-Luc.VERIT@turbomeca.fr - Thu Oct 24 06:06:11 2002]:
This is because you have used $_ twice and the second use clears it. my @array = (1,2,3,4); print @array; The @array will be empty. Why? Because while(<FILE>) { } is really while($_ = <FILE>) { } but $_ is aliased to each element of @array. So it gets blown over. The "foreach" modifier is an iterator: it executes the statement once This allows code like this: for (@array) { which is equivalent to: for my The upshot is this: Its not a bug. Don't use $_ in nested loops. Also much of your reading of FIC is doing nothing. while(<FIC>){ chomp; } This reads each line of the file, removes the trailing newline and then |
The RT System itself - Status changed from 'new' to 'open' |
@schwern - Status changed from 'open' to 'resolved' |
From @schwern[schwern - Mon Jul 04 22:20:52 2005]:
PS I belive this behavior was added in 5.004. |
From Jean-Luc.VERIT@turbomeca.frI was convinced that the problem was about "$_". And I made my script But It is said some where in the Camel book (in the second and in the third An other point (may be related to this) : I wrote about 2 or 3 hundred of Nevertheless thanks ----- Original Message -----
|
From @schwernOn Tue, Jul 05, 2005 at 08:05:11AM +0200, Jean-Luc VERIT wrote:
Only for loops auto-localize $_. A while loop has no intrinsic $_ assignment. It may be worthwhile investigating if auto-localizing $_ in a while(<FH>) Anyhow, stick to "foreach my $thing (@things)" for anything but the smallest
Let's just say file handling is showing its age and hacks. -- |
Migrated from rt.perl.org#18073 (status was 'resolved')
Searchable as RT18073$
The text was updated successfully, but these errors were encountered: