Description ​
Returns the length (size) of the array
Arguments ​
any[]
- The array to get the length of
Returns ​
number
- The length of the array
Example ​
msc
// Predefines
using Console;
using Array;
// EXAMPLE 1 - Illustration of Array.Length
string[] sentence = new string[5];
sentence[1] = "Sometimes";
sentence[2] = "I";
sentence[3] = "dream";
sentence[4] = "about";
sentence[5] = "cheese"; // CHEEEEEEEESE
Console.WriteLine("There are " .. Array.Length(sentence) .. " words in this sentence!");
// OUTPUT: "There are 5 words in this sentence!"
// EXAMPLE 2 - Application of Array.Length
number[] intervals = [1, 2, 5, 10];
// Calculate the length once. In a loop, every time it's done with an iteration, it recalculates the condition to check if it needs to proceed
// with the next iteration. Calling Array.Length every time in between iterations can be expensive.
number len = Array.Length(intervals);
number i = 1;
while(i < len) // Not counting the last index, so we don't error by indexing out of bounds
{
// Calculate the interval
number nextIndex = i + 1;
number interval = intervals[nextIndex] - intervals[i];
Console.WriteLine("[Index pair " .. i .. ", " .. nextIndex .. "] Interval: " .. interval);
i = i + 1;
}
// OUTPUTS:
// [Index pair 1, 2] Interval: 1
// [Index pair 2, 3] Interval: 3
// [Index pair 3, 4] Interval: 5