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.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,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 DB15F21647 for ; Fri, 12 Apr 2024 06:58:44 +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=DJINYvxOpo1SfB2vwlGm3slCTejMyVYUdanbxFK1jr8=; b=JsE6IBX8fuL85IBnkCKa2VQiP5 mPKbgccIM6bhwt+2u5fRWMIAFYZCLLzozPA0SKvmtOMT3I44xutG1l7JPRuFdz/cc7PWLyvHLens4 vkjPRNErOMosH1n/3kV+GPN7IjZX/gZcU9d140NRnRZQzmnqGTjt+kF5yDU2vhVv081DpmgW7K63E 2X18a8DlkJPJu61FBKn8+5ZDR8u588rpXUNvwpw8WnWVWvOtmOXu7bOAIJpojarMLz+p32UXbmvUx 7go/wqRSRyXM2wxCmqQ7eaqLedhe6cA0JsIth1wYZGrW05oYhUY/9TTlVw/gBHAFFoLlkyIQ8Yx3r weSK/ecQ==; Received: by zero.zsh.org with local id 1rv8zq-00034X-Rc; Fri, 12 Apr 2024 04:58:42 +0000 Received: by zero.zsh.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1rv8yw-0002Lm-T1; Fri, 12 Apr 2024 04:57:47 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 42B881200066 for ; Fri, 12 Apr 2024 00:57:45 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute2.internal (MEProxy); Fri, 12 Apr 2024 00:57:45 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeitddgheegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsehttd ertderreejnecuhfhrohhmpefnrgifrhgvnhgtvgcugggvlhojiihquhgviicuoehlrghr rhihvhesiihshhdrohhrgheqnecuggftrfgrthhtvghrnhepieelveehfefghfffieehtd eigedvgfekffetjefgteeltdeivdeivdetueekgeeknecuvehluhhsthgvrhfuihiivgep tdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhihvodhmvghsmhhtphgruhhthh hpvghrshhonhgrlhhithihqdduudehudekjeejtdegqdduudelvdejfeekhedqlhgrrhhr hihvpeepiihshhdrohhrghesfhgrshhtmhgrihhlrdgtohhm X-ME-Proxy: Feedback-ID: iaa214773:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id E549331A0065; Fri, 12 Apr 2024 00:57:44 -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: <97793422-1543-4ba6-b52b-ff93eba03ab1@eastlink.ca> References: <97793422-1543-4ba6-b52b-ff93eba03ab1@eastlink.ca> Date: Fri, 12 Apr 2024 00:55:07 -0400 From: =?UTF-8?Q?Lawrence_Vel=C3=A1zquez?= To: zsh-users@zsh.org Subject: Re: triviality regarding $# counts Content-Type: text/plain X-Seq: 29794 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 Wed, Apr 10, 2024, at 8:56 PM, Ray Andrews wrote: > This works: (Again, this is deep in a function and I have to use eval.) If eval really is necessary (to be frank, I don't trust your judgment on this), show us examples that require it, instead of the misleading Rube Goldberg machines you've been offering. > output=$( eval "$@" ) # '$@' will expand to 'print -l $temp' > which is the text above. > temp=( $( eval $@ ) ) # To get the correct count I need to force > an array. > linecount=$#temp > print -rl -- $output > print $linecount The count is correct by accident. Your unquoted command substitution drops the two empty lines but splits the two lines you say should not be split. % orig='abc quote> quote> def ghi quote> jkl mno quote> quote> pqr' % arr=($(print -r -- $orig)) % typeset -p arr typeset -a arr=( abc def ghi jkl mno pqr ) > Various experiments trying to get the correct linecount (23) There are 25 lines, not 23. > Different efforts at quoting or using ' ${(@f) ....} ' and various > other tricks yield me a linecount of 1, 3, 23, 25, 26, or 738. And > output that deletes the blank lines, or forces everything into one > 'line/element'. Basically I need the array form to to get the line > count, but it won't print properly as an array. Not that it's worth > much trouble, but is it possible to get the variable to print > correctly *and* show the count of lines without having to eval it > twice? % cat foo.zsh orig='abc def ghi jkl mno pqr' # The sensible way to split on LFs. #arr=("${(@f)orig}") # A very silly way to split on LFs. Use double quotes to # prevent the result of $(...) from being split and to retain # empty words in the result of ${(@)...}. arr=("${(@f)$(print -r -- $orig)}") typeset -p arr print -r -- $#arr # Use double-quoted $arr[@] to retain empty elements. Use # "print -C1" to avoid printing an empty line if "arr" is empty. print -rC1 -- "$arr[@]" % zsh ./foo.zsh typeset -a arr=( abc '' 'def ghi' 'jkl mno' '' pqr ) 6 abc def ghi jkl mno pqr % > ... as it is, it seems that '$#' never counts the lines of output as it > actually prints. You both populate and print your array incorrectly. For the umpteenth time, you should use "typeset -p" to inspect your variables' values. -- vq