zsh-users
 help / color / mirror / code / Atom feed
From: Ray Andrews <rayandrews@eastlink.ca>
To: zsh-users@zsh.org
Subject: Re: triviality regarding $# counts
Date: Fri, 12 Apr 2024 07:48:50 -0700	[thread overview]
Message-ID: <b6ad16ad-a569-4925-8b71-1bf0b78b7c11@eastlink.ca> (raw)
In-Reply-To: <e893a082-4b27-4588-9062-ee83ed4ff32d@app.fastmail.com>

[-- Attachment #1: Type: text/plain, Size: 3005 bytes --]



On 2024-04-11 21:55, Lawrence Velázquez wrote:

> 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.
I don't trust it neither, and I taught Rube everything he knows. 
Seriously, you won't sympathize, but the tinkerer's approach was the 
only one available to me at least until I had enough ad hoc code built 
up that sorta works to begin to start to try to understand it formally.  
And my efforts to find patterns of behavior -- as you know better than 
anyone -- lead me to false conclusions all the time.  The manual is 
useless as pedagogy.  If there was 'zsh school' I'd enroll and learn my 
shell ABC's properly from the ground up. But there isn't.  So I hack 
away.  But please understand I *hate* being me.  I like competence :(
> 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.
Right, I'm aware of that possibility at least in theory.  Word splitting 
lurks.
> 	% 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 )
It will take me a long time to understand that.

> 	% 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
> 	%
I think I can chew on that, backatcha later.

> You both populate and print your array incorrectly. For the umpteenth
> time, you should use "typeset -p" to inspect your variables' values.
>
I keep typeset -p close at all times now.  But there are times when I'm 
still baffled.  Like an alchemist.  Sheesh, just yesterday I was 
attempting to transform this:

typeset -g var="
abc

     def ghi
     jkl mno

     pqr
"

... into an array that retained the same 'shape' -- that prints verbatim 
-- and typeset -p showed newlines being transubstantiated into dollar 
signs!  God knows how.

For now, what I'm wishing for is a nice, focused essay: "How to use '$#' 
to count whatever it is you want to count -- characters, words, lines, 
paragraphs (at one point my count was '3' which I'd approximate to being 
a paragraph) ... and all with or without spaces and/or empty lines. " 
Should run about  ten pages but it would be everything there is to know 
about '$#'.


[-- Attachment #2: Type: text/html, Size: 4464 bytes --]

  reply	other threads:[~2024-04-12 14:49 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-11  0:56 Ray Andrews
2024-04-12  4:55 ` Lawrence Velázquez
2024-04-12 14:48   ` Ray Andrews [this message]
2024-04-12 19:09     ` Bart Schaefer
2024-04-13  1:13       ` Ray Andrews
2024-04-13  1:33         ` Mark J. Reed
2024-04-13  2:28           ` Ray Andrews
2024-04-13  3:25             ` Lawrence Velázquez
2024-04-13 14:37               ` Ray Andrews
2024-04-13 15:14                 ` Ray Andrews
2024-04-13 17:19                   ` Mark J. Reed
2024-04-13 17:27                     ` Mark J. Reed
2024-04-13 18:08                       ` Ray Andrews
2024-04-13 19:45                         ` Bart Schaefer
2024-04-13 20:36                           ` Ray Andrews
2024-04-13 21:01                             ` Bart Schaefer
2024-04-14  0:28                               ` Ray Andrews
2024-04-14  0:30                               ` Lawrence Velázquez
2024-04-14  3:26                                 ` Ray Andrews
2024-04-14  3:49                                   ` Lawrence Velázquez
2024-04-14  4:57                                     ` Bart Schaefer
2024-04-14 13:24                                       ` Ray Andrews
2024-04-14 13:35                                         ` Roman Perepelitsa
2024-04-14 14:06                                           ` Ray Andrews
2024-04-14 14:15                                             ` Roman Perepelitsa
2024-04-14 14:53                                               ` Ray Andrews
2024-04-14 15:11                                                 ` Mark J. Reed
2024-04-14 16:23                                                   ` Ray Andrews
2024-04-14 14:06                                         ` Mark J. Reed
2024-04-14 14:47                                           ` Ray Andrews
2024-04-14 14:59                                             ` Mark J. Reed
2024-04-14 15:51                                         ` Bart Schaefer
2024-04-14 17:22                                           ` Ray Andrews
2024-04-14 17:42                                             ` Mark J. Reed
2024-04-14 18:24                                               ` Bart Schaefer
2024-04-14 22:00                                               ` Ray Andrews
2024-04-13 20:11                         ` Mark J. Reed
2024-04-13 20:53                   ` Bart Schaefer
2024-04-14  0:19                     ` Ray Andrews
2024-04-13  1:35         ` Bart Schaefer

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=b6ad16ad-a569-4925-8b71-1bf0b78b7c11@eastlink.ca \
    --to=rayandrews@eastlink.ca \
    --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).