Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 31 Oct 2003 21:16:57 +0100 (CET)
From:      Oliver Fromme <olli@lurza.secnetix.de>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: RFC: proposed new builtin for /bin/sh (associative arrays)
Message-ID:  <200310312016.h9VKGv8X006950@lurza.secnetix.de>
In-Reply-To: <20031031014331.A71967@xorpc.icir.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Luigi Rizzo <rizzo@icir.org> wrote:
 > I am trying to implement, in the most unintrusive way, something
 > resembliung associative arrays in /bin/sh.  I am not interested in
 > syntactic sugar, so i am happy to use something like _ as a separator
 > between the array basename and the "index", i.e.
 > 
 >         foo_red foo_green foo_blue
 > 
 > would be part of the same array. And, as a first step, I am also
 > happy to be limited to [0-9A-Za-z_]+ as "index" values.

It would be cool to have real associative arrays like in
zsh, i.e. ${foo[red]}.  Would be even backwards-compatible
without breaking existing scripts.

Well, wishful thinking ...  :-)

(Personally, when I needed to do things with associative
arrays, I wrote that part of the shell script in awk, which
supports associative arrays natively, cleanly, _and_ it is
portable.)

 > So all it was necessary was a command to enumerate the indexes
 > of an array, which is implemented by the attached patch, and
 > can be used as follows:
 > 
 >         for i in `indexes foo_`
 >         do
 >                 eval x=\$foo_$i
 >                 echo "variable foo_$i has value $x"
 >         done

Maybe I'm not understanding your intentions, but isn't that
already possible using "set | sed -n '/^foo_/s/=.*//p'"?

Or do you want to avoid external programs?  In that case it
would be a little bit more difficult to do, but it's still
possible to do with existing builtins:

indexes()
{
    for i in `set`; do
        case $i in
            $1*) echo ${i%%=*}
        esac
    done
}

Then you could use `indexes foo_` like above.  No patch to
/bin/sh necessary.

 > (basically, "indexes xyz" lists the remaining part of all variable
 > names that start with xyz. As a possibly useful side effect,
 > indexes "" lists all variable names, which i believe is not an
 > available sh function -- and i find it strange since we can
 > list the names of readonly and export-ed variables with the
 > "readonly" and "export" builtins).

Well, "set" (without arguments) lists all variables and
their values.  You can easily filter the variable names
from it.

 > Any comments ? Is this interesting enough to be committed
 > (with a proper manpage description) ?

My personal opinion (please don't kill me):  It appears to
me to be a "dirty hack" with very limited usefulness.  If
associative arrays are to be implemented, it should be done
properly (like in zsh, for example).

Regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co KG, Oettingenstr. 2, 80538 München
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"Clear perl code is better than unclear awk code; but NOTHING
comes close to unclear perl code"  (taken from comp.lang.awk FAQ)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200310312016.h9VKGv8X006950>