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=-1.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI 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 D24F624D47 for ; Fri, 19 Apr 2024 22:43:48 +0200 (CEST) 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:Subject:To:From:Date: References:In-Reply-To:Message-Id: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; bh=6zAUfYKGU1plqhLBRf+w2DCUHDowcvd//+VCGqy9c18=; b=Hn49cRe4huR5lcUdAvb2Ihp03p eESI8YuxIt53QttOQujn6VKgApllVZKmVpMzlQ8z4hIQxahv5Kq4OJDT/Wc6OQCIEPGwrVpgQDEDW MoblY8Wa2BCEFZMnoZMfbMuFzSLZWZ9ucksvShryx/+5aKtZ6JTyFs4Buh/R6XwAMe8/84aF2c6uB HUP0obk9gopdpQdYb9rxAIke/9YvAadeq3YsG0GAI5XGnqP+BlbHAVDI5v0GCCN1wjfoqmvlxqf1/ bc9rfsZSnoKrIUi1/7pxKUTneTga/pza+x8jowQv4yfm+FRzlnnQCTzLKa6hvEC6dc8+HOMzwALM5 pWccxUyA==; Received: by zero.zsh.org with local id 1rxv5G-000KUo-Th; Fri, 19 Apr 2024 20:43:47 +0000 Received: by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1rxv46-000JTG-UU; Fri, 19 Apr 2024 20:42:35 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 557FC1200032 for ; Fri, 19 Apr 2024 16:42:33 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute2.internal (MEProxy); Fri, 19 Apr 2024 16:42:33 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudekvddgudehhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvffutgesth dtredtreerjeenucfhrhhomhepnfgrfihrvghntggvucggvghljoiiqhhuvgiiuceolhgr rhhrhihvseiishhhrdhorhhgqeenucggtffrrghtthgvrhhnpeeileevheefgffhffeihe dtieegvdfgkeffteejgfetledtiedviedvteeukeegkeenucevlhhushhtvghrufhiiigv pedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihvhdomhgvshhmthhprghuth hhphgvrhhsohhnrghlihhthidqudduhedukeejjedtgedqudduledvjeefkeehqdhlrghr rhihvheppeiishhhrdhorhhgsehfrghsthhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: iaa214773:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 1FFE831A0065; Fri, 19 Apr 2024 16:42:33 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-379-gabd37849b7-fm-20240408.001-gabd37849 MIME-Version: 1.0 Message-Id: In-Reply-To: <53fab6be-26d7-4de5-844f-ffc295d9a494@eastlink.ca> References: <53fab6be-26d7-4de5-844f-ffc295d9a494@eastlink.ca> Date: Fri, 19 Apr 2024 16:40:48 -0400 From: =?UTF-8?Q?Lawrence_Vel=C3=A1zquez?= To: zsh-users@zsh.org Subject: Re: optimal expansions? Content-Type: text/plain X-Seq: 29839 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: On Fri, Apr 19, 2024, at 3:22 PM, Ray Andrews wrote: > That's my preferred way to look at 'apt-file search' output (Debian and > derivatives only of course). It works fine and I think I understand > all the expansions and splitting. One you get used to it the nested > expansions aren't so scary, just read them from inside out, one step at > a time and it's easy. But is it optimal? How is anyone supposed to answer this question, when you haven't deigned to mention what your code is supposed to DO? You seem to colorize certain portions of the output by bracketing them with escape sequences, but which portions, exactly? What does "apt-file search" typically output? Without knowing more about the bigger picture, the best we can do is nitpick your code without addressing its larger structural issues. (Unless someone wants to guess about your intentions.) > Script: > > grn=$'\e[32;1m' > nrm=$'\e[0m' > > var=( "${(@f)$(apt-file search $1)}" ) You don't check the exit status of "apt-file", so if it happens to fail for any reason, your code plows on obliviously, printing nothing and exiting with a zero status. You should do something like var=( "${(@f)$(apt-file search -- $1)}" ) || return (assuming this is all in a function, since you have yet again chosen to not provide any context) > targ= > var2=() > > for ((i=1; i<=$#var; i++ )); do Since you only ever use "i" in the expansion "${=var[i]}", there is no reason to use this form of "for". You can just use the usual for x in "$var[@]"; do and subsequently "$x" instead of "$var[i]". > if [[ "$targ" != "${${=var[i]}[1]}" ]]; then > targ="${${=var[i]}[1]}" > var2+="\n${grn}${${=var[i]}[1]}${nrm}" # Copy first word of > line. You're doing that thing again, where you use a literal backslash-n and rely on "print" to interpret it as a newline. This is bad practice here because the rest of the string is the arbitrary output of an external command, which you do not control. It could easily contain substrings that are meaningful to "print". To insert an empty line in your output, just add an empty element to "var2" in the desired position. % var=(a) % var+= % var+=b % typeset -p var typeset -a var=( a '' b ) % print -rC1 -- "$var[@]" a b > fi > var2+="${${=var[i]}[2,-1]}" # Copy the rest of the line no > matter how many words. You should store the result of "${=var[i]}" in a temporary variable and use that, instead of repeatedly word-splitting the same string over and over and over. > done > > print -l "$var2[@]" Use "print -r" to prevent "print" from interpreting escape sequences in its arguments. As I mentioned in a previous email, if "var2" has no elements, "print -l" will print one empty line anyway. Use "print -C1" instead. print -rC1 -- "$var2[@]" -- vq