in reply to Absolute pathnames from relative?

I myself come from a long family of dentists, and there have always loved toothpicks. I hereby present my solution in the form of an incurable form of falling toothpick syndrome;

s/\/[^\/]*\/\.\.\//\//g

Just set this baby on your concatenated paths, and your '..' will magically disappear. You could use

s/\/\.\//\/g
to strip your single dots as well...

This will just give you the path it 'should' be, if there are no weird filesystem thingies going on.

(yes, I could've used s### or something, but this is more fun.)

Replies are listed 'Best First'.
Re^2: Absolute pathnames from relative?
by graff (Chancellor) on Oct 15, 2005 at 16:34 UTC
    Oy veh! This is a case where hairdressers might write the less frightening code, using curlies:
    s{/[^/]*/+\.{2}/}{/}g;
    (also switched to using positive-lookahead to check for the final "/" in the pattern, so as to avoid the need for a loop; and just in case the input is "goofy but functional", ala "/one/two//../three", I added "+").

    (Update: never mind what I said about positive-lookahead. There seems to be no way to avoid using a loop to make this "work", so no point using "(?=/)" as the last part of the match. This was never intended as a serious solution anyway, given the issues discussed in other replies.)

Re^2: Absolute pathnames from relative?
by argv (Pilgrim) on Nov 20, 2004 at 03:36 UTC
    You said:

    s/\/[^\/]*\/\.\.\//\//g

    which is exactly what I was looking for... but it doesn't work if there are two together, as in "foo/bar/../../baz/" So, put it in a loop:

    1 while $foo =~ s/\/[^\/]*\/\.\.\//\//g;

    dan
    www.danheller.com