From owner-freebsd-hackers@FreeBSD.ORG Fri Oct 31 12:17:02 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id AC1BD16A4CE for ; Fri, 31 Oct 2003 12:17:02 -0800 (PST) Received: from lurza.secnetix.de (lurza.secnetix.de [195.143.231.20]) by mx1.FreeBSD.org (Postfix) with ESMTP id C039943FB1 for ; Fri, 31 Oct 2003 12:16:59 -0800 (PST) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (dazwtc@localhost [127.0.0.1]) by lurza.secnetix.de (8.12.9p1/8.12.8) with ESMTP id h9VKGvOC006951 for ; Fri, 31 Oct 2003 21:16:57 +0100 (CET) (envelope-from oliver.fromme@secnetix.de) Received: (from olli@localhost) by lurza.secnetix.de (8.12.9p1/8.12.8/Submit) id h9VKGv8X006950; Fri, 31 Oct 2003 21:16:57 +0100 (CET) Date: Fri, 31 Oct 2003 21:16:57 +0100 (CET) Message-Id: <200310312016.h9VKGv8X006950@lurza.secnetix.de> From: Oliver Fromme To: freebsd-hackers@FreeBSD.ORG In-Reply-To: <20031031014331.A71967@xorpc.icir.org> X-Newsgroups: list.freebsd-hackers User-Agent: tin/1.5.4-20000523 ("1959") (UNIX) (FreeBSD/4.8-RELEASE (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Subject: Re: RFC: proposed new builtin for /bin/sh (associative arrays) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: freebsd-hackers@FreeBSD.ORG List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Oct 2003 20:17:02 -0000 Luigi Rizzo 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)