[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

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).
0
TehJiBuey
Asked:
TehJiBuey
1 Solution
 
KennyIT Application ExecutiveCommented:
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.
0
 
maxkirCommented:

 I'd say:

($Name, $Ext) = split (/\./, $fname{$key});

Note the \ before dot.
0
 
olthoffCommented:
($Ext) = $fname{$Key} =~ m/\.(.*)$/;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ozoCommented:
File::Basename
0
 
Kim RyanIT ConsultantCommented:
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'));

0
 
TehJiBueyAuthor Commented:
thank u so much for all ur help!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now