From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14412 invoked by alias); 5 Jan 2012 05:20:40 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16698 Received: (qmail 25752 invoked from network); 5 Jan 2012 05:20:28 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <120104212012.ZM13664@torch.brasslantern.com> Date: Wed, 04 Jan 2012 21:20:12 -0800 In-reply-to: <4F0263FB.2000202@gstaedtner.net> Comments: In reply to Thomas Gstadtner "read/write associative array from/to file" (Jan 3, 3:12am) References: <4F0263FB.2000202@gstaedtner.net> In-reply-to: Comments: In reply to Wayne Davison "Re: read/write associative array from/to file" (Jan 3, 9:02am) X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: read/write associative array from/to file MIME-version: 1.0 Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: quoted-printable On Jan 3, 3:12am, Thomas Gstadtner wrote: } } I want to read and write an associative array from/to a file, i.e. a } very simple key-value database. Is this possible; if so, how? Zsh doesn't have perl-style DBM bindings if that's what you're after, though it should be possible to write that as a module if someone is feeling ambitious. } If not, what would be the most practical solution to work with a file } like this? } --- } key1 value1 } key2 value2 } key3 value3 } ... } --- Assuming that the only space is the one that separates keys from values on every line, you can populate an associative array like so: tyepset -A dbary dbary=3D( ${=3D${(f)"$( wrote: }=20 } > echo "myArray=3D(${(kv)myArray})" >| $db }=20 } That works if there are no spaces or special characters involved. You } could change that line to remove the double quotes and add (q) or (q-) to } the expansion, and it would handle those: }=20 } typeset -A myArray } myArray=3D( } foo 1\ is\ the\ loneliest\ number } bar 42\ isn\'t\ really\ the\ ultimate\ answer } ) } echo myArray=3D\(${(kvq-)myArray}\) >/tmp/myArray Wayne is correct here, but the easiest way to store/reload a parameter to/from a file is with "typeset -p": typeset -p dbary > dbfile writes a well-formed declaration and assignment to dbfile, which can be read back with source dbfile As was mentioned, this assumes that you can write the dbfile to a secure location and be confident that no one tampers with it before you source it back again.