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

Hello - I have a script that reads in a file. Each line in the file is a directory path and a filename.

I'd like to be able to loop through the directories and replace the backslash with a pipe so that each directory would be delimited.

Here's a snippet :

use File::Spec::Win32; while (<INPUT>) { #split the on pipe ($Name,$Size,$LastChangeDate,$LastAccessDate,$CreatedDate,$Exte +nsion,$Path) = split(/\|/, $_); (my $volume, my $directories, my $file ) = File::Spec::Win32->s +plitpath( $Path ); my @dirs = File::Spec::Win32->splitdir($directories); print "@dirs|$Name"; print "\n";
The input looks like this :
AR2007-37.doc|49152|6/28/2007 3:50:28 PM|4/23/2015 3:52:23 AM|5/30/2007 2:48:25 PM|.doc|h:\shares\ca\ACTION-REQUESTS\ACTION-REQUEST-ARCHIVES\ACTION-REQUEST-FILES-2007\

and the output from my script looks like this :
shares ca ACTION-REQUESTS ACTION-REQUEST-ARCHIVES ACTION-REQUEST-FILES-2007 |AR2007-65.doc

I would like to insert a character delimiting each directory in the output.
I'm not sure how to access each element in the @dirs to be aboe to do this.

Help appreciated!

Replies are listed 'Best First'.
Re: File::Spec splitdir
by 1nickt (Canon) on Oct 26, 2015 at 15:54 UTC

    Seems like you could just use join here:

    my @dirs = File::Spec::Win32->splitdir($directories); my $delimiter = '!!SPLORG!!'; print join($delimiter, @dirs) . "|$Name"; print "\n";
    You can also better write your declaration as:
    my ( $volume, $directories, $file ) = File::Spec::Win32->splitpath( $P +ath );
    Hope this helps!

    The way forward always starts with a minimal test.
      Thank you very much for the reply - it works perfectly! I should've known about join function - I don't use Perl as much as I used to. Appreciated!
Re: File::Spec splitdir
by mr_ron (Deacon) on Oct 26, 2015 at 17:19 UTC

    You seem to be new to PerlMonks, in which case welcome. The directory in your data has leading and trailing slashes which seem to add extra empty directories at the beginning and end from splitpath. I tested under windows BTW. Anyway something like the line below should move you along in the direction you are trying to go.

    print join('|', @dirs), $Name, "\n";
    Ron
      Perfect, thanks! Appreciated!