On Jan 9, Parker, Robin said: >foreach @array_of_files >{ > unless (-r $_ && -w $_) > { > print"***Error : Cannot find file $_\n"; > } > OPEN(FILE, $_) or die etc.......
This code doesn't work -- if you're going to write code, please make sure you've checked it. > while (<FILE>) > { > /^\b$word\b/ && $wordfound++; > } > close (FILE); > unless ($wordfound) > { > print"*** Error : word not found in file $_\n"; > } > $wordfound = 0; >} >Now I understand that this won't work 'cause while will kill off foreach's >$_. However, I tried something with a variable called $storage. > >I added it in here... > > $storage = $_; > while(<FILE>) { > } > $_ = $storage; > close (FILE); The more common approach would be: for $file (@list_of_files) { open FILE, $file; while (<FILE>) { ... } close FILE; } That way, $file holds the filename, and $_ holds each line. >My second foreach loop then complains of my using uninitialised values. So >I put in a print "@array_of_files\n"; after the first foreach loop. > >It's empty. > >OK, so while overwrote $_ but why does this delete the contents of >@array_of_files? Because $_ is an alias to the element in the array. Watch: @n = (1 .. 3); for (@n) { $_ *= 2; } print "@n\n"; # 2 4 6 See? Now, when using while (<FILE>), Perl expands that to the more "proper" while (defined($_ = <FILE>)). When the file is done being read, <FILE> returns undef, and the loop stops. That means that $_ has been set to undef, and since $_ is actually the element of the filename array, you've just trashed that element. >How do I stop while messing things up? Something to do with 'local' isn't >it?? It could, but the better approach is to not use $_ in many different environments at once. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]