bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Is there a way to access associative array from the envir


From: Stephane Chazelas
Subject: Re: [bug-gawk] Is there a way to access associative array from the environment?
Date: Thu, 10 Mar 2016 06:33:02 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2016-03-09 22:31:45 -0600, Peng Yu:
> Hi, The following command will not access "a" in $MYARRAY. Is there a
> way to do so in awk?
> 
> declare -A MYARRAY
> MYARRAY=([a]=x [b]=y)
> awk -e 'BEGIN { print ENVIRON["MYARRAY"]["a"] }'
[...]

The environment is just a list of strings conventionaly in the
format "var=value" passed to executed commands, like the list of
arguments.

associative arrays are a data type of some shells (ksh93, zsh
and recent versions of bash). They are different in all 3 shells
though a few things in common.

Though you could imagine associative arrays of one or more of
those shells could be encoded one way or another so that they be
put in an environment string, it's not currently done, and even
if it was, applications like awk would have to be modified to be
able to understand and decode those variables.

Shells like rc do allow exporting array variables with some
encoding, but those exported variables are only understood by
other rc instances, not awk or anything else:

$ rc -c "zzz=(foo bar); awk 'BEGIN{print ENVIRON[\"zzz\"]}'" | sed -n l
foo\001bar$

All awk sees is the encoded version (here using byte 1 as
separator and byte 2 as an escape character).

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]