Unix Shell - zsh array assignment

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > August 2006 > zsh array assignment





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author zsh array assignment
Eric Moors

2006-08-23, 1:26 pm

Hi,

I am trying to use the associative arrays in zsh, but I cannot figure out
how I can assign the output of a command to them. Hopefully someone here
can provide some assistance (or point me to an alternative)

I'd like to put the output of the jhead command (an image EXIF tag parser)
into an array. The output of jhead is as follows:

$ jhead img.jpg| grep "^File"
File name : img.jpg
File size : 2844547 bytes
File date : 2006:08:23 16:48:37

So I pipe it to a sed command:

$ jhead img.jpg| grep "^File"|
sed -n 's/^\([^:]\+[^ ]\)[ ]*: \(.*\)$/\1 \2/;H;${x;s/\n/ /g;p}'
File name img.jpg File size 2844547 bytes File date 2006:08:23 16:48:37

in order to do this:

$ typeset -A ARRAY
$ ARRAY=(`jhead img.jpg| grep "^File"|
sed -n 's/^\([^:]\+[^ ]\)[ ]*: \(.*\)$/\1 \2/;H;${x;s/\n/ /g;p}'`)
zsh: bad set of key/value pairs for associative array

So I tried several ways of quoting \1 and \2 (' as well as ")

$ jhead img.jpg| grep "^File"|
sed -n 's/^\([^:]\+[^ ]\)[ ]*: \(.*\)$/"\1" "\2"/;H;${x;s/\n/ /g;p}'
"File name" "img.jpg" "File size" "2844547 bytes" "File date" "2006:08:23
16:48:37"

$ jhead img.jpg| grep "^File"| sed -n 's/^\([^:]\+[^ ]\)[ ]*:
\(.*\)$/'''\1''' '''\2'''/;H;${x;s/\n/ /g;p}'
'File name' 'img.jpg' 'File size' '2844547 bytes' 'File date' '2006:08:23
16:48:37'

but to no avail, it keeps complaining!

Any help would be greatly appreciated.

Eric


Stephane Chazelas

2006-08-23, 1:26 pm

On Wed, 23 Aug 2006 17:03:14 +0200, Eric Moors wrote:
> Hi,
>
> I am trying to use the associative arrays in zsh, but I cannot figure out
> how I can assign the output of a command to them. Hopefully someone here
> can provide some assistance (or point me to an alternative)
>
> I'd like to put the output of the jhead command (an image EXIF tag parser)
> into an array. The output of jhead is as follows:
>
> $ jhead img.jpg| grep "^File"
> File name : img.jpg
> File size : 2844547 bytes
> File date : 2006:08:23 16:48:37

[...]

Something like:

typeset -A a
jhead img.jpg | while IFS=": " read -r k v; do a[$k]=$v; done

or:

typeset -A a
IFS=$'\n\n'
a=(
$(jhead img.jpg | sed -n 's/ *: */\
/p')
)


(for example).

--
Stephane
Eric Moors

2006-08-24, 1:27 am

>> I am trying to use the associative arrays in zsh, but I cannot figure out
> [...]
>
> Something like:
>
> typeset -A a
> jhead img.jpg | while IFS=": " read -r k v; do a[$k]=$v; done


I also realized that I could use a while loop, but I preferred
to do without one. Pasting the output of the jhead|... line in
the array assigment worked fine, so I assumed there had to be some
way of doing it directly.

> or:
>
> typeset -A a
> IFS=$'\n\n'
> a=(
> $(jhead img.jpg | sed -n 's/ *: */\
> /p')
> )
>


Setting IFS!
This is just the hint I needed!

I am now using this.

IFS=$'\n='
typeset -A a
a=($(jhead img.jpg | sed -n 's/^\([^:]\+[^ ]\)[ ]*:[ ]\+\(.*\)$/\1=\2/p'))

Thanks

Eric
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com