Core Function UBound
(→Example) |
|||
Line 34: | Line 34: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $arrayOLD = array ( 10..15, 24..30 ) | + | $arrayOLD = array ( 10..15, 24..30 ); |
− | $i = 0 | + | $i = 0; |
− | + | Foreach( $arrayOLD as $j ) | |
− | println("Element ($i) is: " . $i) | + | { |
− | $i++ | + | println("Element ($i) is: " . $i); |
− | + | $i++; | |
+ | } | ||
− | println("Size is: " . UBound($arrayOLD) ) ; Prints 13 | + | println("Size is: " . UBound($arrayOLD) ); // Prints 13 |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 48: | Line 49: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $binary = Pack("z0", "Hello World!") | + | $binary = Pack("z0", "Hello World!"); |
− | ; Yes you can get the size of the binary byte array by using UBound just like with arrays | + | // Yes you can get the size of the binary byte array by using UBound just like with arrays |
− | println("The binary size is: " . UBound($binary) ) | + | println("The binary size is: " . UBound($binary) ); |
− | + | Foreach ( $binary as $i ) | |
− | println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ) | + | { |
− | + | println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); | |
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Revision as of 12:11, 29 November 2011
UBound( <array/binary-array> )
Contents |
Description
Returns the size of array (How many elements it currently has stored).
Parameters
array/binary-array
The array to use.
OR
The binary variable to use.
Return Value
Success - Returns the current size of the array/binary-array.
Failure - Returns an empty array.
Remarks
Remember that the value returned by UBound is one greater than the index of an array's last element!
So if theres only 5 element in the array UBound will return 5 however there is no element at index 5 since elements begin at index 0 this means the first item in the array is index 0 even though you might think of it as being index 1.
Warning - Arrays in Sputnik do not have a fixed size it will suddenly increase in size if you try access an element of the array for example; If you have an array with only 3 elements and you try access element 77 the array will instantly increase in size to 77 and anything that was added as a result of this will be empty strings making element 77 an empty string.
Example
$arrayOLD = array ( 10..15, 24..30 ); $i = 0; Foreach( $arrayOLD as $j ) { println("Element ($i) is: " . $i); $i++; } println("Size is: " . UBound($arrayOLD) ); // Prints 13
Heres an example of getting the size of binary data :
$binary = Pack("z0", "Hello World!"); // Yes you can get the size of the binary byte array by using UBound just like with arrays println("The binary size is: " . UBound($binary) ); Foreach ( $binary as $i ) { println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); }