Home: Perl Programming Help: Beginner:
Remove extensiton from existing string



StarkRavingCalm
User

May 16, 2013, 11:11 AM


Views: 984
Remove extensiton from existing string

Hi all -

I am using an existing string for other requirements (i.e. I dont want to change it) but need to filter out the file extension. I see basename as being a very good option for doing so but cannot seem to get it to work.
Does anyone have a suggestion?

Here is the existing string:

Code
my %local_files; 
foreach my $file (</directorypath/fileprefix*>) { $local_files{basename($file)} = (stat($file))[7];
}


FishMonger
Veteran / Moderator

May 16, 2013, 11:52 AM


Views: 982
Re: [StarkRavingCalm] Remove extensiton from existing string

You can use a regex to strip out the extension or the File::Basename module.
http://search.cpan.org/~rjbs/perl-5.16.3/lib/File/Basename.pm


hwnd
User

May 16, 2013, 1:37 PM


Views: 973
Re: [FishMonger] Remove extensiton from existing string

A simple regex to remove the extension:


Code
$file =~ s/\.[^.]+$//;