MissPerl has asked for the wisdom of the Perl Monks concerning the following question:
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.Kindly let me know if it's unclear,I will improve the sentence.thank you perlmonks !# !/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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Loop through all directory and files and lines
by 1nickt (Canon) on Oct 18, 2018 at 13:32 UTC | |
by MissPerl (Sexton) on Oct 18, 2018 at 14:25 UTC | |
Re: Loop through all directory and files and lines
by zentara (Archbishop) on Oct 18, 2018 at 13:43 UTC | |
by MissPerl (Sexton) on Oct 18, 2018 at 14:58 UTC | |
by poj (Abbot) on Oct 18, 2018 at 15:36 UTC | |
by Anonymous Monk on Oct 19, 2018 at 00:18 UTC | |
Re: Loop through all directory and files and lines
by haukex (Archbishop) on Oct 19, 2018 at 11:14 UTC | |
Re: Loop through all directory and files and lines
by harangzsolt33 (Chaplain) on Oct 19, 2018 at 05:34 UTC |
Back to
Seekers of Perl Wisdom