From: "Jérémie Roquet" <arkanosis@gmail.com>
To: "Thomas Gstädtner" <thomas@gstaedtner.net>
Cc: zsh-users@zsh.org
Subject: Re: read/write associative array from/to file
Date: Tue, 3 Jan 2012 10:40:12 +0100 [thread overview]
Message-ID: <CAFOazAP6U0sJEGs6j+BBNashbaqsjB0Ni4iGE0ziq6Ks68njPg@mail.gmail.com> (raw)
In-Reply-To: <4F0263FB.2000202@gstaedtner.net>
Hi Thomas,
2012/1/3 Thomas Gstädtner <thomas@gstaedtner.net>:
> 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
next prev parent reply other threads:[~2012-01-03 9:46 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-03 2:12 Thomas Gstädtner
2012-01-03 9:40 ` Jérémie Roquet [this message]
2012-01-03 17:02 ` Wayne Davison
2012-01-05 5:20 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAFOazAP6U0sJEGs6j+BBNashbaqsjB0Ni4iGE0ziq6Ks68njPg@mail.gmail.com \
--to=arkanosis@gmail.com \
--cc=thomas@gstaedtner.net \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).