zsh-users
 help / color / mirror / code / Atom feed
* Filtering an array of file names
@ 2016-09-29  6:43 Jesper Nygårds
  2016-09-29  7:18 ` Bart Schaefer
       [not found] ` <160929001853.ZM27784__20418.9757038423$1475133607$gmane$org@torch.brasslantern.com>
  0 siblings, 2 replies; 6+ messages in thread
From: Jesper Nygårds @ 2016-09-29  6:43 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 273 bytes --]

Suppose I have an array of file names, generated from some external
program. Is there an zsh-idiomatic way of filtering this array to only keep
directories?

I suppose I am looking for something on the line of
${(M)files:#<something>}, but I can't figure out how to do it.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filtering an array of file names
  2016-09-29  6:43 Filtering an array of file names Jesper Nygårds
@ 2016-09-29  7:18 ` Bart Schaefer
  2016-09-29  7:38   ` Jesper Nygårds
  2016-09-29  7:54   ` Mikael Magnusson
       [not found] ` <160929001853.ZM27784__20418.9757038423$1475133607$gmane$org@torch.brasslantern.com>
  1 sibling, 2 replies; 6+ messages in thread
From: Bart Schaefer @ 2016-09-29  7:18 UTC (permalink / raw)
  To: Zsh Users

On Sep 29,  8:43am, Jesper Nygards wrote:
} 
} Suppose I have an array of file names, generated from some external
} program. Is there an zsh-idiomatic way of filtering this array to only
} keep directories?

Are these local file names that you can compare to the filesystem?  If
so, you can use rc-expand-param and globbing qualifiers:

  directories=( ${^allfiles}(/) )

(This didn't work on some old versions of the shell, where qualifiers
were interpreted only if the preceding word had a globbing character.)

If they don't refer to the local filesystem, you'll need the array to
have already marked them somehow, such as with a trailing slash, and
then ${(M)files:#*/} would work.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filtering an array of file names
  2016-09-29  7:18 ` Bart Schaefer
@ 2016-09-29  7:38   ` Jesper Nygårds
  2016-09-29  7:54   ` Mikael Magnusson
  1 sibling, 0 replies; 6+ messages in thread
From: Jesper Nygårds @ 2016-09-29  7:38 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 842 bytes --]

Thank you, Bart! This works perfectly.


On Thu, Sep 29, 2016 at 9:18 AM, Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Sep 29,  8:43am, Jesper Nygards wrote:
> }
> } Suppose I have an array of file names, generated from some external
> } program. Is there an zsh-idiomatic way of filtering this array to only
> } keep directories?
>
> Are these local file names that you can compare to the filesystem?  If
> so, you can use rc-expand-param and globbing qualifiers:
>
>   directories=( ${^allfiles}(/) )
>
> (This didn't work on some old versions of the shell, where qualifiers
> were interpreted only if the preceding word had a globbing character.)
>
> If they don't refer to the local filesystem, you'll need the array to
> have already marked them somehow, such as with a trailing slash, and
> then ${(M)files:#*/} would work.
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filtering an array of file names
  2016-09-29  7:18 ` Bart Schaefer
  2016-09-29  7:38   ` Jesper Nygårds
@ 2016-09-29  7:54   ` Mikael Magnusson
  2016-09-29 15:51     ` Bart Schaefer
  1 sibling, 1 reply; 6+ messages in thread
From: Mikael Magnusson @ 2016-09-29  7:54 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On Thu, Sep 29, 2016 at 9:18 AM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> On Sep 29,  8:43am, Jesper Nygards wrote:
> }
> } Suppose I have an array of file names, generated from some external
> } program. Is there an zsh-idiomatic way of filtering this array to only
> } keep directories?
>
> Are these local file names that you can compare to the filesystem?  If
> so, you can use rc-expand-param and globbing qualifiers:
>
>   directories=( ${^allfiles}(/) )

This should be
  directories=( ${^allfiles}(/N) )
unless you have nullglob set, otherwise it will either fail or fill up
the directories array with entries like "foo(/)" depending on if
nomatch is set.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filtering an array of file names
       [not found] ` <160929001853.ZM27784__20418.9757038423$1475133607$gmane$org@torch.brasslantern.com>
@ 2016-09-29  7:55   ` Daniel Shahaf
  0 siblings, 0 replies; 6+ messages in thread
From: Daniel Shahaf @ 2016-09-29  7:55 UTC (permalink / raw)
  To: Zsh Users

Bart Schaefer wrote on Thu, Sep 29, 2016 at 00:18:53 -0700:
>   directories=( ${^allfiles}(/) )

Use the (N) qualifier if it's possible to have zero matches:

    directories=( ${^allfiles}(/N) )


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filtering an array of file names
  2016-09-29  7:54   ` Mikael Magnusson
@ 2016-09-29 15:51     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2016-09-29 15:51 UTC (permalink / raw)
  To: Zsh Users

On Sep 29,  9:54am, Mikael Magnusson wrote:
}
} This should be
}   directories=( ${^allfiles}(/N) )
} unless you have nullglob set

Thanks, and to Daniel also, for pointing out this nuance for the general
case, but in the OP's specific example $allfiles contains only names of
known existing files, so globbing is not expected to fail and (N) would
not be necessary.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-09-29 15:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-29  6:43 Filtering an array of file names Jesper Nygårds
2016-09-29  7:18 ` Bart Schaefer
2016-09-29  7:38   ` Jesper Nygårds
2016-09-29  7:54   ` Mikael Magnusson
2016-09-29 15:51     ` Bart Schaefer
     [not found] ` <160929001853.ZM27784__20418.9757038423$1475133607$gmane$org@torch.brasslantern.com>
2016-09-29  7:55   ` Daniel Shahaf

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).