BlitzCoder Essentials
•
Home Page
•
About BlitzCoder
•
Contributors
•
Terms of Use
•
Email Us
Main Areas
•
BlitzCoder Chat
•
Discussions
•
Articles/Tutorials
•
Code Database
•
Link Database
•
Showcase Area
•
Worklogs
•
Competitions
Special Areas
•
Undocumented
Other Blitz Sites
•
Blitz Basic Home
•
Blitz Showcase
•
BlitzPlay Library
Forum Login
Username:
Password:
•
Register Now!
BlitzCoder Code Archives Page
Main Codes Page
"BlitzArrays[tm]"
, by thechange
A refined explanation of the undocumented alternative to normal arrays called BlitzArrays[tm].
Code
;------------------------------------------------------------------------------; ; There is another type of arrays besides normal arrays. You should be familiar ; with normal arrays. Dim Array( 100 ) For Element = 0 To 100 Array( Element ) = Element Next ;------------------------------------------------------------------------------; ; BlitzArrays[tm] can be used in the same way, but there are some significant ; differences, which will be explained below. Global BlitzArray[ 100 ] For Element = 0 To 100 BlitzArray[ Element ] = Element Next ;------------------------------------------------------------------------------; ; First of all, BlitzArrays cannot be multidimensional, so you'll need special ; maths to fold multiple dimensions into one dimension. Similar to the RGB value ; stored when using ReadPixel or WritePixel. You can check out the RGB value ; conversion routines in the Code Database for more information here: http://www.blitzcoder.com/cgi-bin/code/code_showentry.pl?id=thechange05282002163030&comments=no ;------------------------------------------------------------------------------; ; Secondly, the size of BlitzArrays is static, which means it cannot change ; during run-time. Also, the array cannot be cleared with a single call like Dim ; for normal arrays. Const BlitzArraySize = 100 Global BlitzArray[ BlitzArraySize ] For Element = 0 To BlitzArraySize BlitzArray[ Element ] = 0 Next ;------------------------------------------------------------------------------; ; The third difference, which can be of great advantage, is that you can decide ; whether the array is Global or Local. A local BlitzArray could be used like a ; bunch of temporary variables inside a function, which makes it easy to store ; and iterate through a multitude of things on the fly. Const BlitzArraySize = 255 Local BlitzArray[ BlitzArraySize ] For Element = 0 To BlitzArraySize BlitzArray[ Element ] = Rnd ( -100 , 100 ) Next ;------------------------------------------------------------------------------; ; And another example of a function that puts a string in temporary array. Not ; very useful though :) But just for demonstration. Function DumpString ( Text$ ) Local BlitzArray[ 256 ] For Character = 1 to Len ( Text$ ) BlitzArray[ Character ] = Mid ( Text , Character , 1 ) Next End Function DumpString "The string that went away" ;------------------------------------------------------------------------------; ; If you want more examples, here's a link to another not fully documented side ; to arrays: http://www.blitzcoder.com/cgi-bin/code/code_showentry.pl?id=thechange08022002180240&comments=no ;------------------------------------------------------------------------------; ; The final difference is that you can use a BlitzArray as a function parameter. ; This enables you to let the function alter a BlitzArray that doesn't even have ; to be global. More information on that below. For the rest everything is the ; same as normal arrays. You can declare arrays of various types (including your ; own custom types). Const TotalNames% = 4 .Names Data "John Doe" Data "Jane Doe" Data "John Smith" Data "Mrs. Hits" Local Names$[ TotalNames ] Local Name% Restore Names For Name = 1 to TotalNames Read Names[ Name ] Next ;------------------------------------------------------------------------------; ; Unfortunately a BlitzArray cannot be directly returned from a function. To do ; this, you'll have to know where BlitzArrays come from. BlitzArrays are in fact ; the same as array-fields inside an object. Type Thing Field BlitzArray[ 100 ] End Type ;------------------------------------------------------------------------------; ; You can return a BlitzArray using a reference to an existing object. Function GimmeNumbaz.Thing () Local NewThing.Thing = New Thing For Element = 0 to 100 NewThing\BlitzArray[ Element ] = Element Next Return NewThing End Function Local MyThing.Thing = GimmeNumbaz () Print MyThing\BlitzArray[ 100 ] ; Element 100 contains value '100' ;------------------------------------------------------------------------------; ; Ofcourse, you can make this immensely complex when defining a BlitzArray of ; a type containing another BlitzArray :) Const Size% = 1024 Type Container Field Section%[ Size ] End Type Local Carrier.Container[ Size ] For Instance = 1 To Size Carrier[ Instance ] = LoadContainer ( Instance ) Next Function LoadContainer.Container ( Selected ) Local Container.Container = New Container For Section = 1 To Size Container\Section[ Section ] = Section + Selected * Size Next Return Container End Function ;------------------------------------------------------------------------------; ; If you're ready, here's a link that explains, in short, how to pass a ; BlitzArray onto a function as parameter, which call looks a lot like Object ; Oriented Programming. http://www.blitzcoder.com/cgi-bin/code/code_showentry.pl?id=ashcroftman07192002114143&comments=no ;------------------------------------------------------------------------------; ; And if you're really hungry, you can also check out the Object and Handle ; commands (also undocumented) which enable a certain type of polymorphism ; (one function with different parameter types) which you can find here: http://www.blitzcoder.com/cgi-bin/code/code_showentry.pl?id=thechange08052002184613&comments=no ;------------------------------------------------------------------------------; ; Have fun :D ;------------------------------------------------------------------------------;
Copyright(c) 2000-2004, BlitzCoder. All Rights Reserved.
Code software created by Krylar's Kreations