Searching for a token in a file

Hi All,
I am new to scripting.
I have a requirement where I need to search for token present in array in a file. If the token is found I need to take the first column of that line from the file and append it to token and direct it to a new file.

e.g. token file
token.txt contains

abc
der
xyz

flat.txt contains

123 abc
234  der  sgd
345 wse xyz
000 qwe qaws zksd

expected output

abc 123
der 234
xyz 345

I tried using following code but its not working.

filearray=( `cat token.txt | tr '\n' ' '`)
 
for ((i=0;i<${#filearray
[*]};i++))
{
awk '{for (f=NF; f>=0 ; f--) if ($f ~/${filearray}/) {print  $filearray, $1,; next}}' flat.txt} >> newfile.txt
exit

.

Appreciate your help.

awk 'NR==FNR{A[$1];next}{for(i=2;i<=NF;i++){if($i in A) print $i,$1}}' token.txt flat.txt

Never change horses. If you use awk, try to do as much as possible in it (like Yoda's approach). If you start with shell scripting, try to stick to it. It is possible in shell, assuming filearray ist assigned from token.txt:

while read TOKEN REST
  do for ((i=0; i<${#filearray[@]}; i++))   
     do     [ "$REST" != "${REST/${filearray[$i]}/}" ] && echo ${filearray[$i]} $TOKEN
     done
 done <flat.txt
abc 123
der 234
xyz 345

And, don't open and read flat.txt several times. Memory access is far faster, so loop the array several times.