Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!bu.edu!nntp-read!composer From: composer@chem.bu.edu (Jeff Kellem) Newsgroups: comp.lang.perl Subject: Re: path truncation quest Message-ID: Date: 21 Mar 91 17:21:16 GMT References: <1991Mar21.010547.13331@convex.com> <1991Mar21.064642.29427@eng.umd.edu> Sender: news@bu.edu.bu.edu Reply-To: composer@chem.bu.edu Organization: Boston University Chemistry Department Lines: 38 In-reply-to: ziegast@eng.umd.edu's message of 21 Mar 91 06:46:42 GMT In article <1991Mar21.064642.29427@eng.umd.edu> ziegast@eng.umd.edu (Eric W. Ziegast) writes: > In article <1991Mar21.010547.13331@convex.com> you write: > >Any suggestions on how, given the argument "/directory/sub/100.host.0" > >that one could take off the "100.host.0" component, leaving > >"/directory/sub" ? > > But in Perl, there's always a better way of doing things: > > $path =~ s/((\/[^\s\/]+)+)\/([^\s\/]+)/$1/; You could make the above a tiny bit more readable by writing it as: $path =~ s:((/[^\s/]+)+)/([^\s/]+):$1:; or, use whatever delimiters you'd like. That way you don't have to deal with quoting the slashes. On the other hand, if I was to write the above, assuming I didn't want to save the filename component, I'd probably write it as: $path =~ s|/[^/]*$||; This would get rid of any trailing slash, but will also turn "/" to "".. which is probably not what's wanted .. If you wanted to leave any trailing slash, you could write it as: $path =~ s|[^/]*$||; or $path =~ s|/[^/]+$||; There are, of course, other ways of dealing with the problem. That should give you a start, though... Cheers... -jeff Jeff Kellem Internet: composer@chem.bu.edu