CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
read all .htm files in dir

 



bass
newbie

Jul 26, 2001, 10:19 PM

Post #1 of 7 (1514 views)
read all .htm files in dir Can't Post

Hi,

How to modify this code so it only processes .htm files and not both .htm/.html files.

# Start some loops that look for files with .html
while($file = readdir(DIR))
{
next if ($file !~ /.htm/);
open(FILE, $file);
$foundone = 0;
$title = "";
while (<FILE>)
{

Thanx...

Bass



mhx
Enthusiast / Moderator

Jul 26, 2001, 10:32 PM

Post #2 of 7 (1512 views)
Re: read all .htm files in dir [In reply to] Can't Post

Just add a $ as end-of-string-anchor:

Code
next if $file !~ /\.htm$/;

And never forget to escape a dot in regexes if you want to match a dot. Wink

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



dsb
stranger

Jul 27, 2001, 9:16 AM

Post #3 of 7 (1500 views)
Re: read all .htm files in dir [In reply to] Can't Post

Marcus,
To be a detail freak, you only need to escape a dot in a regex if you only want to match a literal dot.

Code
$str = "blah.htm"; 
$tf1 = $str =~ m/.htm$/;
$tf2 = $str =~ m/\.htm$/;
print $tf1, "\n";
print $tf2, "\n";

As you can see, both of those regexes are successfull.

Wink

dan Wink


abstracts
Novice

Jul 27, 2001, 9:19 AM

Post #4 of 7 (1500 views)
Re: read all .htm files in dir [In reply to] Can't Post

Hello,

You can use the function glob, or the <glob> to return the files you need:

Code
for(<*.htm>){ 
open FILE, "<$_"....
...
...

Hope this helps,,,

Aziz,,,



abstracts
Novice

Jul 27, 2001, 9:26 AM

Post #5 of 7 (1500 views)
Re: read all .htm files in dir [In reply to] Can't Post

Hello,

I guess the original question was about how to process files ending with .htm rather than .htm/.html. That dot was a literal dot, not the regex any-char dot. So, the correct answer was /\.htm$/ since /.htm$/ succeeds on files such as myhtm and yourhtm.

Aziz,,,



mhx
Enthusiast / Moderator

Jul 27, 2001, 9:37 AM

Post #6 of 7 (1498 views)
Re: read all .htm files in dir [In reply to] Can't Post

Just wanted to cause some more traffic in this thread... ;-)

Code
$str = "shtm"; 
print "Oops!\n" if $str =~ /.htm$/;
print "Hehe!\n" if $str =~ /\.htm$/;

Actually, if you don't escape the dot, you will match every filename that ends on 'htm' and has at least one character before the htm. So that's not too cool if you only want *.htm files.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



dsb
stranger

Jul 27, 2001, 9:46 AM

Post #7 of 7 (1496 views)
Re: read all .htm files in dir [In reply to] Can't Post

I was merely illustrating that you don't always want to escape a dot.

I recognize that this example was a bad one to illustrate with. Sorry all. ;)

dan Wink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives