If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > splitting the string and storing in an array

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-03-04, 14:13
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
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
Reply With Quote
  #2 (permalink)  
Old 08-03-04, 14:19
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
for f in `ls`
do
fileArr[${#fileArr[@]}]=$f
done
echo $fileArr[0]

Any better solution than this?
Reply With Quote
  #3 (permalink)  
Old 08-03-04, 14:52
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Try:
Code:
set -A filearr `ls`
or
Code:
filearr=$(ls)
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Last edited by LKBrwn_DBA; 08-03-04 at 15:14.
Reply With Quote
  #4 (permalink)  
Old 08-03-04, 15:35
deebee deebee is offline
Registered User
 
Join Date: Jul 2004
Posts: 45
set -A filearr `ls` WORKS !!!! (the other one doesnt)

thank you very much!
Reply With Quote
  #5 (permalink)  
Old 08-04-04, 05:44
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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 05:47.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On