Reading a file into array

Hi,

I need to read a file into array and print them in a loop:-
1st file :-cat a.txt
RC1
RC2
RC3
RC4

My Program:-

#!/bin/ksh
index=0
while [ $index -lt 5 ]
do
       read cnt[index]<a.txt
       print "cnt [$index] value is ${cnt[index]}
       index=`expr $index + 1`
done

Result expected :-

cnt[0] value is RC1
cnt[1] value is RC2
cnt[2] value is RC3
cnt[3] value is RC4

But i am getting the below result :-

cnt[0] value is RC1
cnt[1] value is RC1
cnt[2] value is RC1
cnt[3] value is RC1

I think there is a problem the way i am reading the file into the array

<a.txt will reopen the file every single time, and give you the same line every time. What you want is more like

while read ARR[index]
do
        let index=index+1
done < filename

But first I must ask, why are you reading a whole file into an array? Array sizes are not unlimited, and there's usually no need to do so anyway.

Try also

cnt=($(tr '\n' ' ' <file))
echo ${#cnt[@]}
4
echo ${cnt[@]}
RC1 RC2 RC3 RC4

Thanks corona688

i have this code and it is working fine.
Actual issue is to read input from 2 files by line wise and print them as below:-

>cat a.txt 
RC1
RC2
RC3
RC4
>cat b.txt
11
22
33
44

Desired output :-

RC1 - 11
RC2 - 22
RC3 - 33
RC4 - 44

If i am using passing of file method,i could not arrive that the desired output .so i am trying other ways .Can you help me if it can be achieved in another method.

Thanks again !!

Lots of ways, actually, none of which mean slurping entire whole files into memory(usually a mistake).

paste -d "-" file1 file2
exec 5<file1 # Open into FD 5
exec 6<file2 # Open into FD 6

while read A<&5 && read b<&6 # Read from FD 5 and 6
do
        echo "$A - $B"
done

exec 5<&- # Close FD 5
exec 6<&- # Close FD 6

Thanks Corona688 !!
You made it so simple