in reply to Finding a extention
A second way is to use substr and index:$ext="i_am_the_walrus.txt"; $ext=~s/^[^.]*(\.[\w]+$)/$1/; print $ext;
The third, and probably nicest way, is to use File::Basename:use strict; my $filename="you_are_the_whale.doc"; my $separator="."; unless (($where=index($filename,$seperator) == -1) { my $ext=substr($filename,$where); print $ext; } else { print "No extensions in the zoo.\n"; }
Beware that the above assumes you're dealing with simple .xyz extensions. For more complicated stuff, see the regexp in elusion's reply above.use strict; use File::Basename; my $filename="we_are_not_all_mammals.zoo"; my ($base, $directory, $ext)=fileparse($filename, '\.[\w]+$'); print $ext;
CU
Robartes-
Update: Fixed regexp so it actually works :) (thx elusion). Adapted second recipe to include dot.
|
|---|