I am new to perl and i am encountering few problems in the search that i am doing for a site that contains hundreds of html files. The following code that i have written actually finds out where the search string appears in all files. This also gives me a count of those files and filenames.
1. I want to sort file by their last modified time. Is using stat(<filename>) the only option. If so how do i sort. If i have to use 2-d arrays how would i do that.
2. I want the result to be displayed on a htm file. How should i go abt it.
Code:-
#!/usr/bin/perl
opendir(DIR,'.');
@files=sort(grep(/htm$/,readdir(DIR)));
closedir(DIR);
$tofind="<*>";
# string to search for
$expr="xyz";
$filecontent="";
$count=0;
$found=0;
$var=" ";
foreach(@files)
{
$icount=0;
@array="";
$filename=$_;
$filecontent="";
open(filename);
@array=<filename>;
close(filename);
foreach(@array)
{
if(m/$tofind/)
{
$_="";
}
s/$var//g;
# print $_;
$filecontent=$filecontent.$_;
$icount=$icount+1;
}
$_=$filecontent;
chomp();
print $_;
if(m/$expr/)
{
print($filename."\n"
$found=$found+1;
}
$count=$count+1;
print ("Internal count for ". $filename." is ".$icount);
}
print "The count is : ".$count."\n";
print "The pattern matched in ". $found." files.\n";
print "The internal count is :".$icount."\n";
Regards
Ganesh
1. I want to sort file by their last modified time. Is using stat(<filename>) the only option. If so how do i sort. If i have to use 2-d arrays how would i do that.
2. I want the result to be displayed on a htm file. How should i go abt it.
Code:-
#!/usr/bin/perl
opendir(DIR,'.');
@files=sort(grep(/htm$/,readdir(DIR)));
closedir(DIR);
$tofind="<*>";
# string to search for
$expr="xyz";
$filecontent="";
$count=0;
$found=0;
$var=" ";
foreach(@files)
{
$icount=0;
@array="";
$filename=$_;
$filecontent="";
open(filename);
@array=<filename>;
close(filename);
foreach(@array)
{
if(m/$tofind/)
{
$_="";
}
s/$var//g;
# print $_;
$filecontent=$filecontent.$_;
$icount=$icount+1;
}
$_=$filecontent;
chomp();
print $_;
if(m/$expr/)
{
print($filename."\n"
$found=$found+1;
}
$count=$count+1;
print ("Internal count for ". $filename." is ".$icount);
}
print "The count is : ".$count."\n";
print "The pattern matched in ". $found." files.\n";
print "The internal count is :".$icount."\n";
Regards
Ganesh