in reply to Extract Name of Parent Directory

Though if you *really* feel like reinventing the wheel {grin}:

my $dir = "/htdocs/project1/docs/file.html"; my ($parent) = $dir =~ m#\A.*/([^/]+)/[^/]*\z#; print $parent, "\n";


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Replies are listed 'Best First'.
Re: Re: Extract Name of Parent Directory
by cchampion (Curate) on Jul 17, 2003 at 07:10 UTC
    if you *really* feel like reinventing the wheel

    Well, if you really must, at least make it short.

    my $dir = "/htdocs/project1/docs/file.html"; my $parent = (split /\//, $dir)[-2];

      man, you guys have obviously never been paid by the line of code - let's use the power of timtowtdi to make this even longer:

      my $file = "/htdocs/project1/docs/file.html"; my $elif = reverse $file; my $slash = index($elif,'/'); my $index = length( $file ) - $slash; $index--; my $dir = substr($file,0, $index); print $dir;
      You know it makes sense ;-)

      /J\