Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    45

    Unanswered: splitting the string and storing in an array

    How can I split the following and get the index in a list of files?
    Eg :
    >ls
    aaa1.txt
    aaa2.txt
    aaa3.txt
    Now how can put this in a array?. I tried,
    lsString=`ls`
    But ${lsString[0]} takes whole list as string rather than first string. How can put this list in an array? (suggest if any method without using loop)

    thanks

  2. #2
    Join Date
    Jul 2004
    Posts
    45
    for f in `ls`
    do
    fileArr[${#fileArr[@]}]=$f
    done
    echo $fileArr[0]

    Any better solution than this?

  3. #3
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Try:
    Code:
    set -A filearr `ls`
    or
    Code:
    filearr=$(ls)
    Last edited by LKBrwn_DBA; 08-03-04 at 16:14.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  4. #4
    Join Date
    Jul 2004
    Posts
    45
    set -A filearr `ls` WORKS !!!! (the other one doesnt)

    thank you very much!

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    I posted similar recently but its a little hang up of mine, so I'll post again!

    DON'T USE 'ls' IF YOU CAN HELP IT! USE A FILE EXPRESSION (i.e. the bit that you provide to 'ls' when you do 'ls /some/where/aFile.*')

    Try...

    $ mkdir aNewDir
    $ cd $_
    $ touch a "b c"
    $ set -A myArray `ls`
    $ echo ${myArray[1]}
    b
    $ set -A myArray *
    $ echo ${myArray[1]}
    b c

    using ls incorrectly results in the second file "b c" being split into 2 elements, "b" and "c"
    Last edited by Damian Ibbotson; 08-04-04 at 06:47.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •