Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use multidimensional arrays ???

Status
Not open for further replies.

netmadcap

Programmer
Jul 1, 2003
10
US
Hi PB Gurus ...

I am very new to PowerBuilder. I need to define a multi dimensional array, how can i do it ? I am using Desktop PB 7.0.

Say, I want to store the following personal data in an array, how would I store it and how will I access it ?

Id,First,Last,Email,Position,Salary
[1,John,Doe,jdoe@company.com,Manager,9000]
[2,Bill,Walker,bill_walker@yahoo.com,UnixAdmin,7500]
[3,Jack,Smith,jack@ofall.com,DBA,4000]

Can somebody give syntax for inserting data into arrays, accessing various elements from array and deleting elements from array ?

Thanks in advance !
 
From the PB7 online books:

Multidimensional array
In a multidimensional array, you still provide the values in a simple, comma-separated list. When the values are assigned to array positions, the first dimension is the fastest-varying dimension and the last dimension is the slowest-varying. In other words, the values are assigned to array positions by looping over all the values of the first dimension for each value of the second dimension, then looping over all the values of the second dimension for each value of the third, and so on.
Assigning values
You can assign values to an array after declaring it using the same syntax of a list of values within braces:
integer li_Arr[]
Li_Arr = {1, 2, 3, 4}

Examples
Example 1 This statement declares an initialized 1-dimensional array of three variables:
real lr_Rate[3]={1.20, 2.40, 4.80}

Example 2 This statement initializes a 2-dimensional array:
integer li_units[3,4] = {1,2,3, 1,2,3, 1,2,3, 1,2,3}
As a result:
Li_units[1,1], [1,2], [1,3], and [1,4] are all 1
Li_units[2,1], [2,2], [2,3], and [2,4] are all 2
Li_units[3,1], [3,2], [3,3], and [3,4] are all 3

There are more examples to be found there.
 
I often use a freeform data window for this kind of requirement. It provides some of the search capabilities you may want without having to write all of the insert/delete code for working with arrays. You can easily load up a freeform data window from delimited text files too (see ImportFile). One down side to this approach though is that "order by" doesn't seem to work in this kind of window and I don't think you can share the data buffers with another datawindow. You would have the usual data window methods to search, insert, modify, etc. This could be implemented as with any other datawindow as a visible or non-visible object.

If you decide to use an array you might want to consider making it a single dimension array of structures. That will allow you to have the effect of storing multiple data types within one array element. But I think you'll wind up creating alot of code to do functions that are already available in data windows.
 
You can define a structure with required fields like id,name,etc in the structure painter.

Then in code define like (eg.structure name is str_detail)

str_detail ldstr_detail[]

This will create an array of structures.

To access use ldstr_detail[1].id
ldstr_detail[1].name

etc.

Hope u got this





 
wow ! this is cool .. i guess, the advantage of using structure may be that even if i add any more elements to the structure, nothing would change. is that right ???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top