Here is a quick solution that took me less time to write again than it would have taken to find where I wrote this before:

#!/usr/bin/perl use strict; use warnings; use File::Spec; # Note that test data is Unix-format! warn "Test data is in Unix format; may not work on $^O" unless File::Spec->isa('File::Spec::Unix'); my @Files = map {chomp; $_} (<DATA>); # find common prefix directories for a list of files; assuming that al +l # files are on the same volume, if the system has volumes sub common_prefix (@) { my @dirs = map {[File::Spec->splitdir((File::Spec->splitpath($_))[1] +)]} @_; my @prefix = @{shift @dirs}; foreach my $dir (@dirs) { for (my $i = 0; $i < @$dir && $i < @prefix; $i++) { splice @prefix, $i unless $dir->[$i] eq $prefix[$i] } } return @prefix } print "Sample files:\n"; print ' ',$_,"\n" for @Files; print "Common prefix:\n"; print ' ',File::Spec->catdir(common_prefix @Files),"\n"; __DATA__ /foo/bar/bax /foo/bar/baz /foo/baz/quux

Sample output:

Sample files: /foo/bar/bax /foo/bar/baz /foo/baz/quux Common prefix: /foo

In reply to Re^3: File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string by jcb
in thread File::Spec::Win32 returning different result than File::Spec when doing catfile with empty string by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.