zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: Testing whether a string matches any pattern from a list
Date: Fri, 15 Nov 2024 10:35:46 +0000 (GMT)	[thread overview]
Message-ID: <2095757117.3382258.1731666946586@mail.virginmedia.com> (raw)
In-Reply-To: <1723013458.3381717.1731666592474@mail.virginmedia.com>

> On 15/11/2024 10:29 GMT Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> > On 15/11/2024 00:12 GMT Philippe Altherr <philippe.altherr@gmail.com>
> > wrote: I have an array that contains a list of patterns and a
> > string. I would like to check whether the string matches any of the
> > patterns in the array. For example if patterns=("foo*" "*bar"), then a
> > test for "foob", "obar", and "foobar" should return true and one for
> > "ooba" should return false.
> >
> > I hoped that I could use the following:
> >
> > if [[ -v patterns[(k)$string] ]]; then ... fi;
> >
> > Unfortunately, the pattern matching effect of the subscript flag
> > (https://zsh.sourceforge.io/Doc/Release/Parameters.html#Subscript-Flags) (k)
> > only works for associative arrays, not for regular arrays.
> 
> I think you might be hitting a slightly different problem, that the test
> is taking place in a special context that doesn't do the pattern lookup.
> This is rather obscure so probably not intentional.
> 
> At least, the following does work for me (and doesn't
> seem to be dependent on any options I have):
> 
> % string=B\*
> % print $signals[(k)$string]
> BUS
> 
> See if this works:
> 
> local lookup=${patterns[(k)$string]}
> if [[ -n $lookup ]]; then
>    # ...
> fi

Actually, it's only -v that's special --- so you can simply do

if [[ -n ${patterns[(k)$string]} ]]; then
  # ...
fi

pws


      reply	other threads:[~2024-11-15 10:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-15  0:12 Philippe Altherr
2024-11-15  5:48 ` Roman Perepelitsa
2024-11-15 10:29 ` Peter Stephenson
2024-11-15 10:35   ` Peter Stephenson [this message]

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=2095757117.3382258.1731666946586@mail.virginmedia.com \
    --to=p.w.stephenson@ntlworld.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).