From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id 5A5322153B for ; Fri, 15 Nov 2024 01:29:27 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1731630567; b=ox/VZtzMh5dKlbVFhd5tmb1M44Y3b2qfnUs5HuDrBgcs0PTUnBAvB54n1TgvFVQcXfKw9PgDgJ eIuTfgvlI3RmfSRyF9H2THrTh5cOADCZcBqM32xryblbNgx7FX395AvxmbPn7Q9eSIfa1FkiLJ /lxTlVuZONHGTEgfhs3Bdc+xDcfIbRLItnN4G0dRTBNUu1BBCtdqk9RytAW8QR8PwpCDRX2PID 0g7IZfQLO9mzntatlwEJxSo8eZXS3GuVqk20eVcXp6WwWKdpBxeERJaAYWxzr1mUhNJFMdwlBB BSKaYb9qjkJ1pFIU3BpxcKVPKT1L4NT3Tt3wNgC7FUHAYA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f53.google.com) smtp.remote-ip=209.85.218.53; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1731630567; bh=vnvyAktOZM7tgVjjj+R4hbtnFYXwfM4lE1fN/UvnYdA=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:To:Subject:Message-ID:Date:From:MIME-Version: DKIM-Signature:DKIM-Signature; b=OwDdRPOqI5XZaTmYv+aZGtvkek78iVkeGxRjX270T4XvH58rt6V38poYpS+a+o987Vdks1PKWF t6H1bo34VDWHTzSQ/usGibX8ZXQteXKZIlAB0lHJKuotJO62qiaqA3lHVkiiYKITi4US1uiZYO EVRaFiC+4Oi+vpquloMfPws3JxbmcoJy+WcocxwUBkgNslO/5thrIaPfmwb42MWmXxHE/ZKeT2 q1Gxt41YjXx2LGrCUNA7TN6IqsESCFRZeAkx3+xDRoX1yESjIM54Zk6/o8MhJgoeZHH1QQpkmi 8AChwBAWWz2TR+MkSCOxl2H4LMnMmZimL7AIOYfvZHBLuQ==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:To:Subject:Message-ID: Date:From:MIME-Version:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=vnvyAktOZM7tgVjjj+R4hbtnFYXwfM4lE1fN/UvnYdA=; b=U7qQg4kjrbEcp2Wzc5g3zHA7la g3Xf3W0ZYz7rEoerbgpn7SYBZdHDJ7hU8gHPmc3C9KRuFw2wDrmTP9MO3IItorle3Fni+kQEfkcea cbpq2MUv4LV4erjdFp0bsI/rtf+xEUvWRDvSVXF+TFk4t/88v6iD0B1c4ZzoetXTlS335AIerEh0+ 2sWQc8/z8kE/haq6CtOBKDV9XOkaF/OO3N0XlHVBm1cImb2Tpag2dth0GjQXAi34tEOoPSU60koqt F4XuSiykYktJljsF/niggRckSMJgl61D1LNSgMenet3wbR9UlNsyUD3tJ1yfg1e2ldhGjp5D8nP4O /3pB7nrA==; Received: by zero.zsh.org with local id 1tBkDF-000ALt-MT; Fri, 15 Nov 2024 00:29:25 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f53.google.com) smtp.remote-ip=209.85.218.53; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-ej1-f53.google.com ([209.85.218.53]:38781) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1tBjwn-0008px-AI; Fri, 15 Nov 2024 00:12:26 +0000 Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a9a017a382bso20160466b.3 for ; Thu, 14 Nov 2024 16:12:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731629544; x=1732234344; darn=zsh.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=vnvyAktOZM7tgVjjj+R4hbtnFYXwfM4lE1fN/UvnYdA=; b=MqSm1KjgnlrxGlHT+aDjeIPvfargYodjcDmcUCZcdpEgFh0nfBMZiqCSuL+5i7PzD6 otwKhLSjwDD40UzTHVBaiKIvi8Z0Ok4+EfajTb7pM0PmLSbZK/Aq5wfG6hsXvW3hEm/w dFpwq5Iyjp2GBqteqn5L5mrwxlgrbfImbC/vHHMx0yN9wZ/cBQK+JPprJ5XfbpLfzIpQ Cwwpc4XWMwv9yMg7SvhsV5gknOlaOuMp7MrDMe5+XB5RWmku7e0oDLKMwfwLMK6Xmsmk VOLGEy9CXcjUxgC55TL7SmDn26nDYSaVShPnwJ2l6/DFJURmCc0jJU+K5OFEFt3wH6Ik 2rsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731629544; x=1732234344; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=vnvyAktOZM7tgVjjj+R4hbtnFYXwfM4lE1fN/UvnYdA=; b=KW7vKBhtuHB7inTDr3TJxEvrIskozXcMfVwLBQpXQZR8SN7CZds18w8nIILRFgkFU/ vtxJJm0J9Fv4yDKMSjlCiH3gT5z8Mo3LBk2wdKiKUnuBqW9/jcSRQaEiZ1Hohu578gWp ouquHn7LAjgaeFC6OiTsdnl/nWHyhmZMe5U2Li0leKkbT9nxW1NVMSnHns7N9PE0J+7a jlzvcjWxjeYXDRKfFgd2Vr1Mcen5m3wDQ2OyCsffGIc5MGvLntT5EsmQ5mGapaKZUImZ tm204uXLTsjzRdrI+myKMYP3/ZMfXwJYJ/+AFAztzVkXtFk3/UNclIGBaa5bS4nwsqU0 +tVg== X-Gm-Message-State: AOJu0YxjJn5Po/CWsx0khASpllLfb8DHe+MJKRxIy3mNcFQkZ/wZbycs zbUG8sbGrRbjYNganswSuuEx9lm2NzJMczqUIjMkw17s7BcFlsZ0GRhUvJPKhU7Qe2cgT2uGJio C7kMoOoohqv/ljLv/l8BBOp7A1hzxkD8G X-Gm-Gg: ASbGncuaWoADCf9gxtTDesJ57UcRT5//qI8tokg5hOwsyLLVzvOKJEyj6x3HUZCt6Uk Xgal2tvVftLQEpRZjXjBK1Hb3npkzhKsf X-Google-Smtp-Source: AGHT+IHKjsBn3Ye1flF2v8ba/qO7NXMU4RStfZprmCMIfywFd+UvZd88Zv4IGrzL4ip6eSeuDfJ3ojBXo3LaEC41ut4= X-Received: by 2002:a17:907:1c85:b0:a99:f388:6f49 with SMTP id a640c23a62f3a-aa4835285d4mr18433166b.9.1731629544100; Thu, 14 Nov 2024 16:12:24 -0800 (PST) MIME-Version: 1.0 From: Philippe Altherr Date: Fri, 15 Nov 2024 01:12:12 +0100 Message-ID: Subject: Testing whether a string matches any pattern from a list To: zsh-users@zsh.org Content-Type: multipart/alternative; boundary="000000000000dbdd0c0626e86b70" X-Seq: 30107 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: --000000000000dbdd0c0626e86b70 Content-Type: text/plain; charset="UTF-8" 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 (k) only works for associative arrays, not for regular arrays. I fixed the "problem" by turning my array of patterns into an associative array: local -A patterns=("foo*" X "*bar" X) It's not a big deal but it feels a bit unnecessarily complicated. Is there a simpler way to achieve this? If not, would it make sense to change (k) and (K) to also do pattern matching on regular arrays? Philippe --000000000000dbdd0c0626e86b70 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I have an array that contains a list of patterns and a str= ing. I would like to check whether the string matches any of the patterns i= n the array. For example if patterns=3D("foo*" "*bar"),= then a test for "foob", "obar", and "foobar"= should return true and one for "ooba" should return false.
<= br>
I hoped that I could use the following:

<= div>if [[ -v patterns[(k)$string] ]]; then ... fi;

Unfortunately, the pattern matching effect of the=C2=A0subscript f= lag=C2=A0(k) only works for associative arrays, not for regular arrays.= I fixed the "problem" by turning my array of patterns into an as= sociative array:

local -A patterns=3D("foo*&q= uot; X "*bar" X)

It's not a big deal= but it feels a bit unnecessarily=C2=A0complicated. Is there a simpler way = to achieve this?

If not, would it make sense to ch= ange (k) and (K) to also do pattern matching on regular arrays?
<= br>
Philippe

--000000000000dbdd0c0626e86b70--