I don't know why your code does not work for the presented inputs. It does on my system under ksh.
But your original code has a problem: the conditional part i<${#aa[@]} is fragile - since ${#aa[@]}, i.e. the array size is decremented after each unset - but the following array elements are not automatically shifted to the left. For your example
45.32.3.5 255.0.0.0 255.255.255.0 19.23.2.12
this does not make a difference - but it would make a difference for - say:
45.32.3.5 255.0.0.0 19.23.2.12 255.255.255.0
I improved the code with respect to that issue (note the assignment before loop entry). I also eliminated an inner loop (using an associative array) which improves the runtime from quadratic to linear:
$ cat x.sh
outputs it:
aa=(45.32.3.5 255.0.0.0 255.255.255.0 19.23.2.12)
bb=([255.0.0.0]=1 [255.255.255.0]=1)
print Size of input ${#aa[*]}
print Size of exclude list ${#bb[*]}
n=${#aa[*]}
for ((i=0; i<$n; ++i))
do
if [[ ${bb[${aa[i]}]} ]]
then
print Removing element with index $i: ${aa[i]}
unset aa[i];
fi
print New size of input ${#aa[*]}
done
print Resulting size of input ${#aa[*]}
print Resulting elements ${aa[*]}
for ((i=0; i<$n; ++i))
do
print Index $i, Value 'a['$i']'=${aa[$i]}
done
It produces following output on Fedora 17:
$ ksh x.sh
Size of input 4
Size of exclude list 2
New size of input 4
Removing element with index 1: 255.0.0.0
New size of input 3
Removing element with index 2: 255.255.255.0
New size of input 2
New size of input 2
Resulting size of input 2
Resulting elements 45.32.3.5 19.23.2.12
Index 0, Value a[0]=45.32.3.5
Index 1, Value a[1]=
Index 2, Value a[2]=
Index 3, Value a[3]=19.23.2.12
IFSin your environment?for run in "${bb[@]}"instead offor run in ${bb[*]}and see?