[Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register]

Author Message
DannyB
Moderator

Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

Posted: Sat Dec 20, 2003 12:51 pm    Post subject: Array Semantics

This thread continued from....
http://www.oooforum.org/forum/viewtopic.php?p=17286#17286

This thread will be about the semantics of arrays in OOo Basic.

The kind of documentation you're looking for still needs to be written.

But honest, once you learn how to "surf" the API reference, and cross reference it with the Developer's Guide, and do lots of first hand experiments, and keep a lab notebook, you can really figure out just about anything about OOo.

It's a detective game.

By the late 1800's Maxwell's electrodynamics equations had proven that the speed of an electromagnetic wavefront always moved at speed c regardless of the motion of the source. This left 19th century physics in a real mess at the end of the 19th century. After all, this doesn't jive with Newton's laws of motion, which had zillions (well hundreds) of years of experimental evidence backing them up. If you're on the sidewalk, and I'm in a car moving towards you at 50 mph, and I throw you an OOo CDROM at 50 mph, then the CDROM is moving towards you at 100 mph. Simple enough. If I climed out on the hood and used a device to measure the speed of light leaving my headlights I would measure c. So you standing on the sidewalk holding a similar device should measure my arriving headlight speed at 50 mph + c right? Wrong. You will measure c also, not c + 50 mph. Maxwell's electrodynamics proves this result. The problem is that we make an assumption so fundamental that we just take it for granted. This assumption is wrong. We assume that you (on sidewalk) and me (moving toward you at 50 mph) can agree on what a second is, and agree on what a mile is. Both are wrong. When I am moving, I have my own idea of spacetime, and you have yours. We do not agree on the definition of either length or time. So indeed I measure speed c (which is 186,281.2 miles per second +/- 0.04 mps) and you measure speed c of my headlights, but your notion of "miles" and "seconds" is not the same as my "miles" and "seconds". Indeed, we not only disagree on such fundamental things as length and time, but we cannot even agree on the order that two events take place! Did event A take place before event B, or vice versa? I've been reading about this subject all summer long up until about late October. My local public library has a number of excellent books on the subject. (Was Einstien Right by Clifford Will, and Relativity by Max Born, were both especially excellent.) Each observer divides spacetime up into their own notion of space and time. That's just the fundamental nature of the universe, the way God made it. Not at all what Newton expected.

Now, just do what Einstein did. Connect all the dots together and arrive at a coherent picture of the reality. The semantics of arrays seem like what you would expect from other Basic dialects. But it is not. In fact, OOo Basic arrays follow the semantics of arrays in Java or Python. It just doesn't seem like it should.

Dim x( 4 ) As String

Is really more like the effect of saying (in Java)....

String x[] = new String[ 4 ];

Indeed, variable x now contains an array. That array can be passed as a parameter or returned as a function result. The "Dim" statement merely "assigned" a new array to variable x. Re-read that last sentence.

So even though the syntax of "Dim" is cleverly designed to mislead you into thinking it has the semantics of other Baisc dialects, it merely "assigns" an array object to a variable. That variable can be assigned to another variable, passed to a function, or returned as a function result.

The Dim statement does cause x to be declared as an array of strings, so you cannot re-use x in a different way. But look at this. (p.s. don't try this at home kids!)

 Code: Sub Main    Dim x( 4 ) As String    x(0) = "zero"    x(1) = "won"    x(2) = "too"    x(3) = "free"    x(4) = "fore"        y() = x()           Print y(3)        x(3) = "freedom"        Print y(3) End Sub

What do you expect the first Print statment to print? The second?

Do you see what happened with the assignment y() = x() ?

An entire array object is assigned such that both x and y now have a reference to the same array object. The first Print says "free", and the second one says "freedom".

Want to see how to return an array as a function result?

 Code: Sub Main    TestFnReturningArray() End Sub Sub TestFnReturningArray()    oArray1 = FnReturningArray1()    bOkay = oArray1( 0 ) = "zero"  And  oArray1( 1 ) = "one"  And  oArray1( 2 ) = "two"    If bOkay Then       MsgBox( "Returnning an array1 successful." )    Else       MsgBox( "Returnning an array1 failed." )    EndIf    oArray2 = FnReturningArray2()    bOkay = oArray2( 0 ) = "zero"  And  oArray2( 1 ) = "one"  And  oArray2( 2 ) = "two"    If bOkay Then       MsgBox( "Returnning an array2 successful." )    Else       MsgBox( "Returnning an array2 failed." )    EndIf End Sub ' This technique works. Function FnReturningArray1() As Variant    Dim oArray( 2 )    oArray( 0 ) = "zero"    oArray( 1 ) = "one"    oArray( 2 ) = "two"        FnReturningArray1() = oArray() End Function ' This technique works. Function FnReturningArray2()    oArray = Array( "zero", "one", "two" )        FnReturningArray2() = oArray() End Function

Note that FnReturningArray2 and FnReturningArray1 have the same effect. They both return a three element array of strings. One uses a Dim statement, and the other uses an Array() function.

Once you have an array, you can ReDim it to a different size.

 Code: Sub Main    oArray = Array()        ReDim oArray( 2 )    oArray( 0 ) = "zero"    oArray( 1 ) = "one"    oArray( 2 ) = "two"        print oArray( 0 ), oArray( 1 ), oArray( 2 )    ' ReDim completely destroys an array's previous content.    ReDim oArray( 3 )    oArray( 3 ) = "three"        print oArray( 0 ), oArray( 1 ), oArray( 2 ), oArray( 3 ) End Sub

But if you use ReDim Preserve, you can keep the original contents of the array...

 Code: Sub Main    oArray = Array()        ReDim oArray( 2 )    oArray( 0 ) = "zero"    oArray( 1 ) = "one"    oArray( 2 ) = "two"        print oArray( 0 ), oArray( 1 ), oArray( 2 )    ' ReDim completely destroys an array's previous contents,    '  unless you use ReDim Preserve.    ReDim Preserve oArray( 3 )    oArray( 3 ) = "three"        print oArray( 0 ), oArray( 1 ), oArray( 2 ), oArray( 3 ) End Sub

You can use LBound() and UBound() to get the bounds of an array "object".

 Code: o = DimArray( 3, 2 )    Print LBound( o, 1 ), UBound( o, 1 ), LBound( o, 2 ), UBound( o, 2 )

Also note in the above example that I used the DimArray() function to create a 3 row by 2 column array without using a "Dim" statement.

Now using everything you learned in this thread, you can build yourself a few library functions which can give you a primitive form of "container" objects in Basic.

Hope this is helpful.

So on the order of two events A and B? Did I save that OOo document before I wrote it?
_________________
Want to make OOo Drawings like the colored flower design to the left?
larsenkc
Guest

Posted: Mon Dec 22, 2003 2:08 am    Post subject: experimentation

 DannyB wrote: But honest, once you learn how to "surf" the API reference, and cross reference it with the Developer's Guide, and do lots of first hand experiments, and keep a lab notebook, you can really figure out just about anything about OOo.

CS is not supposed to be an experimental science. But I have a joke:

A programmer and a mechanic are driving in a car. They come to a hill, and while going down, the brakes fail. They lose control and hit a tree at the bottom. Fortunately, neither is injured, and the mechanic gets under the hood and starts trying to figure out what went wrong. The programmer is muttering to himself about the bright idea of putting computers inside cars, and finally says, "Alright, let's push it back up the hill, roll down the windows, and try again!"

Seriously, I've been trying for 1/2 hour to figure out what's wrong w/ these lines

 Code: cards = StarDesktop.LoadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() ) OpenScratch = cards.getText()

I pasted this into 2 other macros, and it worked, but it didn't work in the macro I wanted it to be in. Suddenly I realized the module (I think) was named 'cards'. Go figure. In the process, I had an array which apparently could not be referenced. I learned that I cannot assign an element of a string array to a variable called 'str'. That, of course, is a function, but it appears green, so I didn't know it was a keyword.

Bjorne Stroustrup, who wrote C, says that once a programmer learns Basic, he is ruined and cannot be useful in any other language. Or something along those lines. After all this wrestling with strange errors and lack of documentation, I'm beginning to understand how that can be true, at least for OOBasic. The reason the programmer is useless is because he has gone completely insane.

 Quote: The Dim statement does cause x to be declared as an array of strings, so you cannot re-use x in a different way. But look at this. (p.s. don't try this at home kids!)

So, x is a read-only pointer variable. Is there any other language that has such an animal?

The strangest thing is that when you say "dim x(4)", you get an array of 5 elements. I swear this one must be a bug documented as a feature.

Okay, I'm going back to my padded cell now...
larsenkc
Guest

 Posted: Mon Dec 22, 2003 6:45 am    Post subject: strange doings Isn't 'eqv' supposed to work the same as =? I was using (strcomp(start, "") eqv 0) 'start is a string as a test for an if statement, and it kept coming out true, no matter what the value of start. Then I changed it to (strcomp(start, "") = 0) and suddenly it worked. But I've been using 'eqv' in other places successfully, to test whether a string is empty. Any explanations?
erikanderson3
OOo Advocate

Joined: 25 Feb 2004
Posts: 332
Location: San Francisco peninsula

Posted: Tue Mar 16, 2004 5:43 pm    Post subject:

From the OOo Basic helpfiles:

 OpenOffice.org Help wrote: Eqv-Operator [Runtime] Calculates the logical equivalence of two expressions. Syntax: Result = Expression1 Eqv Expression2 Parameters: Result: Any numeric variable that contains the result of the comparison. Expression1, expression2: Any expressions that you want to compare. When testing for equivalence between Boolean expressions, the result is True if both expressions are either True or False. In a bit-wise comparison, the Eqv operator only sets the corresponding bit in the result if a bit is set in both expressions, or in neither expression. Example Sub ExampleEqv Dim vA as Variant, vB as Variant, vC as Variant, vD as Variant Dim vOut as Variant vA = 10: vB = 8: vC = 6: vD = Null vOut = A > B Eqv B > C REM returns -1 vOut = B > A Eqv B > C REM returns -1 vOut = A > B Eqv B > D REM returns -1 vOut = (B > D Eqv B > A) REM returns 0 vOut = B Eqv A REM returns -1 End Sub

Running a little check like so:
 Code: Sub EqvTester Dim start as string     start = "Alfred" Dim i as integer     i = 0        for i = 0 to 1           if i = 1 then start = ""           if (strcomp(start, "") eqv 0) then          msgbox "True!" & chr(10) & chr(10) & "start = " & _             start & chr(10) & chr(10) & "Eval:  " & (strcomp(start, "") eqv 0)       endif       if (strcomp(start, "") = 0) then          msgbox "True again!" & chr(10) & chr(10) & "start = " & _             start & chr(10) & chr(10) & "Eval:  " & (strcomp(start, "") = 0)       endif        next i End Sub

shows what might be unexpected results for how eqv evaluates. Consequently, I think I'd use "=" unless there's some compelling reason otherwise ...

Cheers,

Erik
DannyB
Moderator

Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

 Posted: Sat Apr 09, 2005 8:30 am    Post subject:
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 8 Hours Page 1 of 1

 Jump to: Select a forum OpenOffice.org Forums----------------Setup and TroubleshootingOpenOffice.org WriterOpenOffice.org CalcOpenOffice.org ImpressOpenOffice.org DrawOpenOffice.org MathOpenOffice.org BaseOpenOffice.org Macros and APIOpenOffice.org Code Snippets Community Forums----------------General DiscussionSite Feedback
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group