Re: simplify a path

2008-07-06 Thread Brad Baxter
On Jul 3, 4:38 pm, [EMAIL PROTECTED] (Rob Dixon) wrote: > > Pardon me for being dense, but I can't figure out a case where > > >> if ($path[-1] eq '..') { > > > would ever be true. > > My intention was to preserve relative paths starting with '..', but I got it > wrong. Here's V2. > > Rob > >

Re: simplify a path

2008-07-03 Thread Rob Dixon
Brad Baxter wrote: > On Jul 2, 11:46 am, [EMAIL PROTECTED] (Rob Dixon) wrote: >> This seems to do the job. Hope it helps. >> >> Rob >> >> sub canonical_path { >> >> my $path = shift; >> my @path; >> >> foreach (File::Spec->splitdir($path)) { >> if ($_ eq '..') { >> if ($path[-1] eq

Re: simplify a path

2008-07-03 Thread Brad Baxter
On Jul 2, 11:46 am, [EMAIL PROTECTED] (Rob Dixon) wrote: > > This seems to do the job. Hope it helps. > > Rob > > sub canonical_path { > > my $path = shift; > my @path; > > foreach (File::Spec->splitdir($path)) { > if ($_ eq '..') { > if ($path[-1] eq '..') { > push @path, $

Re: simplify a path

2008-07-02 Thread Rob Dixon
John W. Krahn wrote: > John W. Krahn wrote: >> Chen Yue wrote: >> >>> I have a file containing UNIX-styled Path in each line. But the path is >>> simplified enough. Some of them has ".." and "." in the middle, such as >>> "/a/b/./c/../d". Now I want to simplify each Path according to Unix >>> trad

Re: simplify a path

2008-07-02 Thread John W. Krahn
John W. Krahn wrote: Chen Yue wrote: I have a file containing UNIX-styled Path in each line. But the path is simplified enough. Some of them has ".." and "." in the middle, such as "/a/b/./c/../d". Now I want to simplify each Path according to Unix tradition. /a/b/./c/../d->/a/b/d T

Re: simplify a path

2008-07-02 Thread John W. Krahn
Chen Yue wrote: Hi Hello, I have a file containing UNIX-styled Path in each line. But the path is simplified enough. Some of them has ".." and "." in the middle, such as "/a/b/./c/../d". Now I want to simplify each Path according to Unix tradition. /a/b/./c/../d->/a/b/d The only w

Re: simplify a path

2008-07-02 Thread Rob Dixon
Chen Yue wrote: > > Rob Dixon wrote: >> >> Chen Yue wrote: >>> >>> I have a file containing UNIX-styled Path in each line. But the path is >>> simplified enough. Some of them has ".." and "." in the middle, such as >>> "/a/b/./c/../d". >>> Now I want to simplify each Path according to Unix traditi

Re: simplify a path

2008-07-02 Thread Amit Saxena
Hi I agree that the code has some flaws in that. Infact immediately after posting my solution, I thought of your test case only. If I will get some time, I will work on the same and let you know. Thanks for the feedback. Regards, Amit Saxena On Wed, Jul 2, 2008 at 8:08 PM, Chen Yue <[EMAIL PR

RE: simplify a path

2008-07-02 Thread Chen Yue
Hi Amit The code really works but with a tiny flaw. If the path is "/a/b/../../c", the result would be "/a/../c" rather than "/c". So I need to loop to clean up the dot-dot if tied up Thank you for the suggestion Hi The code would be something like this :- [root@ ~]# cat t.p

RE: simplify a path

2008-07-02 Thread Chen Yue
Hi Peter Actually, I don't want to translate the path into realpath because some of the paths even do not exist on the host. What I need is just a simplified format. Anyway, thank you for the advice. On Wed, 02 Jul 2008 21:33:59 +0800, Chen Yue wrote: > I have a file containing UNIX-styled Path

RE: simplify a path

2008-07-02 Thread Chen Yue
Hi Rob Thank you for the suggestion. Actually, abs_path requires the original path exists on the host. But my case does not fulfill this. This is what puzzled me. Sent: Wednesday, July 02, 2008 9:58 PM To: beginners@perl.org Cc: Chen Yue Subject: Re: simplify a path Chen Yue wrote: >

Re: simplify a path

2008-07-02 Thread Peter Scott
On Wed, 02 Jul 2008 21:33:59 +0800, Chen Yue wrote: > I have a file containing UNIX-styled Path in each line. But the path is > simplified enough. Some of them has ".." and "." in the middle, such as > "/a/b/./c/../d". > Now I want to simplify each Path according to Unix tradition. > > /a/b/./c/.

Re: simplify a path

2008-07-02 Thread Amit Saxena
Hi The code would be something like this :- [root@ ~]# cat t.pl #!/usr/bin/perl $str = "/a/b/./c/../d"; $str1 = $str; print "\nstr = [$str]"; print "\nstr1 = [$str1]"; print "\n---"; $str1 =~ s/([^\/]+)\/\.\.\/([^\/]+)/$2/g; print "\nstr = [$st

Re: simplify a path

2008-07-02 Thread Rob Dixon
Chen Yue wrote: > > I have a file containing UNIX-styled Path in each line. But the path is > simplified enough. Some of them has ".." and "." in the middle, such as > "/a/b/./c/../d". > Now I want to simplify each Path according to Unix tradition. > > /a/b/./c/../d->/a/b/d > > The only

Re: simplify a path

2008-07-02 Thread Jeff Peng
On Wed, Jul 2, 2008 at 9:33 PM, Chen Yue <[EMAIL PROTECTED]> wrote: > Hi > > I have a file containing UNIX-styled Path in each line. But the path is > simplified enough. Some of them has ".." and "." in the middle, such as > "/a/b/./c/../d". > Now I want to simplify each Path according to Unix trad