An arrayfilter question

There has been discussions in the past as to why arraysize( returns 1 for an empty array. The explanation is that there is one empty element in the array and thus the correct response is 1. Using this same logic, you would think that using an arrayfilter with an empty array would return one element reflecting the formula result.

Using this code to test an array with only a separator, I get 2 elements from the formula as expected:

let start=","
arrayfilter start,start,",","OK"
message start ☞ "OK,OK"

But, if I eliminate the separator and only use the empty array code I get no element returned at all:

let start=""
arrayfilter start,start,",","OK"
message start ☞ ""

From the explanation supporting arraysize( I should get “OK” returned from this empty array. What is the logic covering this situation. (Note: results are the same for the arrayfilter( function as well.)

I have always assumed that the arraysize( function counts the number of separators and adds one but maybe that’s too simplistic.

This subject has been noted several times in the past. Here is a quote regarding how the arraysize( function treats an empty array by Jim way back in August of 2016:

This is not a bug.

There have been long discussions about this in the past. Basically, Panorama treats zero length text as an array with one empty element.

The underlying issue here is that the length of a text delimited array is ambiguous when there isn’t any text, which also means there is no delimiters. Data Arrays don’t have this issue, there is no ambiguity between a zero element data array and a data array of one empty element.

I don’t know if I ever said that this is the “correct” response, if I did, please accept my apology. Since this situation is ambiguous, there is no “correct” response. Depending on the needs of your application, the correct response might be 1, or it might be zero. When I originally wrote the arraysize( function 25+ years ago, I had to pick one of these results, and I picked 1. I could have picked zero. Either way, the result would be correct for some applications and wrong for some others. The extract( function can also calculate an arraysize (by using -1 for the item number), and it gives the other result, it treats empty text as having zero elements.

If I was starting from scratch today, maybe I would change the arraysize( result to be zero instead of one, if for no other reason than I am quite tired of this topic! Though I suspect if the result was 0 instead of 1 there would be complaints that this was incorrect as well, in fact, in my programming I think that returning 1 has generally been more useful to me, though I’m not 100% sure about that. But the decision made on this years ago is irrevocable – if it was changed now, it would silently cause terrible bugs in much existing code, including and especially in Panorama’s internal libraries.

The same ambiguity applies to arrayfilter, and Gary, you are correct that this ambiguity was resolved differently in the case of arrayfilter than arraysize. So this is certainly inconsistent. Sorry about that. Again, this cannot possibly be changed without silently causing massive bugs.

This is a case where I would definitely do it the same way again if starting over. The example you cite is quite unusual, what would be the point in a real application of filtering with a fixed value? The more common situation is using this statement (or function) as a filter – it’s right in the name! For example, suppose I set up a filter that adds ( and ) to each array element.

let start=""
arrayfilter start,start,",","("+import()+")"

In this case, I think it would make no sense for the result to be (). The result in this case should be empty text, and that’s what it is.

When making a movie, there is a person specifically in charge of continuity, to try to make sure that any inconsistencies are rooted out before the movie is released. A movie is shot over a few weeks. On a software project that over decades has pushed far beyond it’s original goals, unfortunately some continuity problems are going to crop up. This is one of them, and definitely not the only one in Panorama.

Well, to quote Ash from the Army of Darkness when he was asked if he spoke the exact words needed to say when he removed the Necronomicon from the cradle: “Look, maybe I didn’t say every tiny syllable, no. But basically I said them, yeah.” That describes my fuzzy memory.

Let me apologize for belaboring this issue - I was only curious to find out what logic was different between the way arraysize and arrayfilter handled the situation. I certainly didn’t mean to take you away from more important work and have you take the time to write a virtual novella explaining your position.

I certainly would not expect any changes to be made at this late date. As you say, the whole concept is ambiguous and could be interpreted either way, or in this case, both ways.

My current use is rather unusual in that I’m using an arrayfilter( inside a custom function as a simulated loop to gather various amounts of random text depending upon the parameter given. The problem came to the surface when I tried to have one iteration of the fake loop executed and that resulted in an empty return.

arrayfilter(rep(¶,•1),¶,{my formula here to extract random text})  //•1 is the parameter used

By padding the fake array with a space I could then have things work since the first element would never be blank:

arrayfilter(" "+rep(¶,•1),¶,{my formula here to extract random text}

Finis :zipper_mouth_face: