Core Function UBound

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
 
(3 intermediate revisions by one user not shown)
Line 5: Line 5:
 
=== 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 19: Line 19:
 
=== 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.
  
In Sputnik an array does not necessary start at index 0 and move upwards infact an array can start at any number and jump all over place this all depends where the user has placed items into the array.
+
If you want the lowest index instead of the highest use [[Core Function LBound|LBound( <array/binary-array> )]] for that.
 
+
If the user uses push() then all items in array will be correct order.
+
  
 
=== 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