[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Reading more than one file

Hi,

how do i program the code to read a whole directory of several random filenames for processing. (The file is text)
----------------------------
#!/usr/bin/perl
use strict;

open(FILE, "file.txt");

if(<FILE>)
{
 print <FILE>;
}

close FILE;
----------------------------
This is the simple code that i made

I actually need to calculate the number of a certain character in the file, i will ask more again

Thanks in advance!
0
WesleyWee
Asked:
WesleyWee
  • 4
  • 4
1 Solution
 
ozoCommented:
$some_dir='.';
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@files = grep {-f "$some_dir/$_" } readdir(DIR);
for( @files ){
    print "$_\n";
    open F,"<$_" or warn "can't open $_: $!";
    $count = 0;
    while( <F> ){
        print;
        $count += tr/X//;
    }
    print "number of Xs: $count\n";
}
0
 
WesleyWeeAuthor Commented:
Thank you very much OZO.

I'll like to know more like how can i enter a path name by myself?

eg:

C:\test.pl \mydocu~1\testing\


0
 
ozoCommented:
$some_dir=shift;
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
WesleyWeeAuthor Commented:
OZO thanks now it accepts the path i put in

tried opening the files in the folders but it can't open. what should i put in so it can open the file in the folder? It can read the filenames in the folders but can't go into the directory when trying to open it

open F,"<$_" or warn "can't open $_: $!";
0
 
ozoCommented:
open F,"<$some_dir/$_" or warn "can't open $some_dir/$_: $!";
0
 
WesleyWeeAuthor Commented:
oh yes thanks alot it works very well.

i'll like to add that the files that is to be read to be limited to say only *.ABC extention only. how to i go about it?

alot with the *.ABC in the folder there is one file that i with to filter out. filename: "....L1.ABC" how to do ?
0
 
ozoCommented:
@files = grep { /\.ABC$/ && -f "$some_dir/$_" } readdir(DIR);
0
 
WesleyWeeAuthor Commented:
Thank you Ozo. its working fine now

i have another problem. can you help me? it's on the last comment at http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_20553048.html
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now