Notifications
Clear all

[Closed] how to remove empty entries of an array ?

Hi all !
I’m stucked on, probably, a very simple problem . I use some multiplearrays to filter data from a text.

#(#(“a”,“b”,“c”),#(“d”,“e”,“f”), …)

Sometimes i have to use deleteItem myarray[1] [1]
deleteItem myarray[2] [1]
deleteItem myarray[3] [1]

but the empty entry stay and then i can t work no more on my array.
#(#( ),#(“d”,“e”,“f”), …)
for n =1 to myarray.count do
append newarray (myarray[n][1]+myarray[n][2]+myarray[n][3]) –wont work because there’s no [n][1] for n = 1 ,2 or 3
So, I would like to remove the empty entry from the array but i can’t find a way to do it.
Any idea?
Thanks in advance.

5 Replies

A sub-array in an array is exactly like any other item. You can call deleteItem on it, or just collect the valid ones to remove the ones that you don’t want.

So for example

yourArray = #(#(), #(1,2,3), #("a","b","c"))
  yourArray = for i = 1 to yourArray.count where yourArray[i].count > 0 collect yourArray[i]
  -->#(#(1, 2, 3), #("a", "b", "c"))

Alternatively,

yourArray = #(#(), #(1,2,3), #("a","b","c"))
for i = yourArray.count to 1 by -1 where yourArray[i].count == 0 do deleteItem yourArray i
yourArray
-->#(#(1, 2, 3), #("a", "b", "c"))

Try testing the internal array if it has a count of 0.

for n=myarray.count to 1 by -1 do
 (
 	if myarray[n].count == 0 do
 	(
 		deleteitem myarray n
 	)
 )

You have to count backwards since you will be lowering the array count anytime you delete an item.

-Eric

EDIT: Bobo beat me to it.

Thanks to the beauty of MAXScript, my first example can be shortened to

yourArray = #(#(), #(1,2,3), #("a","b","c"))
 yourArray = for i in yourArray where i.count > 0 collect i
#(#(1, 2, 3), #("a", "b", "c"))

Brillant, and yes, i agree with you bobo, beautifull.
Thanks a lot to you two!

To collect all valid arrays is much faster and more memory friendly than remove all empty ones.
But as I see it makes sense for you to collect new array using condition statement in the same loop:


newarray = for i in myarray where i.count > 2 collect (i[1] + i[2] + i[3])