Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g

ls "$HOME"

into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16

Thanks

set -A arr `ls"$HOME"`

Thanks for reply but this command is giving an error.

pls paste here the error message and the task you want to do.what you want to do either assign contents of file to an array or the result of ls command to array.

I just want the result for ls command to array. Thanks for interest

ds: line 4: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

You can do it without "ls":

$ ls
a  b  c
$ set *
$ echo ${1} # first element
a
$ echo ${2} # second
b
$ echo ${@} # all
a b c
$ echo ${#@} # number of elements
3
$ eval echo \${${#@}} # last element
c

Thank u for reply. Can u tell me the concept about the set * command?

Do you have korn shell ksh or you are running into some other shell.
pls switch it to korn shell to use arrays.

#!/usr/bin/ksh
x=`ls skm`
echo $x
set -A look $x
echo ${look[0]}

Positional parameters

With trace enabled (the -x option) you can see how it works:

$ set -x
$ set *
+ set a b c

Or, if you insist to use "ls" (with bash, for ksh93 use typeset -A):

$ a=($(ls))
$ echo ${a[0]}
a
$ echo ${a[1]}
b
$ echo ${a[@]}
a b c
$ echo ${#a[@]}
3

sorry i have only bash. and ur given command are not working on bash and giving same error. Thanks
Please tell me the solution for the bash if u have. Thanks for reply again in advance

have you ever tried man pages :wink:

billym.>A=($HOME/*)
billym.>for x in 1 2 3 4 5;do echo ${A[$x]};done
/export/home/billym/1.c
/export/home/billym/1.cpp
/export/home/billym/1.pl
/export/home/billym/1.sh
/export/home/billym/1.xml