Powerbuilder Dynamic Array Manipulation
Asked Answered
D

4

8
string array[]
long lBound, uBound

lBound = LowerBound(array[]) // = 1, empty array value
uBound = UpperBound(array[]) // = 0, empty array value

array[1] = 'Item 1'
array[2] = 'Item 2'
array[3] = 'Item 3'

lBound = LowerBound(array[]) // = 1
uBound = UpperBound(array[]) // = 3

array[3] = '' //removing item 3

lBound = LowerBound(array[]) // = 1, still
uBound = UpperBound(array[]) // = 3, still (but array[3] is nulled?

I think the line 'array[3]' is wrong, but I think I've read that this should remove the array cell.

What would be the right way to remove an array cell? Does it depend on object type? (String vs Number vs Object)

Or

Can one manipulate the UpperBound value to make it work?

i.e. after removing Item 3, I want the UpperBound, or arraylength, to be 2, since this is logically correct.

Dowery answered 29/3, 2010 at 5:56 Comment(1)
By the way, LowerBound will always return 1 with a variable-size array. LowerBound is used when you declare an array like string foo[-7 TO 34]. LowerBound returns -7 for this array.Thrilling
I
9

For variable-size arrays, memory is allocated for the array when you assign values to it. UpperBound returns the largest value that has been defined for the array in the current script. However, you can do the trick using another dynamic array.

string array2[]
int i

for i = 1 to UpperBound(array[]) - 1
    array2[i] = array[i]
next

array = array2

Then UpperBound(array[]) will be reduced by 1.

This will work for UpperBound(array[]) - 1 > 2 because powerbuilder allocates by default memory size for 3 elements when a dynamic array is declared.

Itu answered 29/3, 2010 at 12:15 Comment(1)
Sigh, I thought I'd have to do something like this. Thanks gd047Dowery
M
3

Powerbuilder doesn't really have any good array manipulation functions built in (that i know of).

You can achieve what you are trying to do by copying the values you want to retain to a new unbounded array.

For example

int i
string array3[] = {'Item 1', 'Item 2', 'Item 3'}, array2[]

for i = 1 to 2
    array2[i] = array3[i]
next
UpperBound(array2[]) // = 2

In your example you are removing only the latest value - this can be done even more simply by copying the whole array to a new smaller, bounded array like so:

string array3[] = {'Item 1', 'Item 2', 'Item 3'}, array2[2]

array2 = array3
UpperBound(array2[]) // = 2
Merlenemerlin answered 29/3, 2010 at 11:40 Comment(0)
T
2

When I was writing a comment for Dan's answer I started thinking about what I do use, since I don't like PFC's lists. What I use is a DataStore, which, if you think about it the right way, is like a list on steroids.

Thrilling answered 30/3, 2010 at 13:24 Comment(0)
M
0

Rather than copying the array, if you need to keep track of a changing upper bound of the array (for instance, if you are trying to use it as something like a stack), why not just keep a separate integer variable indicating the last real element's index? Seems a lot simpler and more efficient than the copying solutions suggested above!

Murchison answered 30/3, 2010 at 11:39 Comment(2)
Main reason: It's one more place to make a mistake. So to protect yourself, you wrap the array up in an object and pretty soon you've implemented a list. In a language like Java with all kinds of support for lists, you very well might choose something like an ArrayList. The PFC has list support, but I've always felt like the implementation was clunky and I've never used it.Thrilling
Also because of the scope of the array between my windows and user objects, it's easier trying to keep 1 item in scope then have to worry about a separate variable that's a derivative of the first.Dowery

© 2022 - 2024 — McMap. All rights reserved.