devlele has asked for the wisdom of the Perl Monks concerning the following question:



Hi Monks,

I have filenames stored in an array such as " c:\perl/lib/XSLoader.pm" I want to store pathnames in a separate array and filenames in separate array. For example: "c:\perl/lib/" in one array and "XSLoader.pm" in separate array. I am currently using "
if ($_ =~ /\/(.*$)/) { print "$1\n"; }
" regular expression but it is matching "/lib/XSLoader.pm". I want only filename. How should I go about it ?

Please Help.

Thanks.

Replies are listed 'Best First'.
Re: pattern matching question
by philcrow (Priest) on Dec 28, 2005 at 20:52 UTC
    You probably want to use the standard module File::Basename (see its perldoc for details).

    Phil

    Update: Removed link to CPAN, it's a standard module.

Re: pattern matching question
by TedPride (Priest) on Dec 28, 2005 at 20:54 UTC
    while (<DATA>) { chomp; m/([^\/\\]*)$/; print "$1\n"; } __DATA__ c:\perl/lib/XSLoader.pm c:\perl/lib/
Re: pattern matching question
by ChrisR (Hermit) on Dec 28, 2005 at 21:02 UTC
    Just for TIMTOWTDI sake:
    use strict; my @pathandfile = qw(c:\dir1/dir2/file1.txt c:\dir1/dir3/file2); for (@pathandfile) { $_ =~ /(.*)(\\|\/)(.*)$/; print "PATH: $1 - FILE: $3\n"; }
      almost the same, but...
      perl -e 'my @pathandfile = qw(c:\d1/d2/f1.txt c:\d1/d3/f2 c:\d1\d2\f4 +c:\d1\d2\d3\f5); for (@pathandfile){print "PATH: $1 - FILE: $2\n" i +f /(.*)[\\|\/]([^\/|^\\]+)$/;}'
Re: pattern matching question
by ysth (Canon) on Dec 29, 2005 at 05:43 UTC
    Just skip past the first /'s?
    /.*\/(.*$)/