HTTP-404 has asked for the wisdom of the Perl Monks concerning the following question:

Hello

ok i have hash it gets info from conifguration file(works fine)
$conf{rootdir}="e:/st"; <- let think it's like this :-)
and i have path to file like that
$files[$i]="e:/st/dir1/text.xt";
i use this pattern to get dir1/text.txt part but i get it wrong
/^$conf{rootdir}(\w+\.\w+)/ <-it works fine if file is in rootdir but +if it's in dir under rootdir, no good
ok in breif i need to do this:
before: $files[$i]="e:/st/dir1/text.xt"; after: $files[$i]="dir1/text.xt";\
Thank You very much

2001/03/20 - Edited by jcwren for <code> tags

Replies are listed 'Best First'.
Re: Cutting of Path
by Masem (Monsignor) on Mar 21, 2001 at 02:18 UTC
    Well, it should be easier to do it this way:
    $files[$i] =~ m#^$conf{rootdir}/(.*)$#; # use alternate regex delimit +ers to avoid confusion $files[$i] = $1;
    Assuming that $files$i will only contain a path name. Your method is trying to match (word.word), which will obviously fail if there are sub dirs.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

      If you want to match a literal string in a regex, then you really need to use \Q and \E: $files[$i] =~ m#^\Q$conf{rootdir}\E/(.*)$#;

      That code can still fail on quite a few platforms (including the one used by the author of the original question).

      You would probably be better off to use File::Spec for this kind of stuff (though it requires Perl 5.6, unfortunately).

              - tye (but my friends call me "Tye")
        How about this low-tech solution? Since the rootdir is at the start of the string...
        substr($files[$i], 0, length($conf{rootdir})+1) = '';
        buckaduck

        UPDATE: I added 1 to the length to deal with the extra forward slash character...

      wow guys ur fast as hell
        this one works very nice /^$conf{rootdir}(.*)$/
Re: Cutting of Path
by TGI (Parson) on Mar 21, 2001 at 04:13 UTC

    Just for the sake of TMTOWTDI try:

    substr($files[$i],0,length($conf{rootdir})+1,'');
    - or -
    substr($files[$i],0,length($conf{rootdir})+1) = '';
    - or -
    $files[$i] = substr($files[$i],length($conf{rootdir}));

    TGI says moo