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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

initializing a two-dimentional array with data

Status
Not open for further replies.

steve4king

IS-IT--Management
Feb 6, 2007
154
US
OK, so I know I've done this in C# and Java..

I know I can do this like:

Dimention Arr[5,3]
Arr[1,1] = "1a"
Arr[1,2] = "1b"
Arr[1,3] = "1c"
Arr[2,1] = "2a"
Arr[2,2] = "2b"
Arr[2,3] = "2c"

But.. I'd like to do it something like
Insert(Arr[] = new string[,]{"1a","1b","1c"})

Is there something like this that is available without creating my own function?

Thanks,
-Stephen
 
Arg, no edit haha.
Mixed up a couple of ideas at once in my psuedo code, Meant more like
Code:
Arr[,] = new string[2,3] { {"1a","1b","1c"}, {"2a","2b","2c"} };
or better yet
Code:
AInsert(Arr,{"1a","1b","1c"})
 
Yeah, I looked at that. Alines() is better suited for single dimension arrays.
I'll just write a simple function for this. Just didn't want to reinvent the wheel if I was missing something.

Thanks,

-
Stephen

 
Stephen,

We haven't got anything quite like that in VFP. There's the AINS() function, but that merely inserts an empty row or element, and pushes the subsequent rows or elements down one place.

Have you thought of using a collection rather than an array? In general, that's a lot more flexible, though it might not be worth it if it means making extensive changes to your existing code.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Alines is a fine basis for this, but it's even simpler to make use of cursors instead of arrays in many cases. If you loop an array, you scan a cursor instead. And in most cases it's a fine thing cursor fields have names and not just an element index, like arrays elements have. In a way that field names make cursors more like associative PHP arrays, where you address elements by names.

Aside of that I use the following routine to fill an array from a TEXT..ENDTEXT passage:

Code:
TEXT TO lcArray NOSHOW
"a";"b";"c"
"d";"e";"f"
ENDTEXT

Local Array laArray[ALines[laRows, lcArray,1+2),3]
For lnRow = 1 To Alen(laRows,1)
   lcRow = laRows[lnRow]
   For lnField = 1 To Alines(laRow, lcRow,1+1,";")
      laArray[lnRow,lnField] = Evaluate(laRow[lnField])
   Endfor
Endfor

You may say this is much code, but put it in a function and it's just a call turning a string with n lines and m columns to an n,m array.

Using Evaluate() allows you to define the array elements in the TEXT..ENDTEXT section and still have any data type, but in case of this sample this means string delimiters around each letter. Choosing ; as seperator allows you to also use expressions including function calls with several parameters, which of course need comma.

In my case the number of columns is fixed (and more than in this example). But instead of 3 you could of course make that a parameter or determine it by the first ALINES(laRow...).

Also you need to know, you can't really put an array into an array element, if you need such nesting of array rather use collection objects. You can of course add a collection object to another one.

Bye, Olaf.
 
I thought about just using a cursor.. I don't believe there are really any performance differences, but I find myself avoiding cursors unless I'm already working with data tables.


Here's what I ended up using:
Code:
**====================================
PROCEDURE ArrInsert
**====================================
   LPARAMETERS oThisForm,var1,var2,var3
   LOCAL nAlen,cline,oErr as Exception
	
   TRY
      nAlen = IIF(EMPTY(oThisForm.aInvoiceTypes[1,1]),ALEN(oThisForm.aInvoiceTypes,1),ALEN(oThisForm.aInvoiceTypes,1) + 1)
      DIMENSION oThisForm.aInvoiceTypes[nAlen,3]	

      oThisForm.aInvoiceTypes[nAlen,1] = var1
      oThisForm.aInvoiceTypes[nAlen,2] = var2	
      oThisForm.aInvoiceTypes[nAlen,3] = var3

   CATCH TO oErr
      MESSAGEBOX("arrinsert: " + oErr.Message)
   
   ENDTRY 
ENDPROC

It's not flexible enough to be reused, which I don't like. But it reduces the rest of my code immensely and works well for the intended function.
 
You see it's awfully complicated to add an array row, but it's simple adding a row to a cursor. That's not all of it, of course.

I like to work with TEXT..ENDTEXT sections defining some constant data, no matter if for an array, cursor or other variables. You might also use a dbf and include it into compilation, making it part of the EXE. It's as good as constant data. And after reading it into a readwrite cursor you may add or change data as needed at runtime, too.

Bye, Olaf.
 
Stephen,

Your procedure doesn't really solve the problem, becuase it is specific to the aInvoicesType array. You would have to write a slightly different procedure to use it with another array, or to insert more (or less) than three columns.

You'd be better off writing a more generic procedure which inserts any number of columns into any array. However, I must agree with Olaf: by the time you've done that, you might as well use a cursor. It really is much simpler. All you need is a simple CREATE CURSOR command - which I accept is more complicated than a DIMENSION or DECLARE, but not that much - plus an INSERT command to actually insert the data. It has the advantage that you can name the fields, rather than relying on column numbers, and you can perform all the operations that can be done with an array, like scanning and sorting.

Or, consider my earlier suggestion of using a collection. In some cases, that would be the ideal solution. Most of the time, though, I would go with a cursor.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top