Discussion:
[ast-users] array of structures?
Richard L. Hamilton
2017-02-01 05:14:36 UTC
Permalink
Modern ksh93 allows both what amounts to structures (typeset -T) and arrays (typeset -a or typeset -A).

Is it possible to have an array of structures? (in particular, an associative array)

If so, are there any examples available?

Say that I have a structure consisting of at a minimum:

vmhost
vmstartcmd
vmstopcmd

and I want a structure with those members, and an array of them that can be looked up by vmname, given that I have VMs on multiple hosts using more than one sort of VM software (e.g. Parallels and VirtualBox).
Philippe Bergheaud
2017-02-01 10:36:53 UTC
Permalink
Post by Richard L. Hamilton
Modern ksh93 allows both what amounts to structures (typeset -T) and
arrays (typeset -a or typeset -A).
Is it possible to have an array of structures? (in particular, an associative array)
If so, are there any examples available?
vmhost
vmstartcmd
vmstopcmd
and I want a structure with those members, and an array of them that
can be looked up by vmname, given that I have VMs on multiple hosts
using more than one sort of VM software (e.g. Parallels and VirtualBox).
I would simply create an associative array with

$ typeset -A vm=(
[name1]=(host='host1'; start='start1'; stop='stop1')
[name2]=(host='host2'; start='start2'; stop='stop2')
)

The whole array could be printed in a reusable format with

$ typeset -p vm

Names (array indices) would be enumerated with

$ print ${!vm[*]} # assuming no blanks in names
or
$ print "${!vm[@]}" # in the general case

A name reference (a pointer) could be used to loop over the array entries
Post by Richard L. Hamilton
do
typeset -n p=${vm[$name]}
print "name=$name host=${p.host} start=${p.start}
stop=${p.stop}"
Post by Richard L. Hamilton
done
Philippe
Richard L. Hamilton
2017-02-01 19:24:33 UTC
Permalink
Thanks, the declaration syntax was what I particularly needed, got it working now.

Definitely options there that would be supportive of future complications (more commands, etc).
Post by Philippe Bergheaud
Post by Richard L. Hamilton
Modern ksh93 allows both what amounts to structures (typeset -T) and
arrays (typeset -a or typeset -A).
Is it possible to have an array of structures? (in particular, an associative array)
If so, are there any examples available?
vmhost
vmstartcmd
vmstopcmd
and I want a structure with those members, and an array of them that
can be looked up by vmname, given that I have VMs on multiple hosts
using more than one sort of VM software (e.g. Parallels and VirtualBox).
I would simply create an associative array with
$ typeset -A vm=(
[name1]=(host='host1'; start='start1'; stop='stop1')
[name2]=(host='host2'; start='start2'; stop='stop2')
)
The whole array could be printed in a reusable format with
$ typeset -p vm
Names (array indices) would be enumerated with
$ print ${!vm[*]} # assuming no blanks in names
or
A name reference (a pointer) could be used to loop over the array entries
Post by Richard L. Hamilton
do
typeset -n p=${vm[$name]}
print "name=$name host=${p.host} start=${p.start} stop=${p.stop}"
done
Philippe
Richard Hamilton
2017-02-01 19:39:30 UTC
Permalink
Thanks, the declaration syntax was what I particularly needed, got it
working now.

I can see how I might use some more of those features for future
complications.
Cedric Blancher
2017-02-01 20:54:00 UTC
Permalink
Try this:
ksh -c 'typeset -T vm_t=(integer size) ; compound container=(vm_t -A
foo=([f]=(size=1))) ; print -v container'
(
vm_t -A foo=(
[f]=(
typeset -l -i size=1
)
)
)

Does that answer your question? Container can be read back into a var
with read -C

Ced
Post by Richard L. Hamilton
Modern ksh93 allows both what amounts to structures (typeset -T) and arrays (typeset -a or typeset -A).
Is it possible to have an array of structures? (in particular, an associative array)
If so, are there any examples available?
vmhost
vmstartcmd
vmstopcmd
and I want a structure with those members, and an array of them that can be looked up by vmname, given that I have VMs on multiple hosts using more than one sort of VM software (e.g. Parallels and VirtualBox).
_______________________________________________
ast-users mailing list
http://lists.research.att.com/mailman/listinfo/ast-users
--
Cedric Blancher <***@gmail.com>
[https://plus.google.com/u/0/+CedricBlancher/]
Institute Pasteur
Loading...