DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   resetting an array (http://www.daniweb.com/forums/thread161478.html)

DimaYasny Dec 9th, 2008 12:28 pm
resetting an array
 
Hi,

I am looping through some files, gathering information into an array during that loop. The array grows with the loop counter and at every point might be longer or shorter (more or less members in it)

so in order to reuse the array I need to drop all values in it, and I can't seem to find an easy way to do so (to loop through the array using its length and putting NULL into every position is a bit too much)

Example:
i=1
for tree in `ls -A /tmp/imagetrees/*.tree`
do
        for line in `cat $tree`
        do
                array[$i]=$line
                let i=$i+1
               
        done
        echo ${array[*]} >
        i=1
        # need to reset array to NULL here, before reusing in the next loop
done

.tree file looks like this:
# cat /tmp/imagetrees/base33111.tree
base33111
base3311
base331
base33
base3
# cat /tmp/imagetrees/base33.tree
base33
base3

Salem Dec 9th, 2008 1:09 pm
Re: resetting an array
 
http://www.gnu.org/software/bash/man...ef.html#Arrays

DimaYasny Dec 9th, 2008 4:10 pm
Re: resetting an array
 
thanks, found the unset function. can't believe I forgot about it


All times are GMT -4. The time now is 1:12 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC