Unix Shell - initializing an array in ksh

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > initializing an array in ksh





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author initializing an array in ksh
hyperboogie@gmail.com

2006-12-17, 1:16 pm

Hello all

I can't believe I'm asking this, but for some reason i can't figure out
why this doesn't work.
I'm trying to initialize an array with the values from a list which
contains all the files in a directory.
What i would like is to have one file/Directory in each element of the
array.

I tried the following but it doesn't work:

1. list=`ls /path/to/directory`
2. set -A files "$list"
3. echo "number of files is ${#files[*]}
4. echo "file #1 is ${files[1]}"

the output is:
5. number of files is 1 # there are more than 1 files in the directory
6. file #1 is


why doesn't the statement in line 2 do what i want it to?
why according to the output in line 5, is there only 1 file in the
directory although there are more.
why is there an empty output in line 6?

Thanks :-)

S.B

hyperboogie@gmail.com

2006-12-17, 1:16 pm

Never mind .....
found the problem

the double quotes had to be removed from line 2
:-)

all is good now

hyperboogie@gmail.com wrote:
> Hello all
>
> I can't believe I'm asking this, but for some reason i can't figure out
> why this doesn't work.
> I'm trying to initialize an array with the values from a list which
> contains all the files in a directory.
> What i would like is to have one file/Directory in each element of the
> array.
>
> I tried the following but it doesn't work:
>
> 1. list=`ls /path/to/directory`
> 2. set -A files "$list"
> 3. echo "number of files is ${#files[*]}
> 4. echo "file #1 is ${files[1]}"
>
> the output is:
> 5. number of files is 1 # there are more than 1 files in the directory
> 6. file #1 is
>
>
> why doesn't the statement in line 2 do what i want it to?
> why according to the output in line 5, is there only 1 file in the
> directory although there are more.
> why is there an empty output in line 6?
>
> Thanks :-)
>
> S.B


Bill Marcum

2006-12-18, 1:20 pm

On 17 Dec 2006 09:09:52 -0800, hyperboogie@gmail.com
<hyperboogie@gmail.com> wrote:
>
>
> Never mind .....
> found the problem
>
> the double quotes had to be removed from line 2
>:-)
>
> all is good now
>

You don't need to use ls; you can write
set -A files *


--
Is knowledge knowable? If not, how do we know that?
Stephane CHAZELAS

2006-12-18, 1:20 pm

2006-12-18, 12:21(-05), Bill Marcum:
> On 17 Dec 2006 09:09:52 -0800, hyperboogie@gmail.com
> <hyperboogie@gmail.com> wrote:
> You don't need to use ls; you can write
> set -A files *


Only in zsh (when not in ksh emulation).

In ksh:

set -A files -- *

or:

files=(*)

in zsh and ksh93 and newer versions of bash.


--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com