* better grammar with $#
@ 2022-10-30 14:16 Ray Andrews
2022-10-30 17:21 ` Philippe Troin
2022-10-30 17:29 ` Roman Perepelitsa
0 siblings, 2 replies; 4+ messages in thread
From: Ray Andrews @ 2022-10-30 14:16 UTC (permalink / raw)
To: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 845 bytes --]
output=$( eval "$@" ) # Input here is a 'find' command outputting
a list of files.
tmp=( ${(f)output} ) # Count lines not characters.
linecount=$#tmp
... it seems clumsy to create 'tmp' just to count lines, can that be a
bit more streamlined? And is it lines or words there? The output is
filenames so words=lines in this case but maybe not in other cases so
I'd like to be sure to have a count of lines. Easy to get one's
splitting wrong.
BTW the ultimate in minutiae but the manual says:
12 Conditional Expressions
A /conditional expression/ is used with the [[ compound command to test
attributes of files and to compare strings. Each expression can be
constructed from one or more of the following unary or binary expressions:
... and AFAICT the tests work fine with the single '[' as well.
[-- Attachment #2: Type: text/html, Size: 1165 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: better grammar with $#
2022-10-30 14:16 better grammar with $# Ray Andrews
@ 2022-10-30 17:21 ` Philippe Troin
2022-10-30 17:29 ` Roman Perepelitsa
1 sibling, 0 replies; 4+ messages in thread
From: Philippe Troin @ 2022-10-30 17:21 UTC (permalink / raw)
To: Ray Andrews, Zsh Users
[-- Attachment #1: Type: text/plain, Size: 623 bytes --]
On Sun, 2022-10-30 at 07:16 -0700, Ray Andrews wrote:
>
> output=$( eval "$@" ) # Input here is a 'find' command
> outputting a list of files.
> tmp=( ${(f)output} ) # Count lines not characters.
> linecount=$#tmp
> ... it seems clumsy to create 'tmp' just to count lines, can that be
> a bit more streamlined? And is it lines or words there? The output
> is filenames so words=lines in this case but maybe not in other cases
> so I'd like to be sure to have a count of lines. Easy to get one's
> splitting wrong.
How about:
linecount=${#${(f)"$(eval "$@")"}}
Phil.
[-- Attachment #2: Type: text/html, Size: 1058 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: better grammar with $#
2022-10-30 14:16 better grammar with $# Ray Andrews
2022-10-30 17:21 ` Philippe Troin
@ 2022-10-30 17:29 ` Roman Perepelitsa
2022-10-30 17:49 ` Ray Andrews
1 sibling, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2022-10-30 17:29 UTC (permalink / raw)
To: Ray Andrews; +Cc: Zsh Users
On Sun, Oct 30, 2022 at 3:17 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>
> output=$( eval "$@" ) # Input here is a 'find' command outputting a list of files.
> tmp=( ${(f)output} ) # Count lines not characters.
> linecount=$#tmp
You can combine the two expansions:
linecount=${#${(f)output}}
> And is it lines or words there?
It's lines.
If you can replace the `find` command with a glob, it'll be simpler.
You'll also avoid quoting issues. If your code is supposed to handle
all sorts of file names, make sure it can handle files with backslash
and line feed in their names.
Roman.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: better grammar with $#
2022-10-30 17:29 ` Roman Perepelitsa
@ 2022-10-30 17:49 ` Ray Andrews
0 siblings, 0 replies; 4+ messages in thread
From: Ray Andrews @ 2022-10-30 17:49 UTC (permalink / raw)
To: zsh-users
On 2022-10-30 10:29, Roman Perepelitsa wrote:
>
> You can combine the two expansions:
>
> linecount=${#${(f)output}}
Nuts, I came close I tried: $#{${(f)output}}
If you can replace the `find` command with a glob, it'll be simpler.
Yeah, I'm migrating in that direction already.
> You'll also avoid quoting issues. If your code is supposed to handle
> all sorts of file names, make sure it can handle files with backslash
> and line feed in their names.
God knows. I tend to crash into problems like that when they happen and
deal with it at the time. I wish there was some option to protect
strings from all such issues, all the time, everywhere.
>
> Roman.
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-10-30 17:50 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-30 14:16 better grammar with $# Ray Andrews
2022-10-30 17:21 ` Philippe Troin
2022-10-30 17:29 ` Roman Perepelitsa
2022-10-30 17:49 ` Ray Andrews
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).