Parker Coates
Parker Coates

Reputation: 9418

Can one read input into a element of a associative array?

I want to use read to put data into a particular element of an associative array, so I wrote the following:

typeset -Ag aa
aa[key]='initial value'
...
read aa[key]

which produces an error: no matches found: aa[key]

So I write the following instead and it works:

typeset -Ag aa
aa[key]='initial value'
...
local line
read line
aa[key]=line

Is there a way to skip the temporary variable line?

Upvotes: 2

Views: 162

Answers (1)

hchbaw
hchbaw

Reputation: 5319

It seems that zsh trys to do globbing somehow for the aa[key] token.

So, it would be fine to turn off globbing temporally using noglob or quoting that token.

echo val0 | noglob read aa[key]; echo $aa[key] ;# => val0

echo val1 | read 'aa[key]'; echo $aa[key] ;# => val1

noglob

Filename generation (globbing) is not performed on any of the words.

-- zshmisc(1) Precommand modifires

Upvotes: 1

Related Questions