I uploaded a sample tree to http://pastebin.com/raw.php?i=FCya382T and
attached the script to this mail.

Olga

2010/5/5 ольга крыжановская <[email protected]>:
> You can create arrays of compound variables, each array element is a
> compound variable which may contain an array.
> Such trees are only limited by memory size and were a driving force to
> deliver a 64bit ksh93 shell in Solaris.
>
> ksh93 -c 'compound container ; compound -A container.a1 ; compound -A
> container.a1[foo].a2 ; compound -A container.a1[foo].a2[bar]=( integer
> z=1 ) ; print -v container'
> (
>        typeset -C -A a1=(
>                [foo]=(
>                        typeset -C -A a2=(
>                                [bar]=(
>                                        typeset -l -i z=1
>                                )
>                        )
>                )
>        )
> )
>
> http://svn.genunix.org/repos/on/branches/ksh93/gisburn/scripts/simplefiletree1.sh
> is a demo application which builds such trees.
>
> Olga
>
> On Wed, May 5, 2010 at 11:25 AM, Daniel Beggemann <[email protected]> wrote:
>> Hello
>> is it possible to have nested associative arrays, i.e. a tree like structure 
>> where each element of an associative array can contain another associative 
>> array?
>>
>> Thanks,
>>
>> --
>> Daniel
>> --
>> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
>> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>> _______________________________________________
>> ast-users mailing list
>> [email protected]
>> https://mailman.research.att.com/mailman/listinfo/ast-users
>>
>
>
>
> --
>      ,   _                                    _   ,
>     { \/`o;====-    Olga Kryzhanovska   -====;o`\/ }
> .----'-/`-/     [email protected]   \-`\-'----.
>  `'-..-| /     Solaris/BSD//C/C++ programmer   \ |-..-'`
>      /\/\                                     /\/\
>      `--`                                      `--`
>



-- 
      ,   _                                    _   ,
     { \/`o;====-    Olga Kryzhanovska   -====;o`\/ }
.----'-/`-/     [email protected]   \-`\-'----.
 `'-..-| /     Solaris/BSD//C/C++ programmer   \ |-..-'`
      /\/\                                     /\/\
      `--`                                      `--`
#!/usr/bin/ksh93

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# simplefiletree1 - build a simple file tree
#

# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are 
not POSIX-conformant
export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin

# Make sure all math stuff runs in the "C" locale to avoid problems
# with alternative # radix point representations (e.g. ',' instead of
# '.' in de_DE.*-locales). This needs to be set _before_ any
# floating-point constants are defined in this script).
if [[ "${LC_ALL-}" != '' ]] ; then
    export \
        LC_MONETARY="${LC_ALL}" \
        LC_MESSAGES="${LC_ALL}" \
        LC_COLLATE="${LC_ALL}" \
        LC_CTYPE="${LC_ALL}"
        unset LC_ALL
fi
export LC_NUMERIC='C'


function add_file_to_tree
{
        typeset treename=$1
        typeset filename=$2
        integer i
        typeset nodepath # full name of compound variable
        typeset -a pe # path elements

        # first built an array containing the names of each path element
        # (e.g. "foo/var/baz" results in an array containing "( 'foo' 'bar' 
'baz' )")
        typeset IFS='/'
        pe+=( ${filename} )
        
        [[ ${pe[0]} == '' ]] && pe[0]='/'

        # walk path described via the "pe" array and build nodes if
        # there aren't any nodes yet
        nodepath="${treename}"
        for (( i=0 ; i < (${#...@]}-1) ; i++ )) ; do
                nameref x="${nodepath}"

                # [[ -v ]] does not work for arrays because [[ -v ar ]]
                # is equal to [[ -v ar[0] ]]. In this case we can
                # use the output of typeset +p x.nodes
                [[ "${ typeset +p x.nodes ; }" == '' ]] && compound -A x.nodes
        
                nodepath+=".nodes[${pe[i]}]"
        done
        
        # insert element
        nameref node="${nodepath}"
        [[ "${ typeset +p node.elements ; }" == '' ]] && typeset -a 
node.elements
        node.elements+=( "${pe[i]}" )
        
        return 0
}

# main
builtin rev

# tree base
compound filetree

# benchmark data
compound bench=(
        float start
        float stop
)

typeset i

# argument prechecks
if (( $# == 0 )) ; then
        print -u2 -f "%s: Missing <path> argument." "$0"
        exit 1
fi

print -u2 "# reading file names"
while (( $# > 0 )) ; do
        IFS=$'\n' ; typeset -a filenames=( $(find "$1" -type f) ) ; IFS=$' \t\n'
        shift
done
print -u2 "# building tree..."

(( bench.start=SECONDS ))

for (( i=0 ; i < ${#filenam...@]} ; i++ )) ; do
        add_file_to_tree filetree "${filenames[i]}"
done

(( bench.stop=SECONDS ))

# print benchmark data
print -u2 -f "# time used: %f\n" $(( bench.stop - bench.start ))

# print tree
print -v filetree

exit 0
# EOF.
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to