?
Solved

Reading more than one file

Posted on 2003-02-26
8
Medium Priority
?
194 Views
Last Modified: 2012-05-04
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
Comment
Question by:WesleyWee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 280 total points
ID: 8034884
$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
 

Author Comment

by:WesleyWee
ID: 8134320
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
 
LVL 84

Expert Comment

by:ozo
ID: 8134382
$some_dir=shift;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:WesleyWee
ID: 8141327
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
 
LVL 84

Expert Comment

by:ozo
ID: 8141780
open F,"<$some_dir/$_" or warn "can't open $some_dir/$_: $!";
0
 

Author Comment

by:WesleyWee
ID: 8141917
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
 
LVL 84

Expert Comment

by:ozo
ID: 8150371
@files = grep { /\.ABC$/ && -f "$some_dir/$_" } readdir(DIR);
0
 

Author Comment

by:WesleyWee
ID: 8164499
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question