1833682 Members
4747 Online
110062 Solutions
New Discussion

array question

 
SOLVED
Go to solution
andi_1
Frequent Advisor

array question

Hi guys,

Thank you for your previous responses. Hopefully, it will be the last question.

Lets say, I have declared an array and have initialized it with the disks currently on the system:
diskArray[0]=/dev/dsk/c0...
diskArray[1]=/dev/dsk/c1
etc.
Currently, I can find out the biggest disk, so I do vgcreate on the biggest disk.
I will perform vgextend on all other disks in the array.

In the shell, how can I remove the value from the array? I know the index of the disk I want to remove from the array.
If I do diskArray[$biggestDiskIndex]="", would it work?

I am hesitant to the above, since for loop will go through all the disks left in the array and perform vgextend of them. If "" is encountered, I am afraid vgextend might fail!

Thank you!
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: array question

Hi:

If you are referring to deleting an element of an array in 'awk', use:

# delete array[index]

The effect will be the same as if the element had never been allocated.

Regards!

...JRF...
andi_1
Frequent Advisor

Re: array question

Actually, I need to delete not in awk but in posix-shell.

Thank you.
A. Clay Stephenson
Acclaimed Contributor

Re: array question

If it were me, I would do it something like this:

XX[0]="Test 0"
XX[1]="Test 3"
XX[2]="Test 6"
XX[3]="Test 34"

Now let's suppose that you determine, XX[1] is the one you want to delete. I would simply move the last entry into that position and then do an unset on the last array member. ${#XX[*]} will then be reduced by 1. The array is no longer order as before but who cares. Of course, in the special case that the element that you need to delete is the last element then simply unset it and your are done.



If it ain't broke, I can fix that.
andi_1
Frequent Advisor

Re: array question

Hi Clay,

Thank you for your response.

I was thinking of replacing item to be removed with the last item. Unfortunately, I don't know the length of an array. I doubt shell has function such length(array), so I guess I will need to do a loop to determine the length of an array...?

Thanks again!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: array question

The value ${#arrayname[*]} IS the number of elements in the array. Typically I do something like this:
LIMIT=$(( ${#arrayname[*]} - 1)) # if the subscripts start at 0
I=0
while [ ${I} -le ${LIMIT} ]
do
echo "${I} --> ${arrayname[${I}]}"
I=$(( ${I} + 1 ))
done
If it ain't broke, I can fix that.