Reading in data sets into arrays from an input file.

Hye all,

I would like some help with reading in a file in which the data is seperated by commas. for instance:

input.dat:
1,2,34,/test

for the above case, the fn. will store the values into an array -> data[] as follows:

data[0] = 1
data[1] = 2
data[2] = 34
data[3] = /test

I am trying to write this fn. but dont know how to look for a comma so that the next data can be read in. Any help will be appreciated. Thanks in advance.

use perl split

What language are you writing the script in?

#!/bin/ksh

file='/tmp/sid.txt'
typeset -i i=0

while read input
do
  IFS=,; set -A arr ${input}
  while (( i < ${#arr[@]} ))
  do
    echo "arr[$i]=${arr[$i]}"
    ((i+=1))
  done
  ((i=0))
done < "${file}"

ksh

oldIFS=$IFS
IFS=,
set -A data $(<input.dat)
IFS=$oldIFS

Thanks for the input..!

I am sorry I didn't mention this earlier, but I am using ksh. I am going to try this out with what I am working on.

Once again, thanks a lot for all the input. Goodday! :slight_smile: