Hello

I'm new to this.  I'm using ksh93 version Version JM 93u 2011-02-08


This little example will create an *array of hashes* ( perl speaking)

Created a little script like this :

#!/usr/bin/ksh
typeset -a ALL

. ./arr.txt  # see below what's in this file.

# array of hashes in ksh93

print ${#ALL[*]}        # show the number of entries in the ${arr[*]}

for (( x=0; $x <= ((${#ALL[*]}-1)) ; x++ ))
do
    echo Index : $x , keys :  ${!ALL[$x][*]}
    for z in ${!ALL[$x][*]}
    do
        print key $z '->' value : ${ALL[$x][$z]}
    #    print "${!ALL[$x][$z]} '->' ${ALL[$x][$z]}"
    done
done


The arr.txt file that is imported looks like this :

ALL[0]=(
    [TEST1]=t1
    [TEST2]=t2
    [TEST3]=t3
    )
ALL[1]=(
    [PROEF1]=proef1
    [proef2]=proef2
    [proef3]=proef3
    )
ALL[2]=(
    [TEST1]=bla1
    [TEST2]=bla1.1
    [TEST3]=bla1.2
    )
ALL[3]=(
    [TEST1]=simac1
    [TEST2]=simac2
    [TEST3]=simac3
    )
ALL[4]=(
    [Machine1]="192.168.2.136"
    [Machine2]="192.168.2.137"
    [Machine3]="192.168.2.138"
    )

output of the script :

5
Index : 0 , keys : TEST1 TEST2 TEST3
key TEST1 -> value : t1
key TEST2 -> value : t2
key TEST3 -> value : t3
Index : 1 , keys : PROEF1 proef2 proef3
key PROEF1 -> value : proef1
key proef2 -> value : proef2
key proef3 -> value : proef3
Index : 2 , keys : TEST1 TEST2 TEST3
key TEST1 -> value : bla1
key TEST2 -> value : bla1.1
key TEST3 -> value : bla1.2
Index : 3 , keys : TEST1 TEST2 TEST3
key TEST1 -> value : simac1
key TEST2 -> value : simac2
key TEST3 -> value : simac3
Index : 4 , keys : Machine1 Machine2 Machine3
key Machine1 -> value : 192.168.2.136
key Machine2 -> value : 192.168.2.137
key Machine3 -> value : 192.168.2.138

I've used the same concept for the hash of hashes ..

This is the script.

#!/usr/bin/ksh
# please notice this is ksh93

typeset -A systems


. ./systems1.txt


print "showing ALL\n";
# Getting the keys and values
for i in ${!systems[@]}
do
print $i :
    for z in ${!systems[$i][@]}
    do
    print $z : ${systems[$i][$z]}
    done
print "\n"
done

# just showing one system
print "see the data for system : \c"
read system

print "Getting data for $system\n"
if [[ ! $systems[$system] ]]; then
    print "could not find system : \"$system\""
else
    for z in ${!systems[$system][@]}
do
        print $z : ${systems[$system][$z]}
done
fi


In the file systems1.txtwe see :

systems=(
    [system1]=(
        [description]="rc1_750_boven"
        [omgeving]="IBM P750"
        [status]="active"
        [beheer]="FB"
        [beheer_omgeving]="FAO"
        [ip_beheer]=""
        [ip_applicatie]=""
        [pnode]="pnode0165"
        [location]="Beursplein 1"
        [servicelevel]="Just testing"
        [type]="IBM 8233-E8B"
        [contract]="gold 7x24"
        [klantnummer]="Fake1"
        [telefoon]="123-345345"
        [serial]="Fake1"
        [record]="40"
        )
    [system2]=(
        [description]="IB-nieuw , rc1_750_onder"
        [omgeving]="IBM P750"
        [status]="active"
        [beheer]="FB"
        [beheer_omgeving]="FAO"
        [ip_beheer]=""
        [ip_applicatie]=""
        [pnode]="pnode0076"
        [location]="Beursplein 1"
        [servicelevel]="Gold 7x24"
        [type]="IBM 8233-E8B"
        [contract]="Fake2"
        [klantnummer]="Fake2"
        [telefoon]="Fake.1"
        [serial]="Serial-Fake"
        [record]="32"
        )
)

You can add add many [systems1], [system2] .... systems101 blocks as you like. It's just an example. Ofcouse these items must be unique ( it's a hash )

One question about compound variables :

# uname -a  on my linux give me back :
#Linux arnot-VirtualBox 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux # So I use the following code to store the items I want into a compound variable:
str=$(uname -a)
typeset -C uname                        # declare a compound variable
uname.os=${str:0:5} # ${str:start:length} is substr ( perl speaking )
uname.virtualMachine=${str:6:16}
uname.linuxKernel=${str:23:16}
uname.osName=${str:40:10}
uname.date=${str:55:27}
uname.arch=${str:84:5}
uname.end=${str:104:9}

# trying , in a loop to get at the values :

for i in ${!uname.*}
do
        print $i '->' ${i}  VALUE??

done

output of the script :
uname.arch -> uname.arch VALUE??
uname.date -> uname.date VALUE??
uname.end -> uname.end VALUE??
uname.linuxKernel -> uname.linuxKernel VALUE??
uname.os -> uname.os VALUE??
uname.osName -> uname.osName VALUE??
uname.virtualMachine -> uname.virtualMachine VALUE??

I do not get back the value of the compound variable : What am I doing wrong ???

kind regards Arno Teunisse


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

Reply via email to