Core Function UBound

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
m (1 revision)
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
UBound( <array/binary-array>, <flag> )
+
UBound( <array/binary-array> )
 
</pre>
 
</pre>
  
 
=== Description ===
 
=== Description ===
  
Returns the size of array (How many elements it currently has stored).
+
Returns the index of the highest element in an array .
  
 
=== Parameters ===
 
=== Parameters ===
Line 16: Line 16:
  
 
The binary variable to use.
 
The binary variable to use.
 
==== flag ====
 
 
For use with arrays.
 
<pre>
 
0 = Normal just count the array size (Default)
 
1 = Count the array and the hash size
 
2 = Count just the hash size
 
</pre>
 
  
 
=== Return Value ===
 
=== Return Value ===
  
Success - Returns the current size of the array/binary-array.
+
Success - Returns the highest index in the array/binary-array.
  
Failure - Returns an empty array.
+
Failure - Returns -1.
  
 
=== Remarks ===
 
=== Remarks ===
  
Remember that the value returned by UBound is one greater than the index of an array's last element!
+
Although this gets the highest index it may not get how many items there are in the array use [[Core Function Count|Count( <array/binary-array> )]] for that.
  
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.
+
If you want the lowest index instead of the highest use [[Core Function LBound|LBound( <array/binary-array> )]] for that.
 
+
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 ===
 
=== Example ===
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$arrayOLD = array ( 10..15, 24..30 );
+
$array = array ("One", "Two", "Three");
 
+
println("Highest index is: " . UBound($array) );
$i = 0;
+
Foreach( $arrayOLD as  $j )
+
{
+
println("Element ($i) is: " . $j);
+
$i++;
+
}
+
 
+
println("Size is: " . UBound($arrayOLD) ); // Prints 13
+
</syntaxhighlight>
+
 
+
Heres an example of getting the size of binary data :
+
 
+
<syntaxhighlight lang="sputnik">
+
$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) );
+
}
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

UBound( <array/binary-array> )

Contents

Description

Returns the index of the highest element in an array .

Parameters

array/binary-array

The array to use.

OR

The binary variable to use.

Return Value

Success - Returns the highest index in the array/binary-array.

Failure - Returns -1.

Remarks

Although this gets the highest index it may not get how many items there are in the array use Count( <array/binary-array> ) for that.

If you want the lowest index instead of the highest use LBound( <array/binary-array> ) for that.

Example

$array = array ("One", "Two", "Three");
println("Highest index is: " . UBound($array) );
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox