From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17697 invoked by alias); 28 Feb 2012 07:47:04 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16807 Received: (qmail 11562 invoked from network); 28 Feb 2012 07:47:02 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at ckhb.org does not designate permitted sender hosts) Authentication-Results: cm-omr10 smtp.user=scowles@ckhb.org; auth=pass (CRAM-MD5) X-Authenticated-UID: scowles@ckhb.org Date: Mon, 27 Feb 2012 23:24:56 -0800 (PST) From: "S. Cowles" X-X-Sender: picmar@ckhb06.ckhb.org To: =?ISO-8859-15?Q?Jesper_Nyg=E5rds?= cc: zsh-users@zsh.org Subject: Re: Filtering an array In-Reply-To: Message-ID: References: User-Agent: Alpine 2.00 (LN8 1167 2008-08-23) X-Face: Ek1c-Ll9]E|9mF*Z|hf5VSHqF.]0Qv%;h%=Zne"Y3am*(:Tf_BlXI;j'}FMhu%sNCjSk|cxD~oD:g5Tv,gN}{Y8("m<8<%%=_vy MIME-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="-1463756798-1804439679-1330410023=:18037" Content-ID: ---1463756798-1804439679-1330410023=:18037 Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-15; FORMAT=flowed Content-Transfer-Encoding: 8BIT Content-ID: On Fri, 24 Feb 2012, Jesper Nygårds wrote: > Date: Thu, 23 Feb 2012 23:11:29 > From: Jesper Nygårds > To: zsh-users@zsh.org > Subject: Filtering an array > > I am scratching my head over getting something seemingly simple to work. > > I have written this function: > > findfiles() { > local myfiles myarg > myfiles=( **/*$1* ) > shift > for myarg in $@; do > myfiles=${(M)myfiles:#*$myarg*} > done > print $myfiles > } jesper this is my version of your function. the biggest difference i see between your function and my version (with regard to intent and disregarding coding errors), is the initial expansion of ${myfiles}. file selection in both versions is done by pattern matching, so i did not use the same initial file list loading as yours myfiles=( **/*$1* ) to cause any subsetting; that step struck me as unneccessary. also, your version redefines ${myfiles} in every instance of the for loop; this never accummulates the pattern matches which is what i thought was your objective. if i misunderstood your requirements, i apologize. the function shown here does give a correct list of unique files matching the pattern[s] given on the invocation line. usage: findfiles starting_sub_dir pat1 [pat2...] findfiles(){ # parameter declarations. typeset -a pats allfiles typeset -aU selection local srcdir pat # argument parsing and checking. srcdir=${1} if [[ ! -d ${srcdir} ]] then # bomb. return 1 fi shift # patterns for which to search. pats=( $@ ) # get all files (only files, including dot files) in specified # source directory. allfiles=( ${srcdir}/**/*(D,.) ) # initialize the collection. selection=() # do the subsetting and collecting. for pat in ${pats} do selection=( ${selection} # append any additional matches. ${(M)allfiles:#*${pat}*} ) done # report the results. print -l ${selection} # clean up. unset srcdir pat pats allfiles selection } ---1463756798-1804439679-1330410023=:18037--