Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by MissPerl (Sexton)
on Oct 18, 2018 at 14:58 UTC ( [id://1224241]=note: print w/replies, xml ) Need Help??


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

thank you for the help !

I'll have a look ! that's very nice.

However, my priority now is to understand what is wrong with my code, which I still cant figure out till now.. :(
  • Comment on Re^2: Loop through all directory and files and lines

Replies are listed 'Best First'.
Re^3: Loop through all directory and files and lines
by poj (Abbot) on Oct 18, 2018 at 15:36 UTC
    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
Re^3: Loop through all directory and files and lines
by Anonymous Monk on Oct 19, 2018 at 00:18 UTC

    The problem is your code contains no subroutines , yet contains four loops -- it should contain three subroutines minimum

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found