Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: counting trouble



On 04/04/18 06:15 PM, Bart Schaefer wrote:
On Wed, Apr 4, 2018 at 11:21 AM, Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

That can't be all there is to it.  $#array only becomes a count of
characters if the context forces the array into string form.  Which
admittedly can happen in some non-obvious ways, but what you've shown
above is not one of those ways.  What are you really doing?
I'll take another look, that's a good clue anyway.

Hacking away I find this works:

     [ -e "$1" ] && tmp=( "${(f)${1}}" ) || tmp=( ${1}*(N) )
Umm ...  [ -e "$1" ] will only succeed for a single literal file name,
Exactly the point of it.   I found I had to handle an exact match specially.  I'm sure there's a better way.
expected this to work: " ${(f)${1}*} " but it doesn't.
Expected it to do what?  Filename generation like ${1}* out in the
open (so to speak)?
Yeah, but as I said, I've figured out that that won't happen, it's not a glob 'inside'.  So how would I get the results of the glob 'inside' as well as the single match not counting characters? .... but you've just said that shouldn't be happening, so I'll follow that clue.

What led you to expect that?  In any case
filename generation would never produce a newline-separated string
(again barring files with newlines in the name), so (f) would not do
anything to the result.

Please don't think about globbing as returning "lines".  It doesn't.

But:

   tmp=( ${1}*(N) )

... seems to give me exactly that -- an array split on lines with no worries about spaces in filenames.  And '$#' counts the number of files just as I want.

  print -l "${tmp[@]}\n"

... does exactly what I'm expecting (bottom example):

15 /aWorking/Zsh/Source/Wk 3 $ . try; find_match try,12
Exact match found: "try,12"

15 /aWorking/Zsh/Source/Wk 3 $ . try; find_match try,1
Unique match found: "try,1"

15 /aWorking/Zsh/Source/Wk 3 $ . try; find_match try,14
Exact match found: "try,14,interim"

15 /aWorking/Zsh/Source/Wk 3 $ . try; find_match try,*

ERROR: No wildcards please

15 /aWorking/Zsh/Source/Wk 3 $ . try; find_match try,

ERROR: 18:: 15 matches for "try,", please disambiguate or copy manually:
try,1
try,10,working
try,11,very nice, help text
try,12
try,13
try,14,interim
try,15,important noglob and messages
try,2,exact match handled
try,3,now add declare
try,4,no ls
try,5,shakeout bugs
try,6,interim safe
try,7,now with find_match
try,8
try,9,working
===========================================

What would be manna from heaven is to be able to look at an array and 'see' the construction of it -- how it's split, if one could see the newlines and/or spaces in both input and output that would be huge.  Even after several years I still find myself throwing syntax at things hopefully but almost randomly until eventually something works.  If I could see the hidden structure of both input and output there'd be a lot less guessing.




Messages sorted by: Reverse Date, Date, Thread, Author