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

Re: associative array questions




On 2022-12-13 12:06, Roman Perepelitsa wrote:
`set` is a builtin (not a reserved word), so you use it like a normal
command. Your first email in this thread used it correctly. You can
also refer to `man zshbuiltins` for documentation.

Nuts, I didn't even consider the problem being the equal sign. One gets used to using it in assignment, it looks strange without it.  But yes, referring back, I see the mistake.

set -A $list "${(@M)${(P)list}:#(#i)*$SEARCHBUF*}"

... that seems to be working.  BTW I know it's a builtin and it seems to me I used it like a normal command even if mistakenly, how not?

Bart:

You missed where I said "if [$list in this case] names an array, you
can only assign to the entire array"?

It's nothing but the contents of a file broken down into lines. Yes, I'm assigning the whole thing.  This little piece of ... ah, that's where the confusion comes in -- at this juncture I'm not worried about the entire A-A, just the data array, namely the contents of a file.  So I suspect we're talking apples and oranges.  Subtle change in the topic, pardon! Anyway ... :

eval 'list=( "${(@M)list:#(#i)*$SEARCHBUF*}" )'     # filter the array.

That second line isn't doing anything like what you want.

But it does work perfectly. Remembering of course that there could be some lurking gotcha, but it *seems* fine.

You don't
have the (P) flag in there anywhere to use the value of $list as a
parameter name, and you're just assigning that back to $list instead
of to the name from $1.  The eval accomplishes nothing because the
whole thing being eval'd is in single quotes to begin with; you might
as well drop both the single quotes and the word eval.

Can't say much, only to report that it does work. If I drop the eval and the outer quotes it does nothing, the original array is not changed. As I think I understand it, the second "list" simply becomes the name of the source array and, as I said, eval 'just does it' -- accesses the named array via it's name exactly as I'd intuitively want it to do.  So the '(P)' isn't needed.  Seems so, anyway. But I've been known to completely misunderstand things.

set -A $list "${(P@M)list:#(#i)*$SEARCHBUF*}"

is what Roman is driving at.

Right, as above, I ... ah ... well maybe I didn't get it perfect, I don't have the '(P@M)' just '(P)' ... but so far so good. Once I dropped the equal sign I got it right the first time.







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