Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays 1

Status
Not open for further replies.

jestrada101

Technical User
Mar 28, 2003
332
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...
 
#!/usr/bin/ksh

set -A arrayA a b c d e f g
set -A arrayB c e

set -- ${arrayA[@]}

integer subb=0

for i in $@
do
subb=0
while :
do
[[ $i = ${arrayB[subb]} ]] &&
break
(( subb += 1 ))
[[ $subb -ge ${#arrayB[*]} ]] &&
{
NewString=" $NewString $i"
break
}
done
done
set -A arrayA $NewString
 
Thanks!
Can you explain a bit on what is going on inside the "for" loop?

Worked perfect.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top