| View previous topic :: View next topic |
| Author |
Message |
r_vinoya Super User


Joined: 03 Dec 2003 Posts: 619 Location: Somewhere in the Philippines
|
Posted: Fri Sep 17, 2004 12:23 am Post subject: Show/Print contents of Array Without looping? |
|
|
Is there a way that I can print/msgbox the contents of an array without having to create a loop.... sort of like printr(my_array())? _________________ # : - ) |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Fri Sep 17, 2004 5:19 am Post subject: |
|
|
See the Join function. It is part of the OOo Basic language, therefore, see it in OOo's help about the Basic language.
The Split function is the opposite. (Takes a string, and returns an array, splitting the string into seperate elements when it encounters some character, such as a comma, or carriage return, etc.)
Join is useful if your array is an array of strings. Join creates a single string from an array of strings. You can specify a string that seperates each of the joined strings.
See this example.
| Code: | Sub Main
' Create a spreadsheet.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, Array() )
' Get the collection of sheets.
oSheets = oDoc.getSheets()
' Get an array of the sheet names.
aSheetNames = oSheets.getElementNames()
' At this point, aSheetNames contains an ARRAY of sheet names.
' This is just as if we had written something like....
' Dim aSheetNames( 4 ) As String
' aSheetNames( 0 ) = "something"
' aSheetNames( 1 ) = "doodah"
' ....
' aSheetNames( 4 ) = "hello there"
' Now join the sheet names from the array together
' into a single long string, with each array element seperated
' from the next by a carriage return (chr(13)).
' Then display the single long joined string in a MsgBox.
MsgBox Join( aSheetNames, Chr(13) )
End Sub
|
Apologies for having so many comments for only four lines of executable code. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Fri Sep 17, 2004 5:38 am Post subject: |
|
|
If you don't like the previous example, due to excessive commenting, here are two more examples that might be more helpful. Maybe not.
This example is only two lines of code. Apologies for having so many comments.
| Code: | Sub Main
' Create a writer document.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
' oDoc.getStyleFamilies() returns a collection of style families.
' getElementNames() returnes the names of the elements in the collection,
' as an array of strings.
' Join concatenates the strings in the array together, seperated by a chr(13)
' MsgBox displays the single string.
MsgBox Join( oDoc.getStyleFamilies().getElementNames(), Chr(13) ), 0, "Style Family Names"
End Sub
|
This creates a writer document, then gets the names of the style families from the document, and displays them in a MsgBox.
I could have expanded it more like this, but I wrote it on a single line.
oDoc = .....
oStyleFamilies = oDoc.getStyleFamilies()
aStyleFamilyNames = oStyleFamilies.getElementNames()
so that aStyleFamilyNames got the array of element names from the collection object which is in oStyleFamilies. (The collection implements one or more of the typical collection interfaces such as XNameAccess or XIndexAccess, which provide methnods like getByName, hasByName, getElementNames, getByIndex, getCount, etc., etc..)
The MsgBox displays....
CharacterStyles
ParagraphStyles
FrameStyles
PageStyles
NumberingStyles
This is a list of style families.
Now suppose we wanted to see a list of individual styles within a family? Let's pick the first one, CharacterStyles.
We could do this....
| Code: | Sub Main
' Create a writer document.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
' oDoc.getStyleFamilies() returns a collection of style families.
' getByName() returns a style family, which is a collection of styles, each with a name.
' getElementNames() returns an array of the individual styles within the collection.
' Join concatenates the strings in the array together, seperated by a chr(13)
' MsgBox displays the single string.
MsgBox Join( oDoc.getStyleFamilies().getByName( "CharacterStyles" ).getElementNames(), Chr(13) ), 0, "Styles within CharacterStyles"
End Sub
|
Again, apologies for having too many comments.
This one shows you the individual style names within the CharacterStyles family.
In both of the above examples, I think you can see how Join is very useful to use with MsgBox. Join is useful when your array is an array of string. Since any collection that implements XNameAccess has the method getElementNames(), the Join function can be used to easily show you the names of the elements within the collection.
As you can see here, the XNameAccess interface is used for many purposes, and other interfaces are derrived from it, and those derrived interfaces are used for many purposes. Therefore, there are many potential places where doing Join of a getElementNames() can be useful. As you can see, both com.sun.star.style.StyleFamilies and com.sun.star.style.StyleFamily both appear in this list, which is how I contrived these two examples.
The pair of examples in this message use a Writer document. But in fact, by changing the "private:factory/swriter", you can use these examples with any document type that has style families and styles.
I hope this is helpful. I'll try to stop rambling on now. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
pitonyak Administrator


Joined: 09 Mar 2004 Posts: 3618 Location: Columbus, Ohio, USA
|
Posted: Fri Sep 17, 2004 6:08 am Post subject: |
|
|
| Quote: | | This example is only two lines of code. Apologies for having so many comments. |
I do not see too many comments, looks just right to me!  _________________ --
Andrew Pitonyak
http://www.pitonyak.org/oo.php |
|
| Back to top |
|
 |
Iannz OOo Advocate

Joined: 14 Feb 2004 Posts: 494 Location: Christchurch, New Zealand
|
Posted: Fri Sep 17, 2004 6:14 pm Post subject: |
|
|
(Edit: I jumped into the previous post from the code snippets forum, posted stuff below then read that the original post was about displaying arrays without loops - oops sorry. I have left the posting as it still may be useful to some people.)
I think there is a problem with the above examples as the names returned by getElementNames and the names displayed are not always the same.
E.g. Standard v Default (there are other examples too)
To get around this problem, and to put the names into alphabetical order I use the following functions (my apologies for the variable declarations but I'm too lazy to remove them):
| Code: | function fnGetDisplayStyleNames(sStyleFamily as string)
dim mApiNames, nStyles as integer, i as integer, oStyles as object
oStyles = oDoc.styleFamilies.getByName(sStyleFamily)
mApiNames = oStyles.ElementNames
nStyles = uBound(mApiNames)
dim mStyleNames(nStyles)
for i = 0 to nStyles
mStyleNames(i) = oStyles.getByIndex(i).DisplayName
next
subShellSort(mStyleNames())
fnGetDisplayStyleNames = mStyleNames()
end function
sub subShellSort(mArray)
'Based on a routine available from: http://www.oopweb.com/Algorithms/Documents/Sman/Volume/s_vss.txt
dim n as integer, h as integer, i as integer, j as integer, t as string, Ub as integer, LB as integer
Lb = lBound(mArray)
Ub = uBound(mArray)
' compute largest increment
n = Ub - Lb + 1
h = 1
if n > 14 then
do while h < n
h = 3 * h + 1
loop
h = h \ 3
h = h \ 3
end if
do while h > 0
' sort by insertion in increments of h
for i = Lb + h to Ub
t = mArray(i)
for j = i - h to Lb step -h
if strComp(mArray(j), t, 0) < 1 then exit for
mArray(j + h) = mArray(j)
next j
mArray(j + h) = t
next i
h = h \ 3
loop
end sub |
_________________ Cheers, Ian
http://wiki.services.openoffice.org/wiki/Extensions_development_basic a wiki about writing OpenOffice.org extensions. |
|
| Back to top |
|
 |
r_vinoya Super User


Joined: 03 Dec 2003 Posts: 619 Location: Somewhere in the Philippines
|
Posted: Tue Sep 21, 2004 4:37 pm Post subject: |
|
|
Hello there... Just came back from a Four Day vacation... Actually I like the comments specially for apprentice programmer(?) like me...
I haven't tried the codes yet but i think that's what I need .
Thanks again!  _________________ # : - ) |
|
| Back to top |
|
 |
r_vinoya Super User


Joined: 03 Dec 2003 Posts: 619 Location: Somewhere in the Philippines
|
Posted: Tue Sep 21, 2004 6:31 pm Post subject: |
|
|
| DannyB wrote: | See the Join function. It is part of the OOo Basic language, therefore, see it in OOo's help about the Basic language.
The Split function is the opposite. (Takes a string, and returns an array, splitting the string into seperate elements when it encounters some character, such as a comma, or carriage return, etc.)
Join is useful if your array is an array of strings. Join creates a single string from an array of strings. You can specify a string that seperates each of the joined strings.
See this example.
| Code: | Sub Main
' Create a spreadsheet.
oDoc = StarDesktop.loadComponentFromURL( "private:factory/scalc", "_blank", 0, Array() )
' Get the collection of sheets.
oSheets = oDoc.getSheets()
' Get an array of the sheet names.
aSheetNames = oSheets.getElementNames()
' At this point, aSheetNames contains an ARRAY of sheet names.
' This is just as if we had written something like....
' Dim aSheetNames( 4 ) As String
' aSheetNames( 0 ) = "something"
' aSheetNames( 1 ) = "doodah"
' ....
' aSheetNames( 4 ) = "hello there"
' Now join the sheet names from the array together
' into a single long string, with each array element seperated
' from the next by a carriage return (chr(13)).
' Then display the single long joined string in a MsgBox.
MsgBox Join( aSheetNames, Chr(13) )
End Sub
|
Apologies for having so many comments for only four lines of executable code. |
Hello again,
I have tried the first solution here is my observation...
I am having problem with this part:
MsgBox Join( aSheetNames, Chr(13) ) '<= results in error
What I did is:
MsgBox Join( aSheetNames(), Chr(13) ) '<=this works for me
A follow-up question (I do not know if this is answered already)... What If I have an array with size of 100 (ex. Dim a(100) as string ) ... What would I do to print only those arrays which are not empty using the JOIN technique? _________________ # : - ) |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Wed Sep 22, 2004 6:41 am Post subject: |
|
|
| Quote: | | What would I do to print only those arrays which are not empty using the JOIN technique? |
I don't think that is possible with Join. You would just write a loop, or a function with a loop. The function may make the code more clear. You call a function, pass it the array, and it returns a string of the non-empty elements. The function only exists once, rather than multiple occurences of a looping construct. The function makes your main code more readable.
| Quote: | | MsgBox Join( aSheetNames(), Chr(13) ) '<=this works for me |
Very interesting.
This would probably be necessary if the array were statically declared using a Dim statement, rather than a dynamically allocated array as you would get by calling getElementNames(). _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1569 Location: Frankfurt, Germany
|
Posted: Wed Oct 20, 2004 4:44 pm Post subject: Re: Show/Print contents of Array Without looping? |
|
|
| r_vinoya wrote: | | Is there a way that I can print/msgbox the contents of an array without having to create a loop.... sort of like printr(my_array())? |
The quickest way I know of - and what I also use - is to call one of the built-in functions (thereby I mean the functions already coming with OOo versions in libraries like "Tools"):
The function's name is "ShowArray" and it takes one parameter which is the array you want to investigate.
Of course the ShowArray function itself loops through the contents so it's not functioning like the RTL function "Join", but at least you don't have to write the loop .
Christian _________________ - Knowledge is Power - |
|
| Back to top |
|
 |
|
|
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
|