From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21750 invoked from network); 31 May 2021 17:37:41 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 31 May 2021 17:37:41 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1622482661; b=xo7vfTu5BmLDIEOOi5O9E30xRBeI9nI8d/2/yZS6rLvo9WKDfCpPqqUI0yF58hK4+f7bgC5EDn Wy+WPKSso+cjzvrEzNFTyZBm7tzDdxUgs0Jz58wzvLAJu3HZMiJ2TKz8bhfmITup6HLGDIbF9w 7yhBTgGGwT7U77ZdOn5P4OzFoTtOHIDStf3vvcdowWjt+u91uZwMo8z88H4EiXYLkxbv9w+kRl tbv2D4Jh6fFr2MSkZKduRJzin1WA4f+GGzWhPoywSyWuNhv6dVEaz1QABWG9SO0vaPpR3T8dh3 x4J/M8LfrpWaN3c3wy8RzTjy2QxMGaQE7baqt/XME6zmXQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay11.mail.gandi.net) smtp.remote-ip=217.70.178.231; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1622482661; bh=2QnfBTQU8FkqlbEnUhjZvSGwzWMTsizG+1GSw7smGGU=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:DKIM-Signature; b=MxsCWY9TNMHCthEmfkNdy6lFsGYqI9MO7EDkqhEcZ6SC0QY26DCzqx5gEj1cVY+gP3JX5Bkvt/ kNLaWQfTNbI26+olzaHTDWYXQCwG1yMdeMGhjYExRFrSMsvNQf1SFjDzHMUqB1CdBtASLbO5W0 ATExo20ltU0SnxorZ26ZmO/1vUbS//xowDyxD1RVfPkDGa5zgTllibm9Jv0ua/tFPOYK7PN7PC fSAPCzqOGNJ9mp3bYn9JOHNlbv3TCLdvXcAohHds+YoWRG2zlul5ckMhshcYdn9BbCYQkKIewa XXd1zehltO2Ck6udNs37unTeU2ZTvTw7TMODnmKutbi9ow==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID; bh=YBGsDUSdPWBvR5PJFX+90wj3hRtsSNVpfc62l+nvyM0=; b=VRvSZXmpMJJCgUrtGepUsujaxC 6JpGVhc4axGfBDwyH4IGu6AJs97qyQNlWrKE+rFM+dQH9orFNQEQ704B+fecchfKjy3n0WTzR1XwN vV7yVsyXtIF98nVY/XUCqPFnGYo8PiGLxaW3T+lo1jE1Cz6aidChggwN8UabYmXr8tjW3bFSPO4tc AnBb+AulxzkVTnlG86K4xui2WehO+jXTQ23+zPWoM2DFTPTRawgYn0mG+Phr+gosZjxSziFPYcahG ruYvgcrcCknb4kRtm6ZJhAWu3/PzHbkqFS/IVmuvdW0nEq7Gv6uhRos5mNa/aFfipAQ6GpTVFFS4r DajUGsRA==; Received: from authenticated user by zero.zsh.org with local id 1lnlrB-000Gqg-72; Mon, 31 May 2021 17:37:41 +0000 Authentication-Results: zsh.org; iprev=pass (relay11.mail.gandi.net) smtp.remote-ip=217.70.178.231; dmarc=none header.from=chazelas.org; arc=none Received: from relay11.mail.gandi.net ([217.70.178.231]:38779) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1lnlqP-000G7h-SZ; Mon, 31 May 2021 17:36:54 +0000 Received: (Authenticated sender: stephane@chazelas.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 8F81A100003; Mon, 31 May 2021 17:36:52 +0000 (UTC) Date: Mon, 31 May 2021 18:36:51 +0100 From: Stephane Chazelas To: =?utf-8?B?UmVuw6k=?= Neumann Cc: zsh-users@zsh.org Subject: Re: [Review Request] Arrays and their usage Message-ID: <20210531173651.2pfklkhv5zizfldg@chazelas.org> Mail-Followup-To: =?utf-8?B?UmVuw6k=?= Neumann , zsh-users@zsh.org References: <26fc6ecd-4a05-e562-7274-ba045df536bf@necoro.eu> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <26fc6ecd-4a05-e562-7274-ba045df536bf@necoro.eu> X-Seq: 26752 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: 2021-05-31 01:24:36 +0200, René Neumann: [...] > I always feel a little unsure around arrays in zsh. I've currently used the > following: > > local pkgs=( `makepkg --printsrcinfo | \ > sed -n -e 's/pkgname = \(.*\)$/\1/p'` ) Here's my take on answering this (repeating some of what has already been said). First, I'd say: `...` form of command substitution should really be banned these days. That's really a broken heritage from the Bourne shell. There's not good reason to keep using it these days. The main problem with it is the handling of backslash inside it (and the awkward nesting and the fact that it's less legible, etc...). The sed command could be written: sed -n 's/pkgname = //p' Strictly speaking, it's not exactly equivalent with some sed implementations when the input contains sequences of bytes that don't form valid text in the current locale. For instance, in a locale that uses UTF-8 as its charmap, on the output of: printf 'foopkgname = bar\200baz\n' yours would output foobar while mine would output foobar<0x80>baz with GNU sed as the . regexp operator only matches on *characters* so would not match on that 0x80 byte which doesn't form a valid character in UTF-8. Then, leaving `...` (or the better $(...)) unquoted performs IFS-splitting, so you're left with the same kind of conundrum as you get in POSIX shells when you leave any form of expansion ($param, $((arith)) as well there!) unquoted though at least zsh doesn't perform globbing there: either you're happy that the default value of IFS (space, tab, newline, nul) is good enough for all splitting, or you need to set it every time you use it (in zsh, that's for unquoted $(...) or the $=param operator). Here, you can do pkgs=( $( makepkg --printsrcinfo | sed -n 's/pkgname = //p' ) ) With the default value of $IFS if you know the values don't contain any of the $IFS characters. If that can't be guaranteed, you'd need: IFS=$'\n' pkgs=( $( makepkg --printsrcinfo | sed -n 's/pkgname = //p' ) ) But in zsh, rather than using IFS-splitting which is cumbersome to use as it relised on a global parameter, you can use explicit splitting operators, using the "f" (short for "ps:\n:") parameter expansion flag. Then you don't have to worry about what $IFS may contain at the time: pkgs=( ${(f)"$( makepkg --printsrcinfo | sed -n 's/pkgname = //p' )"} ) Here, we're quoteing the $(...) to disable IFS-splitting and use the "f" flag to do splitting on line feeds. Note that empty elements are discarded. > pkgs=(${pkgs/#/"$DATABASE/"}) The more idiomatic zsh variant to that ksh syntax would be: pkgs=( $DATABASE/$^pkgs ) (same as rc's pkgs = ( $DATABASE/$pkgs )). -- Stephane