PDA

View Full Version : Arrays


jestrada10103
05-29-03, 16:30
Hello,

I have two arrays

arrayA[x]
arrayB[y]

In arrayA I have 20 procedures
In arrayB I have 2 procedures that need to be excluded from arrayA

How can I remove these from arrayA, the data stored in arrayB will be the array number (e.g. x) that needs to be excluded from my processes...

Any ideas would be great.. I'm doing this in korn shell...

jestrada10103
06-02-03, 12:44
Tek-tips.com helped provide this solution.. FYI:

set -- ${arrayA[@]} # set positional parameters to elements of arrayA

integer subb=0

for i in $@ # for each parameter (ie $1 $2 $3 ...)
do
subb=0
while : #endless loop
do
# if there is a match, we want to drop this element
[[ $i = ${arrayB[subb]} ]] &&
break #jump out of THIS loop
(( subb += 1 ))
#if we've tested all elements of arrayB, then we have no match
[[ $subb -ge ${#arrayB } ]] &&
{
#so we build a new variable containing all elements of arrayA that we want to keep.
NewString=" $NewString $i"
break
}
done
done
set -A arrayA $NewString # recreate arrayA


${#arrayB } evaluates to number of array elements
${arrayA[@]} evaluates to all array elements
${arrayA } evaluates to all array elements
${arrayB[subb]} evaluates to the array element indexed by $subb

Once you have created an array, you cannot "remove" any elements, or insert any new ones.