Gossamer Forum
Home : General : Perl Programming :

$filename =~ Question

Quote Reply
$filename =~ Question
Hi,

I currently have a regex:

my ($name) = $file =~ m,/path/to/directory/images/(.*),;

which looks for a path and file combination that matches $file - then returns the files name. What I want to do is change it to:

my ($filename) = $file =~ m,$MYCONFIG->{ImagePath}/(.*),;

where $MYCONFIG->{ImagePath} contains the exact path as above but stored in the hash.

The problem I'm having is that when using the hash ref the regex returns the complete path and filename that matches, instead of just the filename which it does when you specify the full path normally.

Can anyone tell me where I'm going wrong?

Cheers,
Regan.

Quote Reply
Re: $filename =~ Question In reply to
Please try with:
my ($filename) = $file =~ /\Q$MYCONFIG->{ImagePath}\E\/(.*)/;

Hope with this help

Quote Reply
Re: $filename =~ Question In reply to
Hi Atlas,

Thanks, but that still just returns the full path with filename included.

Cheers,
R.

Quote Reply
Re: $filename =~ Question In reply to
Try:

Code:
my ($filename,$file);
$filename =~ /$CONF->{ImagePath}\/(.*)$/ and $file = $1;
Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: $filename =~ Question In reply to
Code:
[jagerman@jagerman jagerman]$ perl

my $full = "/a/b/c/jag.gif";
my ($name1) = $full =~ m,/a/b/c/(.*),;
my $hash;
$hash->{dir} = "/a/b/c/";
my ($name2) = $full =~ m,$hash->{dir}(.*),;
print "\$name1: $name1, \$name2: $name2\n";
Code:
$name1: jag.gif, $name2: jag.gif
As you can see, it works as expected. It is possible that your hash value has an extra slash in it - so you are in fact doing /a/b/c//file instead of /a/b/c/file.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com