Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching through files in a directory 2

Status
Not open for further replies.

carthoris5

Technical User
Joined
Dec 11, 2006
Messages
2
Location
US
I am trying to write a perl program that will search for specific information in every file in the specified directory.


opendir(a, '.');

@t = readdir(a);

foreach $filename (@t) {

open (infile, '$filename');

while (infile) {

i am then using grep to search for the information.

when I print it out, all it shows is the commas I used to seperate the variables in the print statement.
 
Don't forget your read operator:

Code:
while ([COLOR=red]<[/color]infile[COLOR=red]>[/color]) {

It would also be a good idea if you added some error checking as a reflex whenever you do file opens.

Code:
open (infile, '$filename') or die "open $filename failed: $!";
 
Also ... your open current is not using an interpolated string. Change:

Code:
open (infile, [COLOR=red]'[/color]$filename[COLOR=red]'[/color]);

to

Code:
open (infile, [COLOR=green]"[/color]$filename[COLOR=green]"[/color]) or die "open $filename failed: $!";
 
really, just drop all quotes around scalar variables unless they are really needed, there is no need to use "$variable". This just forces perl to make an un-needed copy of the original scalar.




- Kevin, perl coder unexceptional!
 
The information I'm searching through as a line that reads
"DOI: 02 NOV 06"

I have my program set up to searh for it using the grep function:

@thefile = infile;

@date = grep(/DOI: {9}/, @thefile);

but it retuns 0 results.

can anyone explain this to me?
 
/DOI: {9}/ doesn't make a lot of sense. Are you looking for "DOI:" followed by 9 spaces or "DOI:" followed by a space and
a date string consisting of 9 characters? Maybe your regexp should be more like:

Code:
/DOI: \d{2} [A-Z]{3} \d{2}/

are you looking for the lines that have that pattern or only need to count the number of times the patten is found?

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top