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

Re: uninvited members of associative array



On Thu, Dec 15, 2022 at 08:16:46PM -0800, Ray Andrews wrote:
>
> On 2022-12-15 19:21, Lawrence Velázquez wrote:
> > Or before.
> Exactly the case here.  This code has many global variables and ... well, no
> need to say more.
> > Wrong.  It assumed that "main" contained the expected data and
> > nothing extra.  I've already asked you to use "typeset -p" to verify
> > its actual contents, but you still haven't.
>
> No need, zeroing the array solves the issue. Mind, I suppose I could do a
> forensic on that, and figure out when and where the corruption occurs

The issue is not solved.  You still have something that adds the
bogus values before the reinitialisation.  Whether this has some
adverse effects depends on the scripts.  (Sample script attached).
Basically you probably do ~ this:

  init_main
  # ...
  main=($main)           <-- Creates extra values
  # ...
  init_main
  printf "%-20s %s\n" ${(kv)main}

This is a bug in your scripts and not in zsh.

> and then, of course I'll use typeset -p

Forget about the typeset, look for "main=$(main)" or similar.

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt
#!/usr/bin/zsh

emulate zsh

List="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
mainWH="50"
mainWW="92"

typeset -A main

init_main () {
	main[window]="main"
	main[list]='List'
	main[lastE]="$#List"
	main[hight]=$(( mainWH - 2 ))
	main[width]=$(( mainWW - 2 ))
	main[bottomE]="$main[hight]"
	main[topE]=1
	main[currentE]=1
	main[offset]=0
	main[active]=1; echo ${(kv)main}
}

init_main
main=($main)
init_main
printf "%-20s %s\n" ${(kv)main}


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