Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Filtering an array
- X-seq: zsh-users 16807
- From: "S. Cowles" <scowles@xxxxxxxx>
- To: Jesper Nygårds <jesper.nygards@xxxxxxxxx>
- Subject: Re: Filtering an array
- Date: Mon, 27 Feb 2012 23:24:56 -0800 (PST)
- Authentication-results: cm-omr10 smtp.user=scowles@xxxxxxxx; auth=pass (CRAM-MD5)
- Cc: zsh-users@xxxxxxx
- In-reply-to: <CABZhJg9QFx89rSrRhaN8UPNpbfsKHGQbAVJhzYGT+05PYeRR=g@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CABZhJg9QFx89rSrRhaN8UPNpbfsKHGQbAVJhzYGT+05PYeRR=g@mail.gmail.com>
On Fri, 24 Feb 2012, Jesper Nygårds wrote:
Date: Thu, 23 Feb 2012 23:11:29
From: Jesper Nygårds <jesper.nygards@xxxxxxxxx>
To: zsh-users@xxxxxxx
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
}
Messages sorted by:
Reverse Date,
Date,
Thread,
Author