zsh-users
 help / color / mirror / code / Atom feed
From: "Lawrence Velázquez" <larryv@zsh.org>
To: zsh-users@zsh.org
Subject: Re: optimal expansions?
Date: Fri, 19 Apr 2024 16:40:48 -0400	[thread overview]
Message-ID: <c30661ec-b838-4e81-9be9-559761da224b@app.fastmail.com> (raw)
In-Reply-To: <53fab6be-26d7-4de5-844f-ffc295d9a494@eastlink.ca>

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


  reply	other threads:[~2024-04-19 20:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-19 19:22 Ray Andrews
2024-04-19 20:40 ` Lawrence Velázquez [this message]
2024-04-19 23:25   ` Ray Andrews
2024-04-20  7:42 ` Roman Perepelitsa
2024-04-20 14:23   ` Ray Andrews
2024-04-20 22:54     ` Lawrence Velázquez
2024-04-20 23:59       ` Ray Andrews
2024-04-21 12:23     ` Roman Perepelitsa
2024-04-21 14:09       ` Ray Andrews
2024-04-21 14:19         ` Roman Perepelitsa
2024-04-21 16:14           ` Stephane Chazelas
2024-04-21 17:39             ` Roman Perepelitsa
2024-04-21 20:13               ` Stephane Chazelas
2024-04-21 20:46                 ` Lawrence Velázquez

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=c30661ec-b838-4e81-9be9-559761da224b@app.fastmail.com \
    --to=larryv@zsh.org \
    --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).