Max( and arrays

I have an array of dates:
2459725, 2459237, 2459269, 2459298, 2459331, 2459360, 2459390, 2459422, 2459453, 2459482, 2459513, 2459542, 2459569, 2459604, 2459634, 2459664, 2459695, 2459215, 2459597, 2459225, 2459246, 2459256, 2459271, 2459284, 2459277, 2459282, 2459299

And I need to determine the latest date among them. Trying to use max( produces an error that max requires at least two parameters.

max(DateArray)

So in spite of the array using commas as separators, the array is seen as a single parameter. Is there a way to overcome this?

This worked.

let DateArray = "2459725, 2459237, 2459269, 2459298, 2459331, 2459360, 2459390, 2459422, 2459453, 2459482"
let MaxDate = formulavalue("max("+DateArray+")")
message MaxDate

That is very ingenuous solution, but unPanorama-like. Shouldn’t Jim’s formula work?

Another less clever solution, very specific to the data at issue, is

arraysort(DateArray,",")[-7,-1]

No it shouldn’t. The max( function is expecting 2 or more separate parameters. The content of the DateArray parameter is one string of text. The commas within it are just characters in that one parameter, not delimiters between parameters. You wouldn’t want every string that happened to contain commas to be interpreted as multiple parameters.

No, I wouldn’t want every string with a comma interpreted as multiple parameters, but wouldn’t I want every string inside a max() function interpreted as multiple parameters?

How would you expect this to be interpreted?

max("one, two", "A", "B")

Given how max( works, I am not sure what to expect in your hypothetical.

When I first looked at Jim’s questions, I thought, “oh, you didn’t specify the separator.” Of course, it not allowed because the max function assumes and requires a comma. But if it didn’t, the problem you pose could be trivial.

Suppose we have a new function, arraynumericmax(ArrayName, separator), which could be defined as

 array(arraynumericsort(ArrayName, separator), -1, separator)

This would correctly return 2459725 for Jim’s array.

We could have one for text values, just use arraysort instead of arraynumericsort.

Then the array “one, two;A;B” would return one, two, assuming a semicolon separator.
The array “one, two;X;Y” would return Y.

I meant that 3 strings of text should be compared. The first was the eight character string “one, two”. The second was the single character string “A”, and the last was the single character string “B”. If I had wanted “one” to be compared to “two” to be compared to “A” to be compared to “B”, I would have written

max("one", "two", "A", "B")

Characters inside the quotes are just characters. Delimiters between parameters will be found outside the quotes. Without that rule, I don’t have any way to tell Panorama that “one, two” is a single, eight character parameter.

I think there is some confusion about what the max( function is all about. It’s not a function for operating on arrays. It’s a function for comparing two or more discrete parameters, and returning the greatest.

In Panorama 6, it was limited to two parameters, and they both had to be numeric. In Panorama X, there can be an unlimited number of parameters, and they can be either numeric or text, but not both in the same function. Those commas are not array separators, they are delimiters between parameters.

Function parameters are evaluated independently, before the function is called. The values are passed to the function when it is called. The function only sees those values, not the expressions that were used to generate the values. The max( function isn’t going to take parameters that were passed separately and break them up into still more parameters.

1 Like

Dave has left me with nothing to explain – he has done it perfectly, and everything he has said is exactly right.

There are a number of Panorama statements and functions that are designed to interpret text as a text array. The max( function is not one of them – it doesn’t know a text array from a bag of bones. If the documentation for a function or statement doesn’t specifically mention text arrays, that function or statement doesn’t support text arrays.

What @JamesCook would like is a maxnumberinarray( function. However, this function doesn’t currently exist. Dave’s formulavalue( solution may well be the best way to do this at this time. It seems very “Panorama-y” to me. :+1:

Well, quite the discussion my question inspired. I had figured the array was being see as a single parameter, and yes, a maxnumberinarray( function would do the trick.

With no such function available, David has provided the solution that I hadn’t figured out on my own and it will work just fine.

These simple functions might work for arrays of numbers to find the max or min of the elements:

registercustomfunction "ARRAYNUMERICMAX",2,||| arraylast(arraynumericsort(•1,•2),•2) |||

registercustomfunction "ARRAYNUMERICMIN",2,||| arrayfirst(arraynumericsort(•1,•2),•2) |||

After thinking about this for a while, I see what you are saying. Hopefully my confusion is cleared up, at least on this topic.