Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Filtering an array



Hi :)

On Fri, 24 Feb 2012 08:11:29 +0100, Jesper Nygårds wrote:
> The idea is to find all files that contain all of the strings given. I
> first find all files containing the first argument, then shift away
> the first argument and filter the array of file names with each of the
> remaining arguments.
> 
> However, for some reason only the first round of filtering (i.e. using
> the second argument) is happening[...].

I'm no expert at all at zsh-scripts but I think the problem is that
myfiles isn't an array anymore after the first iteration. I changed your
code to the following:

findfiles() {
    local myfiles myarg
    myfiles=( **/*$1* )
    shift
    for myarg; do
        myfiles=(${(M)myfiles:#*$myarg*})
    done
    print $myfiles
}

There are two little differences here. First of all, I didn't write '$@'
in the for-line. If you don't give an 'in x', for iterates over the
remaining arguments.

The second difference - and the more important one - is, that I put ()
around the rhs of the myfiles-assignment, to make sure to assign an
array to the variable myfiles. I think that was the catch. After this
modification, your script works fine :)

Greetings,
Felix Herrmann.



Messages sorted by: Reverse Date, Date, Thread, Author