Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!bionet!ames!dftsrv!mimsy!mojo!ziegast From: ziegast@eng.umd.edu (Eric W. Ziegast) Newsgroups: comp.lang.perl Subject: Re: path truncation quest Keywords: path,truncation Message-ID: <1991Mar21.064642.29427@eng.umd.edu> Date: 21 Mar 91 06:46:42 GMT References: <1991Mar21.010547.13331@convex.com> Sender: news@eng.umd.edu (C-News) Organization: University of Merryland, Engineering Computing Services Lines: 38 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" ? At first, I thought about using an array: @tmp = split(/\//,$path); $dir = join("/",pop(@tmp)); But in Perl, there's always a better way of doing things: $path =~ s/((\/[^\s\/]+)+)\/([^\s\/]+)/$1/; A simplified version would be: /((\/F)+)+)\/(F)/ F matches a one word file/directory name. In my example I use [^\s\/]+ which simply matches any word without whitespace or "/" in it. You may want to use something more strict instead. $1 will match the directory path. (man dirname) $2 is used only for /F grouping $3 will match the base name. (man basename) This is, of course, only if it's a full path name. Anything else won't match (and won't be changed). -- Perl - 1. (n) The Mother of All Languages 2. (v) To convert an otherwise boring program to use Perl. [-ed, -ing] ________________________________________________________________________ Eric W. Ziegast, University of Merryland, Engineering Computing Services ziegast@eng.umd.edu - Eric@(301.405.3689)