* read/write associative array from/to file @ 2012-01-03 2:12 Thomas Gstädtner 2012-01-03 9:40 ` Jérémie Roquet 0 siblings, 1 reply; 4+ messages in thread From: Thomas Gstädtner @ 2012-01-03 2:12 UTC (permalink / raw) To: zsh-users Hi list, 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 ... --- Thanks in advance, thomasg ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: read/write associative array from/to file 2012-01-03 2:12 read/write associative array from/to file Thomas Gstädtner @ 2012-01-03 9:40 ` Jérémie Roquet 2012-01-03 17:02 ` Wayne Davison 0 siblings, 1 reply; 4+ messages in thread From: Jérémie Roquet @ 2012-01-03 9:40 UTC (permalink / raw) To: Thomas Gstädtner; +Cc: zsh-users 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: read/write associative array from/to file 2012-01-03 9:40 ` Jérémie Roquet @ 2012-01-03 17:02 ` Wayne Davison 2012-01-05 5:20 ` Bart Schaefer 0 siblings, 1 reply; 4+ messages in thread From: Wayne Davison @ 2012-01-03 17:02 UTC (permalink / raw) To: Jérémie Roquet; +Cc: Thomas Gstädtner, zsh-users [-- Attachment #1: Type: text/plain, Size: 567 bytes --] On Tue, Jan 3, 2012 at 1:40 AM, Jérémie Roquet <arkanosis@gmail.com> wrote: > echo "myArray=(${(kv)myArray})" >| $db > 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: typeset -A myArray myArray=( foo 1\ is\ the\ loneliest\ number bar 42\ isn\'t\ really\ the\ ultimate\ answer ) echo myArray=\(${(kvq-)myArray}\) >/tmp/myArray typeset -A myArray source /tmp/myArray echo ${(q-)myArray[bar]} ..wayne.. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: read/write associative array from/to file 2012-01-03 17:02 ` Wayne Davison @ 2012-01-05 5:20 ` Bart Schaefer 0 siblings, 0 replies; 4+ messages in thread From: Bart Schaefer @ 2012-01-05 5:20 UTC (permalink / raw) To: zsh-users 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=( ${=${(f)"$(<file)"}} ) If the values can have spaces but the keys cannot, then a loop: tyepset -A dbary while read k v; do dbary[$k]="$v"; done < file There are some syntactic restrictions on things that can be easily used as associative array keys; if your keys contain punctuation characters, particularly quotes, you may want to think twice about doing this in zsh at all. Having populated dbary from the file, we join the discussion already in progress ... On Jan 3, 9:02am, Wayne Davison wrote: } Subject: Re: read/write associative array from/to file } } On Tue, Jan 3, 2012 at 1:40 AM, Jérémie Roquet <arkanosis@gmail.com> wrote: } } > echo "myArray=(${(kv)myArray})" >| $db } } 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: } } typeset -A myArray } myArray=( } foo 1\ is\ the\ loneliest\ number } bar 42\ isn\'t\ really\ the\ ultimate\ answer } ) } echo myArray=\(${(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. ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-01-05 5:20 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2012-01-03 2:12 read/write associative array from/to file Thomas Gstädtner 2012-01-03 9:40 ` Jérémie Roquet 2012-01-03 17:02 ` Wayne Davison 2012-01-05 5:20 ` Bart Schaefer
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).