Hi fellow monks!
CODE EDITED
I want to go into first directory/i/j/*, then read all the files and all the lines inside every file. Eg, /somewhere/1/2/*
Currently the script gave me no error, but the output was surprisingly all from the (else condition).
I couldnt make sure, since there are a lot of files inside it, but there should at least have some cases match the (if ) condition.
Can anyone have a look at the script i wrote and tell me if anything i have overlooked ?
Each file, I want to find if any three of the strings exists, if it does, I'll store its filepath in store_location.
Also the (i, j), was from an array with even number contents, odd number will be i; while even is j.
# !/usr/bin/perl
use strict; use warnings;
use File::Glob 'bsd_glob';
my $store_location = '/path/to/file';
my $Dir1 = "/somewhere";
my $Dir2 = "/somewhereelse";
my @first_directory = ( bsd_glob("$Dir1/"), bsd_glob("$Dir2/") );
my @store_array = qw (1 2 3 4 5 6);
foreach my $first (@first_directory){
while(my ($i,$j) = splice(@store_array,0,2)){
my $second_directory = "$first/$i/$j";
if (-e $second_directory and -d $second_directory){
my @third_directory = bsd_glob("$first/$i/$j/*");
foreach my $file (@third_directory){
open(FILE, "<" , $file) or die "Can't open file '$file
+': $!";
while (<FILE>){
if (($_ =~ /TbhODK/) or ($_ =~ /octuov/) or ($_ =~
+ /qas_uop/))
{
open(my $filehand, '>>', $store_location) or d
+ie "Fail to open file '$store_location' $!";
print $filehand "$first/$i/$j/$file \n";
close $filehand;
}
}
}
close FILE;
}
else{
open(my $filehand, '>>', $store_location) or die "Fail to
+open file '$store_location' $!";
print $filehand "$first/$i/$j (fail to exist)\n";
close $filehand;
}
}
}#first_directory
Kindly let me know if it's unclear,I will improve the sentence.thank you perlmonks !
-
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.