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

My output prints the full directory absolute path. I want to get the output where it doesnt print the startdirectory.
My output currently prints out:
/startdirectory/index.html /startdirectory/nextdir/here.html /startdirectory/anotherdir/home.html
I would like it to print out without the startdirectory:
/index.html /nextdir/here.html /anotherdir/home.html
My attempt but didnt work:
use strict; use warnings; my $diry = '/startdirectory'; my $newname; $name = $File::Find::name; open ( DATA, $name ) || warn "Can\'t open File $name: $!\n"; while($hit = <DATA>) { if ($hit =~ /regexpressionhere/i) { ($newname) = $name =~ /$dir(\s+*)/i; print "$1\n"; } } close DATA;

Replies are listed 'Best First'.
Re: changing File Find name
by arden (Curate) on Mar 09, 2004 at 14:38 UTC
    Here's a short example of how to do what you want.

    while($hit = <DATA>) { if ($hit =~ /regexpressionhere/i) { $hit =~ s|^/startdirectory||; print "$hit\n"; } }

    - - arden.

Re: changing File Find name
by skx (Parson) on Mar 09, 2004 at 15:07 UTC

    If you know that the prefix is always there I'd not bother with a regexp.

    Just take the substr function and remove the first N characters from your string.

    Verbosely it'd look something like this:

    while($hit = <DATA>) { print substr( $hit, length( '/startdirectory' ) ); }
    Steve
    ---
    steve.org.uk
      Thanks thats what I needed! It now looks great.
Re: changing File Find name
by tinita (Parson) on Mar 09, 2004 at 15:25 UTC
    or here's a more general solution. suppose you have the $fullpath and the start directory $startdir:
    use File::Spec; my $file = '/' . File::Spec->abs2rel($fullpath, $startdir);