Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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"; ..


#!/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;

In reply to Re^3: Loop through all directory and files and lines by poj
in thread Loop through all directory and files and lines by MissPerl

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-12-06 08:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found