Finding indices in an array nearest to a set of values

I have an two arrays. One array BINDIST consists of fences. I have another array XOFFS.

Eg

BINDIST = 0 10 20 30 40 50 60
XOFFS = 2 3 4 23 25 28 55 58 

I want to find to find the indices of values in XOFFS that are closest to each BINDIST.

My idea is to do as follows

I create array XMIDS such that

XMIDS(I) = ( BINDIST(I) + BINDIST(I+1) ) / 2, for I = 1 to NBINDIST-1  

Then I check values in XOFFS such that

I find all XOFFS between BINDIST(1) and XMID(1), and choose among these, the closest to BINDIST(1)
Save in VALS(1)

I find all XOFFS between XMIDS(I) and XMIDS(I+1) , and choose among these, the closest to BINDIST(I+1), for I = 1 to NBINDIST-2
Save in VALS(I+1)

I find all XOFFS between XMIDS(NBINDIST-1) and BINDIST(NBINDIST) , and choose among these, the closest to BINDIST(NBINDIST)
Save in VALS(NBINDIST)

If I don't find any XOFFS if the regions described, I return ZERO to the corresponding VALS.

Result should be

VALS = 2 0 23 28 0 55 58

what is the results surpposed to be?

VALS = 2 0 23 28 0 55 58

try:

echo -e "${XOFFS[@]}\n${BINDIST[@]}" |\
awk  'NR==1{len=split($0,X);next}
{for(i=1;i<=NF;i++){
   t=x=0
   for(j=1;j<=len;j++){
      x=sqrt((X[j]-$i)*(X[j]-$i))
      if(j==1){t=x}else{t=t<x?t:x}
      a[x]=X[j]}
      printf  t<=5?a[t]" ":"0 "
      delete a}
   printf "\n"}'
2 0 23 28 0 55 58

Coded the following csh script and got the correct result

2 0 23 28 0 55 58
#!/bin/csh

set XOFFS = (2 3 4 23 25 28 55 58)
set BINDIST = (0 10 20 30 40 50 60)
echo $XOFFS
echo "${XOFFS}\n${BINDIST}" |        \
  awk  'NR == 1 { len = split($0, X); next }  \
    { for (i=1; i<=NF; i++) {           \
      t = x = 0                         \
      for (j=1; j<=len; j++) {          \
        x = sqrt((X[j]-$i)*(X[j]-$i))   \
        if(j==1) {t=x} else {t=t<x?t:x} \
        a[x] = X[j]                     \
      }                                 \
      printf t<=5?a[t]" ":"0 "          \
      delete a                          \
    }                                   \
      printf "\n"                       \
    }'
    

---------- Post updated at 08:35 PM ---------- Previous update was at 08:21 PM ----------

Got no idea how this code is working. Why do you use a[x] and what's the sqrt about. You did not calculate XMIDS

I don't know if this codes can be run in csh, I did it in bash.

#!/bin/bash
XOFFS=(2 3 4 23 25 28 55 58) # array 
BINDIST=(0 10 20 30 40 50 60) # array 
echo ${XOFFS[@]} echo -e "${XOFFS[@]}\n${BINDIST[@]}"|\
awk  'NR==1{len=split($0,X);next}
{for(i=1;i<=NF;i++){
   t=x=0
   for(j=1;j<=len;j++){
      x=sqrt((X[j]-$i)*(X[j]-$i))
      if(j==1){t=x}else{t=t<x?t:x}
      a[x]=X[j]}
      printf  t<=5?a[t]" ":"0 "
      delete a}
      printf "\n"}'

It works but trying to figure out the calculations. Values in both XOFFS and BINDIST will have both negative and positive values.
The values can be floating point, not just integers.

awk  'NR==1{len=split($0,X);next}
 {for(i=1;i<=NF;i++){
    t=x=0
    for(j=1;j<=len;j++){ 
    x=sqrt((X[j]-$i)*(X[j]-$i)) # if the value of X[j]-$i could be negative or positive, this calculation make this value to be absolute value, since there is no abs function in awk, ps. sqrt is SQuare Roo
       if(j==1){t=x}else{t=t<x?t:x}
       a[x]=X[j]}
       printf  t<=5?a[t]" ":"0 " # if abs value of X[j]-$i (t) is less than or equal to 5, accept as the closet value, if not, set it as 0
         delete a}       printf "\n"}'