Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: read/write associative array from/to file
- X-seq: zsh-users 16689
- From: Jérémie Roquet <arkanosis@xxxxxxxxx>
- To: Thomas Gstädtner <thomas@xxxxxxxxxxxxxx>
- Subject: Re: read/write associative array from/to file
- Date: Tue, 3 Jan 2012 10:40:12 +0100
- Cc: zsh-users@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=rIJjqqojB5kuaHQzt9HjinMb6kSrVETnM9kNP3+5Kuo=; b=VJNkHn2v4Lu4qjee1iNnUcP03mSNa+aqtiT/MZ1TL6yZD3YAUYUX8sQJ/T36VkMSEy 98XPIP0PZ2MYy+kBUOy5dzC9Yx7IgTGJJzmCZ+I6bb662VtW0qywfXe/NJjz6pH6+yud Rf2/bR8MKZkLC/uUUJqjE7cimwd5993c45Mzw=
- In-reply-to: <4F0263FB.2000202@gstaedtner.net>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <4F0263FB.2000202@gstaedtner.net>
Hi Thomas,
2012/1/3 Thomas Gstädtner <thomas@xxxxxxxxxxxxxx>:
> 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?
> If not, what would be the most practical solution to work with a file
> like this?
> ---
> key1 value1
> key2 value2
> key3 value3
> ...
If 1) you're confident about the content of your database (ie. nobody
can write nasty things in it) and 2) you do not plan to have a lot of
entries in your database and 3) concurrency and performance is not an
issue, an easy solution is to serialize your array in a shell script
and to source this script to reload the database:
# dummy associative array
typeset -A myArray
myArray=(
foo 1
bar 42
)
# serialization
echo "myArray=(${(kv)myArray})" >| $db
# loading (don't forget to typeset -A myArray before this)
source $db
Best regards,
--
Jérémie
Messages sorted by:
Reverse Date,
Date,
Thread,
Author