Link to home
Start Free TrialLog in
Avatar of TehJiBuey
TehJiBuey

asked on

Replacement of filename in variable

A filename is stored in the associative array "$fname{$key}". For eg, either ".gif or .jpg" file.
Then how do i extract only this ".gif or .jpg out" from the variable??
This is because I want to replace the filename wif another name(.gif or .jpg).
Avatar of Kenny
Kenny
Flag of Malaysia image

Try this :
  ($Name, $Ext) = split (/./, $fname{$key});

# The file name is stored in $Name and the extension is stored in $Ext.
# if you want to join it again,

$FileName = join('.',($Name,$Ext));

Hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of maxkir
maxkir
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of olthoff
olthoff

($Ext) = $fname{$Key} =~ m/\.(.*)$/;
Avatar of ozo
File::Basename
This module will allow for more than 1 dot in the file name

use File::Basename;
($name,$path,$suffix) = fileparse($fullname,'\..*');

If you want to limit the type of extentsions
($name,$path,$suffix) = fileparse($fullname,('\.jpg','\.gif'));

Avatar of TehJiBuey

ASKER

thank u so much for all ur help!!