zsh-users
 help / color / mirror / code / Atom feed
From: "S. Cowles" <scowles@ckhb.org>
To: "Jesper Nygårds" <jesper.nygards@gmail.com>
Cc: zsh-users@zsh.org
Subject: Re: Filtering an array
Date: Mon, 27 Feb 2012 23:24:56 -0800 (PST)	[thread overview]
Message-ID: <alpine.LN8.2.00.1202272208530.18037@ckhb06.ckhb.org> (raw)
In-Reply-To: <CABZhJg9QFx89rSrRhaN8UPNpbfsKHGQbAVJhzYGT+05PYeRR=g@mail.gmail.com>

[-- Attachment #1: Type: TEXT/PLAIN, Size: 2002 bytes --]

On Fri, 24 Feb 2012, Jesper Nygårds wrote:
> Date: Thu, 23 Feb 2012 23:11:29
> From: Jesper Nygårds <jesper.nygards@gmail.com>
> 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
}

  parent reply	other threads:[~2012-02-28  7:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-24  7:11 Jesper Nygårds
2012-02-24 10:25 ` Felix Herrmann
2012-02-28  7:24 ` S. Cowles [this message]
2012-02-29 13:43   ` Jesper Nygårds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LN8.2.00.1202272208530.18037@ckhb06.ckhb.org \
    --to=scowles@ckhb.org \
    --cc=jesper.nygards@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).