Topic Options
#38950 - 03/26/12 02:53 PM Revisiting arrays
JediToby Offline
OL Guru

Registered: 01/14/02
Posts: 140
Loc: Salt Lake City, UT
I've got XML data with multiple elements that I'm trying to sort so I can show them in a particular order. I don't know what my upper bound on an array could be, but I can do xmlcount(<element>) to find that.

I define a global variable: define(&ServiceOrder,arrayinteger,15)

Then a global function @OrderedArray():arrayinteger
Within the function,
define(&temparray,arrayinteger,xmlcount(<element>)

and, after sorting,

&result:=&temparray
endfunction()

Then call the function with this line: &ServiceOrder:=@OrderedArray

What Does &ServiceOrder look like now? Does it have xmlcount() elements, or the original 15?
_________________________
Toby Dillon
Skymail International

Dim null as nothing = "42"

Top
#38951 - 03/26/12 02:59 PM Re: Revisiting arrays [Re: JediToby]
Raphael Lalonde Lefebvre Offline
OL Expert

Registered: 10/14/05
Posts: 3639
Loc: Objectif Lune Montreal
Toby,

In Design time, if you do this, it appears as though it resized the array, and it now has a different amount of items. However, when trying to preview, it just won't compile.

PlanetPress Press does not support dynamic arrays. Arrays have to be of a predetermines size and cannot be dynamically resized in a PressTalk code.

Regards,
Raphaël Lalonde Lefebvre

Top
#38952 - 03/26/12 03:09 PM Re: Revisiting arrays [Re: Raphael Lalonde Lefebvre]
JediToby Offline
OL Guru

Registered: 01/14/02
Posts: 140
Loc: Salt Lake City, UT
That's so not cool. So, can I pass the arraystring out of the function, and if so, I need to make that arraystring the same size as the global variable, right?
_________________________
Toby Dillon
Skymail International

Dim null as nothing = "42"

Top
#38953 - 03/26/12 03:13 PM Re: Revisiting arrays [Re: Raphael Lalonde Lefebvre]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1051
Loc: Montreal, Qc
Toby,

To avoid those kinds of issues altogether, you should pass your array as a parameter to your Sort function:
Code:

&MyArray := @OrderedArray(&MyArray)



That way, your OrderedArray function doesn't have to declare another array (because it already receives a copy of the original one as its parameter). The function body would therefore look something like:
Code:

function @OrderedArray(&MyArr:arrayinteger):arrayinteger
  define(&i,integer,0)
  for(&i,0,1,length(&MyArr)-1)
    %%
    %% sorting code goes here
    %%
  endfor()
  &Result:=&MyArr
endfunction()

_________________________
Product Development Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
#38954 - 03/26/12 03:18 PM Re: Revisiting arrays [Re: Philippe F.]
JediToby Offline
OL Guru

Registered: 01/14/02
Posts: 140
Loc: Salt Lake City, UT
I like that, Phillipe. It has it's limitations, but it is simple.

I tried it out and am gratified to see that as long as the &temparray is the same size as the global array, everything works. The remaining element's values are already defaulted to 0, so I don't have to do that manually. Now I just need to code the loop to stop when it sees a 0 value. Or, more effectively, at the end of xmlcount() iterations.


Edited by JediToby (03/26/12 03:23 PM)
_________________________
Toby Dillon
Skymail International

Dim null as nothing = "42"

Top
#38955 - 03/26/12 03:42 PM Re: Revisiting arrays [Re: JediToby]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1051
Loc: Montreal, Qc
Ahhh... now I think I understand your issue: global variables cannot be initialized with dynamic values because they get initialized before the data is read. So once you've picked a length for your Global Array, you're stuck with it. Unfortunately, there's no way around this restriction.
Either you don't use Global Arrays for this task, or you do exactly what you do now: pick a default size that's large enough to accomodate any other array.
_________________________
Product Development Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
#38956 - 03/26/12 03:48 PM Re: Revisiting arrays [Re: Philippe F.]
JediToby Offline
OL Guru

Registered: 01/14/02
Posts: 140
Loc: Salt Lake City, UT
Yes, you've got it. That's a buzzkill. I remember way back when when global variables could be set by the data....

Thus far, it all seems to be working. I'm testing the existing data (an accurately representative sample) to see just what the maximum size needs to be for speed's sake.
_________________________
Toby Dillon
Skymail International

Dim null as nothing = "42"

Top
#38957 - 03/26/12 04:23 PM Re: Revisiting arrays [Re: JediToby]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1051
Loc: Montreal, Qc
I prefer to recall even further, when there were just no global variables... grin
_________________________
Product Development Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top