Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Loop through all directory and files and lines

by poj (Abbot)
on Oct 18, 2018 at 15:36 UTC ( #1224245=note: print w/replies, xml ) Need Help??


in reply to Re^2: Loop through all directory and files and lines
in thread Loop through all directory and files and lines

what is wrong with my code

My guess is you need to refresh @store_array within the @first_directory loop

foreach my $first (@first_directory){ my @store_array = qw (1 2 3 4 5 6); # here while(my ($i,$j) = splice(@store_array,0,2)){ my $second_directory = "$first/$i/$j"; ..

try

#!/usr/bin/perl use strict; use warnings; use File::Glob 'bsd_glob'; my $store_location = '/path/to/file/store.txt'; open my $fh_log, '>', $store_location or die "Fail to open file '$store_location' $!"; my $Dir1 = "/somewhere"; my $Dir2 = "/somewhereelse"; my @first_directory = ( $Dir1, $Dir2 ); foreach my $first (@first_directory){ my @store_array = qw (1 2 3 4 5 6 7 8); while (my ($i,$j) = splice(@store_array,0,2)){ my $second_directory = "$first/$i/$j"; if (-e $second_directory and -d $second_directory){ my @files = bsd_glob("$first/$i/$j/*"); foreach my $file (@files){ my $lines = 0; my $match = 0; open(FILE, "<" , $file) or die "Can't open file '$file': $!"; + while (<FILE>){ if (/TbhODK|octuov|qas_uop/) { ++$match; } ++$lines; } print "$file : $lines lines read. $match matches found\n"; close FILE; print $fh_log "File $file has $match matches\n"; } } else { print $fh_log "Directory $first/$i/$j (fail to exist)\n"; } } } close $fh_log;
poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1224245]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-12-06 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?