Array length: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 13: | Line 13: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang=11l>print([‘apple’, ‘orange’].len)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 21: | Line 21: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
Array length is computed at compilation time with the formula : (AEND-A)/L'A |
Array length is computed at compilation time with the formula : (AEND-A)/L'A |
||
< |
<syntaxhighlight lang=360asm>* Array length 22/02/2017 |
||
ARRAYLEN START |
ARRAYLEN START |
||
USING ARRAYLEN,12 |
USING ARRAYLEN,12 |
||
Line 32: | Line 32: | ||
AEND DC 0C |
AEND DC 0C |
||
PG DC CL25'Array length=' buffer |
PG DC CL25'Array length=' buffer |
||
END ARRAYLEN</ |
END ARRAYLEN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 40: | Line 40: | ||
{{trans|360 Assembly}} |
{{trans|360 Assembly}} |
||
Array length is computed at compilation time with the formula: (Array_End-Array). Even though the labels Array and Array_End are both 16-bit values, if their difference fits into 8 bits the assembler will allow you to load it into a register. |
Array length is computed at compilation time with the formula: (Array_End-Array). Even though the labels Array and Array_End are both 16-bit values, if their difference fits into 8 bits the assembler will allow you to load it into a register. |
||
< |
<syntaxhighlight lang=6502asm>start: |
||
LDA #(Array_End-Array) ;evaluates to 13 |
LDA #(Array_End-Array) ;evaluates to 13 |
||
RTS |
RTS |
||
Line 47: | Line 47: | ||
byte "apple",0 |
byte "apple",0 |
||
byte "orange",0 |
byte "orange",0 |
||
Array_End:</ |
Array_End:</syntaxhighlight> |
||
=={{header|68000 Assembly}}== |
=={{header|68000 Assembly}}== |
||
Line 55: | Line 55: | ||
< |
<syntaxhighlight lang=68000devpac>start: |
||
MOVE.B #(MyArray_End-MyArray)/4 ;evaluates to 2 |
MOVE.B #(MyArray_End-MyArray)/4 ;evaluates to 2 |
||
RTS |
RTS |
||
Line 69: | Line 69: | ||
DC.L Apple |
DC.L Apple |
||
DC.L Orange |
DC.L Orange |
||
MyArray_End:</ |
MyArray_End:</syntaxhighlight> |
||
=={{header|8th}}== |
=={{header|8th}}== |
||
< |
<syntaxhighlight lang=forth> |
||
["apples", "oranges"] a:len . cr |
["apples", "oranges"] a:len . cr |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 82: | Line 82: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
< |
<syntaxhighlight lang=AArch64 Assembly> |
||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program lenAreaString64.s */ |
/* program lenAreaString64.s */ |
||
Line 147: | Line 147: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ABAP}}== |
=={{header|ABAP}}== |
||
The concept of arrays does not exist in ABAP, instead internal tables are used. Since ABAP Version 7.40 they can be accessed with the common index notation. Note that the index starts at 1 and out of bound access raises an exception. The built-in function "lines" returns the number of records. |
The concept of arrays does not exist in ABAP, instead internal tables are used. Since ABAP Version 7.40 they can be accessed with the common index notation. Note that the index starts at 1 and out of bound access raises an exception. The built-in function "lines" returns the number of records. |
||
< |
<syntaxhighlight lang=ABAP> |
||
report z_array_length. |
report z_array_length. |
||
Line 158: | Line 158: | ||
write: internal_table[ 1 ] , internal_table[ 2 ] , lines( internal_table ). |
write: internal_table[ 1 ] , internal_table[ 2 ] , lines( internal_table ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 167: | Line 167: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang=Ada>with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Array_Length is |
procedure Array_Length is |
||
Line 180: | Line 180: | ||
Ada.Text_IO.Put_Line (" Array Size : " & Integer'Image (Fruits'Length)); |
Ada.Text_IO.Put_Line (" Array Size : " & Integer'Image (Fruits'Length)); |
||
end Array_Length;</ |
end Array_Length;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 186: | Line 186: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang=algol68># UPB returns the upper bound of an array, LWB the lower bound # |
||
[]STRING fruits = ( "apple", "orange" ); |
[]STRING fruits = ( "apple", "orange" ); |
||
print( ( ( UPB fruits - LWB fruits ) + 1, newline ) ) # prints 2 #</ |
print( ( ( UPB fruits - LWB fruits ) + 1, newline ) ) # prints 2 #</syntaxhighlight> |
||
=={{header|AntLang}}== |
=={{header|AntLang}}== |
||
< |
<syntaxhighlight lang=AntLang>array: seq["apple"; "orange"] |
||
length[array] |
length[array] |
||
/Works as a one liner: length[seq["apple"; "orange"]]</ |
/Works as a one liner: length[seq["apple"; "orange"]]</syntaxhighlight> |
||
=={{header|Apex}}== |
=={{header|Apex}}== |
||
< |
<syntaxhighlight lang=apex>System.debug(new String[] { 'apple', 'banana' }.size()); // Prints 2</syntaxhighlight> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
<lang |
<syntaxhighlight lang=apl>⍴'apple' 'orange'</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>2</pre> |
<pre>2</pre> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang=AppleScript> |
||
set theList to {"apple", "orange"} |
set theList to {"apple", "orange"} |
||
count theList |
count theList |
||
Line 211: | Line 211: | ||
-- or |
-- or |
||
number of items in theList |
number of items in theList |
||
</syntaxhighlight> |
|||
</lang> |
|||
''Strictly speaking, 'items in' can be omitted from the last example, since 'number of' does essentially the same as 'length of'. The additional stage of extracting theList's 'items' is inefficient.'' |
''Strictly speaking, 'items in' can be omitted from the last example, since 'number of' does essentially the same as 'length of'. The additional stage of extracting theList's 'items' is inefficient.'' |
||
{{out}} |
{{out}} |
||
Line 222: | Line 222: | ||
<pre>fold (λx n -> 1 + n) 0</pre> |
<pre>fold (λx n -> 1 + n) 0</pre> |
||
< |
<syntaxhighlight lang=AppleScript>on run |
||
set xs to ["alpha", "beta", "gamma", "delta", "epsilon", ¬ |
set xs to ["alpha", "beta", "gamma", "delta", "epsilon", ¬ |
||
Line 263: | Line 263: | ||
end repeat |
end repeat |
||
end fold |
end fold |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 271: | Line 271: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
< |
<syntaxhighlight lang=ARM Assembly> |
||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program lenAreaString.s */ |
/* program lenAreaString.s */ |
||
Line 407: | Line 407: | ||
.Ls_magic_number_10: .word 0x66666667 |
.Ls_magic_number_10: .word 0x66666667 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang=rebol>fruit: ["apple" "orange"] |
||
print ["array length =" size fruit]</ |
print ["array length =" size fruit]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 420: | Line 420: | ||
=={{header|ATS}}== |
=={{header|ATS}}== |
||
< |
<syntaxhighlight lang=ATS> |
||
#include |
#include |
||
"share/atspre_staload.hats" |
"share/atspre_staload.hats" |
||
Line 433: | Line 433: | ||
implement main0((*void*)) = ((*void*)) |
implement main0((*void*)) = ((*void*)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang=AutoHotkey>MsgBox % ["apple","orange"].MaxIndex()</syntaxhighlight> |
||
{{Out}}<pre>2</pre> |
{{Out}}<pre>2</pre> |
||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
< |
<syntaxhighlight lang=AutoIt>Opt('MustDeclareVars',1) ; 1 = Variables must be pre-declared. |
||
Local $aArray[2] = ["Apple", "Orange"] |
Local $aArray[2] = ["Apple", "Orange"] |
||
Line 449: | Line 449: | ||
ConsoleWrite("aArray[" & $i & "] = '" & $aArray[$i] & "'" & @CRLF) |
ConsoleWrite("aArray[" & $i & "] = '" & $aArray[$i] & "'" & @CRLF) |
||
Next |
Next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Elements in array: 2 |
<pre>Elements in array: 2 |
||
Line 459: | Line 459: | ||
Using Avail's tuples and the `|_|` method: |
Using Avail's tuples and the `|_|` method: |
||
< |
<syntaxhighlight lang=Avail>|<"Apple", "Orange">|</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 468: | Line 468: | ||
Another method to count the elements of the array is by using a variant of for(). |
Another method to count the elements of the array is by using a variant of for(). |
||
< |
<syntaxhighlight lang=awk># usage: awk -f arraylen.awk |
||
# |
# |
||
function countElements(array) { |
function countElements(array) { |
||
Line 482: | Line 482: | ||
print "String length:", array[1], length(array[1]) |
print "String length:", array[1], length(array[1]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 492: | Line 492: | ||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
BaCon knows three types of arrays, the UBOUND function can query all. |
BaCon knows three types of arrays, the UBOUND function can query all. |
||
< |
<syntaxhighlight lang=bacon>' Static arrays |
||
DECLARE fruit$[] = { "apple", "orange" } |
DECLARE fruit$[] = { "apple", "orange" } |
||
Line 509: | Line 509: | ||
meat$("first") = "chicken" |
meat$("first") = "chicken" |
||
meat$("second") = "pork" |
meat$("second") = "pork" |
||
PRINT UBOUND(meat$)</ |
PRINT UBOUND(meat$)</syntaxhighlight> |
||
=={{header|Bash}}== |
=={{header|Bash}}== |
||
< |
<syntaxhighlight lang=bash> |
||
fruit=("apple" "orange" "lemon") |
fruit=("apple" "orange" "lemon") |
||
echo "${#fruit[@]}"</ |
echo "${#fruit[@]}"</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang=basic>DIM X$(1 TO 2) |
||
X$(1) = "apple" |
X$(1) = "apple" |
||
X$(2) = "orange" |
X$(2) = "orange" |
||
PRINT UBOUND(X$) - LBOUND(X$) + 1</ |
PRINT UBOUND(X$) - LBOUND(X$) + 1</syntaxhighlight> |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang=basic>10 DIM A$(2) |
||
20 A$(1) = "ORANGE" |
20 A$(1) = "ORANGE" |
||
30 A$(2) = "APPLE" |
30 A$(2) = "APPLE" |
||
Line 557: | Line 557: | ||
170 L$ = L$ + STR$ ( FN P(I)) |
170 L$ = L$ + STR$ ( FN P(I)) |
||
180 L$ = L$ + " ": NEXT I |
180 L$ = L$ + " ": NEXT I |
||
190 RETURN</ |
190 RETURN</syntaxhighlight> |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang=BASIC256> |
||
fruta$ = {"apple", "orange", "pear"} |
fruta$ = {"apple", "orange", "pear"} |
||
Line 567: | Line 567: | ||
print fruta$[1] |
print fruta$[1] |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
3 |
3 |
||
Line 577: | Line 577: | ||
Commodore BASIC has no way within the language to query an array for its length, but you can dive into the implementation to get that information. On a C-64 in particular, this works: |
Commodore BASIC has no way within the language to query an array for its length, but you can dive into the implementation to get that information. On a C-64 in particular, this works: |
||
{{works with|Commodore BASIC|2.0 on C-64}} |
{{works with|Commodore BASIC|2.0 on C-64}} |
||
< |
<syntaxhighlight lang=basic>10 DIM A$(1):REM 1=LAST -> ROOM FOR 2 |
||
20 A$(0) = "ORANGE" |
20 A$(0) = "ORANGE" |
||
30 A$(1) = "APPLE" |
30 A$(1) = "APPLE" |
||
Line 587: | Line 587: | ||
90 IF T=128 THEN N$=N$+"$": REM STRING |
90 IF T=128 THEN N$=N$+"$": REM STRING |
||
100 L=PEEK(AT+6): REM FIRST INDEX SIZE |
100 L=PEEK(AT+6): REM FIRST INDEX SIZE |
||
110 PRINT N$" HAS"L"ELEMENTS."</ |
110 PRINT N$" HAS"L"ELEMENTS."</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 593: | Line 593: | ||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
< |
<syntaxhighlight lang=qbasic> |
||
DIM fruta$(2) |
DIM fruta$(2) |
||
READ fruta$(1), fruta$(2) |
READ fruta$(1), fruta$(2) |
||
Line 602: | Line 602: | ||
PRINT "La longitud del array fruta$ es" ; tamano |
PRINT "La longitud del array fruta$ es" ; tamano |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 610: | Line 610: | ||
True BASIC's arrays are not fixed in length and, although True BASIC is a compiled-language, the number of elements can be changed during runtime using such functions as the MAT REDIM (matrix re-dimension) function. Although the starting index of 1 is in implicit, it can be changed by setting the lower and upper bounds (eg. fruit(0 to 3)) when declaring the array. Also, the example below uses the MAT READ function to read in the data elements into the array without having to explicitly list each variable-array index. The example also uses the SIZE function vs the bounds method to determine the length of the array. Finally, in this example the SIZE function was not assigned to a separate variable and instead is used within the PRINT function itself. |
True BASIC's arrays are not fixed in length and, although True BASIC is a compiled-language, the number of elements can be changed during runtime using such functions as the MAT REDIM (matrix re-dimension) function. Although the starting index of 1 is in implicit, it can be changed by setting the lower and upper bounds (eg. fruit(0 to 3)) when declaring the array. Also, the example below uses the MAT READ function to read in the data elements into the array without having to explicitly list each variable-array index. The example also uses the SIZE function vs the bounds method to determine the length of the array. Finally, in this example the SIZE function was not assigned to a separate variable and instead is used within the PRINT function itself. |
||
< |
<syntaxhighlight lang=qbasic> |
||
DIM fruit$(2) |
DIM fruit$(2) |
||
MAT READ fruit$ |
MAT READ fruit$ |
||
Line 617: | Line 617: | ||
PRINT "The length of the array 'fruit$' is "; SIZE(fruit$) |
PRINT "The length of the array 'fruit$' is "; SIZE(fruit$) |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 626: | Line 626: | ||
While batch files don't support arrays in the traditional sense, sets of variables forming somewhat of a pseudo-array are extremely useful. They are usually in the form of <code>%name{number}%</code>. The below code gives an example of how to create an array from a list stored in a variable, and how to acquire the amount of entries in the array. |
While batch files don't support arrays in the traditional sense, sets of variables forming somewhat of a pseudo-array are extremely useful. They are usually in the form of <code>%name{number}%</code>. The below code gives an example of how to create an array from a list stored in a variable, and how to acquire the amount of entries in the array. |
||
< |
<syntaxhighlight lang=dos> |
||
@echo off |
@echo off |
||
Line 655: | Line 655: | ||
set /a arraylength+=1 |
set /a arraylength+=1 |
||
goto loop |
goto loop |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{in}} |
{{in}} |
||
<pre> |
<pre> |
||
Line 667: | Line 667: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang=bbcbasic> DIM array$(1) |
||
array$() = "apple", "orange" |
array$() = "apple", "orange" |
||
PRINT "Number of elements in array = "; DIM(array$(), 1) + 1 |
PRINT "Number of elements in array = "; DIM(array$(), 1) + 1 |
||
PRINT "Number of bytes in all elements combined = "; SUMLEN(array$()) |
PRINT "Number of bytes in all elements combined = "; SUMLEN(array$()) |
||
END</ |
END</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 678: | Line 678: | ||
=={{header|Brat}}== |
=={{header|Brat}}== |
||
< |
<syntaxhighlight lang=brat> |
||
p ["apple", "orange"].length |
p ["apple", "orange"].length |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
<code>≠</code> gives the length of an array in BQN. |
<code>≠</code> gives the length of an array in BQN. |
||
<lang |
<syntaxhighlight lang=bqn>≠ 1‿"a"‿+</syntaxhighlight> |
||
<lang |
<syntaxhighlight lang=bqn>3</syntaxhighlight> |
||
[https://mlochbaum.github.io/BQN/try.html#code=4omgIDHigL8iYSLigL8r Try It!] |
[https://mlochbaum.github.io/BQN/try.html#code=4omgIDHigL8iYSLigL8r Try It!] |
||
Line 697: | Line 697: | ||
For static arrays: |
For static arrays: |
||
<syntaxhighlight lang=c> |
|||
<lang c> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
Line 719: | Line 719: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
A C pre-processor macro may be created for ease of use: |
A C pre-processor macro may be created for ease of use: |
||
<syntaxhighlight lang=c> |
|||
<lang c> |
|||
#define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0])) |
#define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0])) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Note that these arrays become pointers when passed as a parameter to a function. |
Note that these arrays become pointers when passed as a parameter to a function. |
||
Line 744: | Line 744: | ||
</p> |
</p> |
||
====Solution==== |
====Solution==== |
||
< |
<syntaxhighlight lang=C>#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings |
||
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS |
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS |
||
Line 819: | Line 819: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre>There are 2 elements in an array with a capacity of 10 elements: |
<pre>There are 2 elements in an array with a capacity of 10 elements: |
||
Line 827: | Line 827: | ||
====An example why sizeof(A)/sizeof(E) may be a bad idea in C==== |
====An example why sizeof(A)/sizeof(E) may be a bad idea in C==== |
||
< |
<syntaxhighlight lang=C>#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings |
||
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS |
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS |
||
Line 938: | Line 938: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<p> |
<p> |
||
As we can see the ''sizeof(ArrayType)/sizeof(ElementType)'' approach mostly fail. |
As we can see the ''sizeof(ArrayType)/sizeof(ElementType)'' approach mostly fail. |
||
Line 1,005: | Line 1,005: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang=csharp> |
||
using System; |
using System; |
||
Line 1,016: | Line 1,016: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Note that any of the following array declarations could be used: |
Note that any of the following array declarations could be used: |
||
< |
<syntaxhighlight lang=csharp> |
||
var fruit = new[] { "apple", "orange" }; |
var fruit = new[] { "apple", "orange" }; |
||
var fruit = new string[] { "apple", "orange" }; |
var fruit = new string[] { "apple", "orange" }; |
||
Line 1,026: | Line 1,026: | ||
string[] fruit = new string[] { "apple", "orange" }; |
string[] fruit = new string[] { "apple", "orange" }; |
||
string[] fruit = { "apple", "orange" }; |
string[] fruit = { "apple", "orange" }; |
||
</syntaxhighlight> |
|||
</lang> |
|||
A shorter variant could also have been used: |
A shorter variant could also have been used: |
||
< |
<syntaxhighlight lang=csharp> |
||
using static System.Console; |
using static System.Console; |
||
Line 1,040: | Line 1,040: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 1,048: | Line 1,048: | ||
However, C++ has an additional <code>std::array</code> type (amongst other collections) in its standard library: |
However, C++ has an additional <code>std::array</code> type (amongst other collections) in its standard library: |
||
< |
<syntaxhighlight lang=cpp>#include <array> |
||
#include <iostream> |
#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 1,057: | Line 1,057: | ||
std::cout << fruit.size(); |
std::cout << fruit.size(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Note that <code>char*</code> or <code>const char*</code> could have been used instead of <code>std::string</code>. |
Note that <code>char*</code> or <code>const char*</code> could have been used instead of <code>std::string</code>. |
||
Line 1,063: | Line 1,063: | ||
In addition to the <code>std::array</code> type, the C++ standard library also provides dynamically-sized containers to hold arbitrary objects. |
In addition to the <code>std::array</code> type, the C++ standard library also provides dynamically-sized containers to hold arbitrary objects. |
||
These all support similar interfaces, though their implementations have different performance characteristics. |
These all support similar interfaces, though their implementations have different performance characteristics. |
||
< |
<syntaxhighlight lang=cpp> std::vector<std::string> fruitV({ "apples", "oranges" }); |
||
std::list<std::string> fruitL({ "apples", "oranges" }); |
std::list<std::string> fruitL({ "apples", "oranges" }); |
||
std::deque<std::string> fruitD({ "apples", "oranges" }); |
std::deque<std::string> fruitD({ "apples", "oranges" }); |
||
std::cout << fruitV.size() << fruitL.size() << fruitD.size() << std::endl;</ |
std::cout << fruitV.size() << fruitL.size() << fruitD.size() << std::endl;</syntaxhighlight> |
||
Of these, vector is probably the most widely used. |
Of these, vector is probably the most widely used. |
||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang=ceylon>shared void run() { |
||
value array = ["apple", "orange"]; |
value array = ["apple", "orange"]; |
||
print(array.size); |
print(array.size); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clipper/XBase++}}== |
=={{header|Clipper/XBase++}}== |
||
< |
<syntaxhighlight lang=Clipper/XBase++>/* |
||
* nizchka: March - 2016 |
* nizchka: March - 2016 |
||
* This is a Clipper/XBase++ of RosettaCode Array_Length |
* This is a Clipper/XBase++ of RosettaCode Array_Length |
||
Line 1,088: | Line 1,088: | ||
? LEN(FRUIT) |
? LEN(FRUIT) |
||
RETURN |
RETURN |
||
</syntaxhighlight> |
|||
</lang> |
|||
Outputs:<pre>2</pre> |
Outputs:<pre>2</pre> |
||
[[User:nizchka|nizchka]] 23:27, 16 March 2016 (UTC) |
[[User:nizchka|nizchka]] 23:27, 16 March 2016 (UTC) |
||
Line 1,094: | Line 1,094: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang=clojure>; using count: |
||
(count ["apple" "orange"]) |
(count ["apple" "orange"]) |
||
; OR alength if using Java arrays: |
; OR alength if using Java arrays: |
||
(alength (into-array ["apple" "orange"]))</ |
(alength (into-array ["apple" "orange"]))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Arrays in COBOL are usually referred to as tables. Tables can have fixed or variable (with known maximum) allocations, using a syntax of OCCURS DEPENDING ON. The value of the ODO identifier is the number of active elements in the table. |
Arrays in COBOL are usually referred to as tables. Tables can have fixed or variable (with known maximum) allocations, using a syntax of OCCURS DEPENDING ON. The value of the ODO identifier is the number of active elements in the table. |
||
< |
<syntaxhighlight lang=COBOL> identification division. |
||
program-id. array-length. |
program-id. array-length. |
||
Line 1,142: | Line 1,142: | ||
. |
. |
||
end program array-length.</ |
end program array-length.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ cobc -xjd array-length.cob |
<pre>$ cobc -xjd array-length.cob |
||
Line 1,151: | Line 1,151: | ||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
< |
<syntaxhighlight lang=coldfusion> |
||
<cfset testArray = ["apple","orange"]> |
<cfset testArray = ["apple","orange"]> |
||
<cfoutput>Array Length = #ArrayLen(testArray)#</cfoutput> |
<cfoutput>Array Length = #ArrayLen(testArray)#</cfoutput> |
||
</syntaxhighlight> |
|||
</lang> |
|||
Outputs:<pre>Array Length = 2</pre> |
Outputs:<pre>Array Length = 2</pre> |
||
[[User:grandnegus|Mike Knapp]] 15:57, 26 May 2016 (UTC) |
[[User:grandnegus|Mike Knapp]] 15:57, 26 May 2016 (UTC) |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang=Lisp> |
||
(print (length #("apple" "orange"))) |
(print (length #("apple" "orange"))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Alternate solution=== |
===Alternate solution=== |
||
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1] |
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1] |
||
< |
<syntaxhighlight lang=lisp> |
||
;; Project : Array length |
;; Project : Array length |
||
Line 1,174: | Line 1,174: | ||
(length my-array) |
(length my-array) |
||
(terpri) |
(terpri) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,182: | Line 1,182: | ||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
{{works with|BlackBox Component Builder}} |
{{works with|BlackBox Component Builder}} |
||
< |
<syntaxhighlight lang=oberon2> |
||
MODULE AryLen; |
MODULE AryLen; |
||
IMPORT StdLog; |
IMPORT StdLog; |
||
Line 1,215: | Line 1,215: | ||
END AryLen. |
END AryLen. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Execute: ^Q AryLen.Do |
Execute: ^Q AryLen.Do |
||
{{out}} |
{{out}} |
||
Line 1,224: | Line 1,224: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<syntaxhighlight lang=ruby> |
||
puts ["apple", "orange"].size |
puts ["apple", "orange"].size |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,234: | Line 1,234: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang=d> |
|||
<lang d> |
|||
import std.stdio; |
import std.stdio; |
||
Line 1,243: | Line 1,243: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Or a somewhat shorter... |
Or a somewhat shorter... |
||
<syntaxhighlight lang=d> |
|||
<lang d> |
|||
import std.stdio; |
import std.stdio; |
||
Line 1,253: | Line 1,253: | ||
["apple", "orange"].length.writeln; |
["apple", "orange"].length.writeln; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang=dart> |
||
arrLength(arr) { |
arrLength(arr) { |
||
return arr.length; |
return arr.length; |
||
Line 1,266: | Line 1,266: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|DataWeave}}== |
=={{header|DataWeave}}== |
||
< |
<syntaxhighlight lang=DataWeave>var arr = ["apple", "orange"] |
||
sizeOf(arr) |
sizeOf(arr) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang=delphi> |
||
showmessage( length(['a','b','c']).ToString ); |
showmessage( length(['a','b','c']).ToString ); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Diego}}== |
=={{header|Diego}}== |
||
< |
<syntaxhighlight lang=diego>set_namespace(rosettacode)_me(); |
||
me_msg()_array()_values(apple,orange)_length(); |
me_msg()_array()_values(apple,orange)_length(); |
||
reset_namespace[];</ |
reset_namespace[];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,290: | Line 1,290: | ||
=={{header|Dragon}}== |
=={{header|Dragon}}== |
||
< |
<syntaxhighlight lang=dragon>select "std" |
||
a = ["apple","orange"] |
a = ["apple","orange"] |
||
Line 1,296: | Line 1,296: | ||
show b |
show b |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang=dyalect>var xs = ["apple", "orange"] |
||
print(xs.Length())</ |
print(xs.Length())</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>fruit$[] = [ "apples" "oranges" ] |
<lang>fruit$[] = [ "apples" "oranges" ] |
||
print len fruit$[]</ |
print len fruit$[]</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang=scheme> |
||
(length '("apple" "orange")) ;; list |
(length '("apple" "orange")) ;; list |
||
→ 2 |
→ 2 |
||
(vector-length #("apple" "orange")) ;; vector |
(vector-length #("apple" "orange")) ;; vector |
||
→ 2 |
→ 2 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
<lang |
<syntaxhighlight lang=ela>length [1..10]</syntaxhighlight> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
< |
<syntaxhighlight lang=elena> |
||
var array := new string[]{"apple", "orange"}; |
var array := new string[]{"apple", "orange"}; |
||
var length := array.Length; |
var length := array.Length; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang=elixir>iex(1)> length( ["apple", "orange"] ) # List |
||
2 |
2 |
||
iex(2)> tuple_size( {"apple", "orange"} ) # Tuple |
iex(2)> tuple_size( {"apple", "orange"} ) # Tuple |
||
2</ |
2</syntaxhighlight> |
||
=={{header|Elm}}== |
=={{header|Elm}}== |
||
< |
<syntaxhighlight lang=elm> |
||
import Array |
import Array |
||
import Html |
import Html |
||
Line 1,343: | Line 1,343: | ||
|> Basics.toString |
|> Basics.toString |
||
|> Html.text |
|> Html.text |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang=Lisp>(length ["apple" "orange"]) |
||
=> 2</ |
=> 2</syntaxhighlight> |
||
<code>length</code> also accepts a list or a string. |
<code>length</code> also accepts a list or a string. |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang=Erlang> |
||
1> length(["apple", "orange"]). %using a list |
1> length(["apple", "orange"]). %using a list |
||
2 |
2 |
||
1> tuple_size({"apple", "orange"}). %using a tuple |
1> tuple_size({"apple", "orange"}). %using a tuple |
||
2 |
2 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang=Euphoria> |
||
sequence s = {"apple","orange",2.95} -- Euphoria doesn't care what you put in a sequence |
sequence s = {"apple","orange",2.95} -- Euphoria doesn't care what you put in a sequence |
||
Line 1,381: | Line 1,381: | ||
1 -- 2.95 is an atomic value |
1 -- 2.95 is an atomic value |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang=fsharp>[|1;2;3|].Length |> printfn "%i"</syntaxhighlight> |
||
Or: |
Or: |
||
< |
<syntaxhighlight lang=fsharp>[|1;2;3|] |> Array.length |> printfn "%i"</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang=factor> |
||
{ "apple" "orange" } length |
{ "apple" "orange" } length |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 1,425: | Line 1,425: | ||
REPEAT |
REPEAT |
||
DROP |
DROP |
||
R> ; \ return counter to data stack </ |
R> ; \ return counter to data stack </syntaxhighlight> |
||
Test code at Forth console |
Test code at Forth console |
||
< |
<syntaxhighlight lang=forth>CREATE Q { " Apples" " Oranges" } q {len} . 2 ok</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 1,440: | Line 1,440: | ||
For a simple example, the WRITE(6,*) suffices: write to standard output (the 6), in free-format (the *). |
For a simple example, the WRITE(6,*) suffices: write to standard output (the 6), in free-format (the *). |
||
< |
<syntaxhighlight lang=Fortran> |
||
MODULE EXAMPLE |
MODULE EXAMPLE |
||
CONTAINS |
CONTAINS |
||
Line 1,461: | Line 1,461: | ||
WRITE (6,*) "L. bound",LBOUND(ARRAY),", U. bound",UBOUND(ARRAY) |
WRITE (6,*) "L. bound",LBOUND(ARRAY),", U. bound",UBOUND(ARRAY) |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 1,475: | Line 1,475: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang=freebasic>' FB 1.05.0 Win64 |
||
Dim fruit(1) As String = {"apple", "orange"} |
Dim fruit(1) As String = {"apple", "orange"} |
||
Line 1,482: | Line 1,482: | ||
Print |
Print |
||
Print "Press any key to quit the program" |
Print "Press any key to quit the program" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,490: | Line 1,490: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang=frink> |
||
a = ["apple", "orange"] |
a = ["apple", "orange"] |
||
println[length[a]] |
println[length[a]] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FurryScript}}== |
=={{header|FurryScript}}== |
||
< |
<syntaxhighlight lang=furryscript>THE_LIST( <apple> <orange> ) |
||
COUNT[ 0 SW ~| COUNT_STEP# 0 SW SU ] |
COUNT[ 0 SW ~| COUNT_STEP# 0 SW SU ] |
||
COUNT_STEP[ DR 1 SU ] |
COUNT_STEP[ DR 1 SU ] |
||
`THE_LIST COUNT# +<></ |
`THE_LIST COUNT# +<></syntaxhighlight> |
||
=={{header|Futhark}}== |
=={{header|Futhark}}== |
||
Line 1,506: | Line 1,506: | ||
The <code>shape</code> builtin returns the shape of an array as an array of integers. The length is element 0 of the shape: |
The <code>shape</code> builtin returns the shape of an array as an array of integers. The length is element 0 of the shape: |
||
< |
<syntaxhighlight lang=Futhark> |
||
fun length(as: []int): int = (shape as)[0] |
fun length(as: []int): int = (shape as)[0] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
NSUInteger count = fn ArrayCount( ''array'' ). Example: |
NSUInteger count = fn ArrayCount( ''array'' ). Example: |
||
< |
<syntaxhighlight lang=futurebasic> |
||
window 1 |
window 1 |
||
print fn ArrayCount( @[@"apple",@"orange",@"cherry",@"grape",@"lemon"] ) |
print fn ArrayCount( @[@"apple",@"orange",@"cherry",@"grape",@"lemon"] ) |
||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,532: | Line 1,532: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=2a452c807c7030eb64a2f1d60d31a830 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=2a452c807c7030eb64a2f1d60d31a830 Click this link to run this code]''' |
||
< |
<syntaxhighlight lang=gambas>Public Sub Main() |
||
Dim siList As Short[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] |
Dim siList As Short[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] |
||
Print siList.Count |
Print siList.Count |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,544: | Line 1,544: | ||
=={{header|Genie}}== |
=={{header|Genie}}== |
||
< |
<syntaxhighlight lang=genie>[indent=4] |
||
/* Array length, in Genie */ |
/* Array length, in Genie */ |
||
init |
init |
||
arr:array of string = {"apple", "orange"} |
arr:array of string = {"apple", "orange"} |
||
stdout.printf("%d ", arr.length) |
stdout.printf("%d ", arr.length) |
||
print arr[1]</ |
print arr[1]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,558: | Line 1,558: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang=Go>package main |
||
import "fmt" |
import "fmt" |
||
Line 1,566: | Line 1,566: | ||
fmt.Printf("Length of %v is %v.\n", arr, len(arr)) |
fmt.Printf("Length of %v is %v.\n", arr, len(arr)) |
||
}</ |
}</syntaxhighlight> |
||
Line 1,576: | Line 1,576: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang=Groovy> |
||
def fruits = ['apple','orange'] |
def fruits = ['apple','orange'] |
||
println fruits.size() |
println fruits.size() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Harbour}}== |
=={{header|Harbour}}== |
||
< |
<syntaxhighlight lang=visualfoxpro> |
||
LOCAL aFruits := {"apple", "orange"} |
LOCAL aFruits := {"apple", "orange"} |
||
Qout( Len( aFruits ) ) // --> 2 |
Qout( Len( aFruits ) ) // --> 2 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=Haskell>-- [[Char]] -> Int |
||
length ["apple", "orange"]</ |
length ["apple", "orange"]</syntaxhighlight> |
||
=={{header|hexiscript}}== |
=={{header|hexiscript}}== |
||
< |
<syntaxhighlight lang=hexiscript>let a arr 2 |
||
let a[0] "apple" |
let a[0] "apple" |
||
let a[1] "orange" |
let a[1] "orange" |
||
println len a</ |
println len a</syntaxhighlight> |
||
=={{header|Hoon}}== |
=={{header|Hoon}}== |
||
< |
<syntaxhighlight lang=Hoon>|= arr=(list *) (lent arr)</syntaxhighlight> |
||
=={{header|i}}== |
=={{header|i}}== |
||
< |
<syntaxhighlight lang=i>main: print(#["apple", "orange"])</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Icon, and therefore Unicon, includes a prefix size operator, star <code>*</code>. This operator can be applied to just about any data type or data structure. |
Icon, and therefore Unicon, includes a prefix size operator, star <code>*</code>. This operator can be applied to just about any data type or data structure. |
||
< |
<syntaxhighlight lang=unicon>write(*["apple", "orange"])</syntaxhighlight> |
||
=={{header|Idris}}== |
=={{header|Idris}}== |
||
< |
<syntaxhighlight lang=Idris>length ["apple", "orange"]</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Tally (<code>#</code>) returns the length of the leading dimension of an array (or 1 if the array has no dimensions). Shape Of (<code>$</code>) returns the length of each dimension of an array. |
Tally (<code>#</code>) returns the length of the leading dimension of an array (or 1 if the array has no dimensions). Shape Of (<code>$</code>) returns the length of each dimension of an array. |
||
< |
<syntaxhighlight lang=j> # 'apple';'orange' |
||
2 |
2 |
||
$ 'apple';'orange' |
$ 'apple';'orange' |
||
2</ |
2</syntaxhighlight> |
||
For the list array example given, the result appears to be the same. The difference is that the result of Tally is a scalar (array of 0 dimensions) whereas the result of Shape Of is a list (1 dimensional array), of length 1 in this case. |
For the list array example given, the result appears to be the same. The difference is that the result of Tally is a scalar (array of 0 dimensions) whereas the result of Shape Of is a list (1 dimensional array), of length 1 in this case. |
||
< |
<syntaxhighlight lang=j> $#'apple';'orange' |
||
$$'apple';'orange' |
$$'apple';'orange' |
||
1</ |
1</syntaxhighlight> |
||
This might be a clearer concept with a few more examples. Here's an array with two dimensions: |
This might be a clearer concept with a few more examples. Here's an array with two dimensions: |
||
< |
<syntaxhighlight lang=j> >'apple';'orange' |
||
apple |
apple |
||
orange |
orange |
||
Line 1,631: | Line 1,631: | ||
2 6 |
2 6 |
||
#>'apple';'orange' |
#>'apple';'orange' |
||
2</ |
2</syntaxhighlight> |
||
And, here's an array with no dimensions: |
And, here's an array with no dimensions: |
||
<lang> 9001 |
<lang> 9001 |
||
Line 1,638: | Line 1,638: | ||
1 |
1 |
||
$9001 |
$9001 |
||
</syntaxhighlight> |
|||
</lang> |
|||
You can count the number of dimensions of an array (the length of the list of lengths) using <code>#$array</code>: |
You can count the number of dimensions of an array (the length of the list of lengths) using <code>#$array</code>: |
||
<syntaxhighlight lang=j> |
|||
<lang j> |
|||
#$9001 |
#$9001 |
||
0 |
0 |
||
Line 1,646: | Line 1,646: | ||
1 |
1 |
||
#$>'apple';'orange' |
#$>'apple';'orange' |
||
2</ |
2</syntaxhighlight> |
||
=={{header|Janet}}== |
=={{header|Janet}}== |
||
< |
<syntaxhighlight lang=janet> |
||
(def our-array @["apple" "orange"]) |
(def our-array @["apple" "orange"]) |
||
(length our-array) |
(length our-array) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,661: | Line 1,661: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang=java>public class ArrayLength { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
System.out.println(new String[]{"apple", "orange"}.length); |
System.out.println(new String[]{"apple", "orange"}.length); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang=javascript>console.log(['apple', 'orange'].length);</syntaxhighlight> |
||
However, determining the length of a list, array, or collection may simply be the wrong thing to do. |
However, determining the length of a list, array, or collection may simply be the wrong thing to do. |
||
Line 1,675: | Line 1,675: | ||
If, for example, the actual task (undefined here, unfortunately) requires retrieving the final item, while it is perfectly possible to write '''last''' in terms of '''length''' |
If, for example, the actual task (undefined here, unfortunately) requires retrieving the final item, while it is perfectly possible to write '''last''' in terms of '''length''' |
||
< |
<syntaxhighlight lang=JavaScript>function last(lst) { |
||
return lst[lst.length - 1]; |
return lst[lst.length - 1]; |
||
}</ |
}</syntaxhighlight> |
||
using length has the disadvantage that it leaves ''last'' simply undefined for an empty list. |
using length has the disadvantage that it leaves ''last'' simply undefined for an empty list. |
||
Line 1,683: | Line 1,683: | ||
We might do better to drop the narrow focus on length, and instead use a fold (''reduce'', in JS terms) which can return a default value of some kind. |
We might do better to drop the narrow focus on length, and instead use a fold (''reduce'', in JS terms) which can return a default value of some kind. |
||
< |
<syntaxhighlight lang=JavaScript>function last(lst) { |
||
return lst.reduce(function (a, x) { |
return lst.reduce(function (a, x) { |
||
return x; |
return x; |
||
}, null); |
}, null); |
||
}</ |
}</syntaxhighlight> |
||
Alternatively, rather than scanning the entire list to simply get the final value, it might sometimes be better to test the length: |
Alternatively, rather than scanning the entire list to simply get the final value, it might sometimes be better to test the length: |
||
< |
<syntaxhighlight lang=JavaScript>function last(list, defaultValue) { |
||
return list.length ?list[list.length-1] :defaultValue; |
return list.length ?list[list.length-1] :defaultValue; |
||
}</ |
}</syntaxhighlight> |
||
Or use other built-in functions – this, for example, seems fairly clear, and is already 100+ times faster than unoptimised tail recursion in ES5 (testing with a list of 1000 elements): |
Or use other built-in functions – this, for example, seems fairly clear, and is already 100+ times faster than unoptimised tail recursion in ES5 (testing with a list of 1000 elements): |
||
< |
<syntaxhighlight lang=JavaScript>function last(list, defaultValue) { |
||
return list.slice(-1)[0] || defaultValue; |
return list.slice(-1)[0] || defaultValue; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
< |
<syntaxhighlight lang=jq>["apple","orange"] | length</syntaxhighlight> |
||
Output: |
Output: |
||
<lang |
<syntaxhighlight lang=sh>2</syntaxhighlight> |
||
Note that the ''length'' filter is polymorphic, so for example the empty string (""), the empty list ([]), and ''null'' all have ''length'' 0. |
Note that the ''length'' filter is polymorphic, so for example the empty string (""), the empty list ([]), and ''null'' all have ''length'' 0. |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
< |
<syntaxhighlight lang=javascript>/* Array length, in jsish */ |
||
var arr = new Array('apple', 'orange'); |
var arr = new Array('apple', 'orange'); |
||
puts(arr.length); |
puts(arr.length); |
||
puts(arr[1]);</ |
puts(arr[1]);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,722: | Line 1,722: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang=Julia> |
||
a = ["apple","orange"] |
a = ["apple","orange"] |
||
length(a) |
length(a) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Klingphix}}== |
=={{header|Klingphix}}== |
||
< |
<syntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy |
||
( "apple" "orange" ) len print |
( "apple" "orange" ) len print |
||
" " input</ |
" " input</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2</pre> |
<pre>2</pre> |
||
Line 1,738: | Line 1,738: | ||
=={{header|Klong}}== |
=={{header|Klong}}== |
||
<syntaxhighlight lang=K> |
|||
<lang K> |
|||
#["apple" "orange"] |
#["apple" "orange"] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang=scala>fun main(args: Array<String>) { |
||
println(arrayOf("apple", "orange").size) |
println(arrayOf("apple", "orange").size) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang=scheme> |
||
{A.length {A.new 1 2 3}} |
{A.length {A.new 1 2 3}} |
||
-> 3 |
-> 3 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Latitude}}== |
=={{header|Latitude}}== |
||
Line 1,758: | Line 1,758: | ||
In Latitude, <code>length</code> and <code>size</code> are synonymous and will both retrieve the size of a collection. |
In Latitude, <code>length</code> and <code>size</code> are synonymous and will both retrieve the size of a collection. |
||
< |
<syntaxhighlight lang=latitude>println: ["apple", "orange"] length.</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Line 1,770: | Line 1,770: | ||
NOTE -- This program runs only under LB Booster version 3.05 or higher because of arrays with more than two dimensions, passed array names to functions and subroutines as a parameter, and structured error trapping syntax. Get the LBB compiler here: http://lbbooster.com/ |
NOTE -- This program runs only under LB Booster version 3.05 or higher because of arrays with more than two dimensions, passed array names to functions and subroutines as a parameter, and structured error trapping syntax. Get the LBB compiler here: http://lbbooster.com/ |
||
{{works with|LB Booster}} |
{{works with|LB Booster}} |
||
<syntaxhighlight lang=lb> |
|||
<lang lb> |
|||
FruitList$(0)="apple" 'assign 2 cells of a list array |
FruitList$(0)="apple" 'assign 2 cells of a list array |
||
FruitList$(1)="orange" |
FruitList$(1)="orange" |
||
Line 1,882: | Line 1,882: | ||
print ")" |
print ")" |
||
end sub |
end sub |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,893: | Line 1,893: | ||
LIL does not use arrays, but indexed lists. The builtin '''count''' command returns the item count in a list. The '''length''' command returns the length of the list after string conversion. |
LIL does not use arrays, but indexed lists. The builtin '''count''' command returns the item count in a list. The '''length''' command returns the length of the list after string conversion. |
||
< |
<syntaxhighlight lang=tcl># Array length, in LIL |
||
set a [list "apple"] |
set a [list "apple"] |
||
append a "orange" |
append a "orange" |
||
print [count $a] |
print [count $a] |
||
print [index $a 1]</ |
print [index $a 1]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,905: | Line 1,905: | ||
=={{header|Limbo}}== |
=={{header|Limbo}}== |
||
< |
<syntaxhighlight lang=Limbo>implement Command; |
||
include "sys.m"; |
include "sys.m"; |
||
Line 1,920: | Line 1,920: | ||
a := array[] of {"apple", "orange"}; |
a := array[] of {"apple", "orange"}; |
||
sys->print("length of a: %d\n", len a); |
sys->print("length of a: %d\n", len a); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang=lingo>fruits = ["apple", "orange"] |
||
put fruits.count |
put fruits.count |
||
-- 2</ |
-- 2</syntaxhighlight> |
||
=={{header|Little}}== |
=={{header|Little}}== |
||
< |
<syntaxhighlight lang=C>string fruit[] = {"apples", "oranges"}; |
||
puts(length(fruit));</ |
puts(length(fruit));</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang=LiveCode>put "apple","orange" into fruit |
||
split fruit using comma |
split fruit using comma |
||
answer the number of elements of fruit</ |
answer the number of elements of fruit</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang=Lua>-- For tables as simple arrays, use the # operator: |
||
fruits = {"apple", "orange"} |
fruits = {"apple", "orange"} |
||
print(#fruits) |
print(#fruits) |
||
Line 1,955: | Line 1,955: | ||
end |
end |
||
print(size(fruits))</ |
print(size(fruits))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,964: | Line 1,964: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
< |
<syntaxhighlight lang=M2000 Interpreter> |
||
\\ A is a pointer to array |
\\ A is a pointer to array |
||
A=("Apple", "Orange") |
A=("Apple", "Orange") |
||
Line 2,003: | Line 2,003: | ||
Dim K(1,1,1,1,1,1) ' Maximum 10 dimensions |
Dim K(1,1,1,1,1,1) ' Maximum 10 dimensions |
||
Print Len(K()=1 ' True |
Print Len(K()=1 ' True |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang=maple>a := Array(["apple", "orange"]); |
||
numelems(a);</ |
numelems(a);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,015: | Line 2,015: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>Length[{"apple", "orange"}]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang=Matlab>length({'apple', 'orange'})</syntaxhighlight> |
||
For arrays with more than one dimension, length reports the length of the larges dimension. The number of elements in a multi-dimensional array can be obtained with numel. |
For arrays with more than one dimension, length reports the length of the larges dimension. The number of elements in a multi-dimensional array can be obtained with numel. |
||
< |
<syntaxhighlight lang=Matlab>numel({'apple', 'orange'; 'pear', 'banana'})</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang=modula3>MODULE ArrayLength EXPORTS Main; |
||
IMPORT IO; |
IMPORT IO; |
||
Line 2,032: | Line 2,032: | ||
BEGIN |
BEGIN |
||
IO.PutInt(NUMBER(Arr)); |
IO.PutInt(NUMBER(Arr)); |
||
END ArrayLength.</ |
END ArrayLength.</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
< |
<syntaxhighlight lang=Mercury>:- module array_length. |
||
:- interface. |
:- interface. |
||
Line 2,047: | Line 2,047: | ||
main(!IO) :- |
main(!IO) :- |
||
Array = array(["apples", "oranges"]), |
Array = array(["apples", "oranges"]), |
||
io.write_int(size(Array), !IO).</ |
io.write_int(size(Array), !IO).</syntaxhighlight> |
||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.3}} |
{{works with|min|0.19.3}} |
||
< |
<syntaxhighlight lang=min>("apple" "orange") size print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,058: | Line 2,058: | ||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
< |
<syntaxhighlight lang=MiniScript> |
||
fruits = ["apple", "orange"] |
fruits = ["apple", "orange"] |
||
print fruits.len |
print fruits.len |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MiniZinc}}== |
=={{header|MiniZinc}}== |
||
< |
<syntaxhighlight lang=MiniZinc> |
||
array[int] of int: arr = [1,2,3]; |
array[int] of int: arr = [1,2,3]; |
||
var int: size = length(arr); |
var int: size = length(arr); |
||
Line 2,070: | Line 2,070: | ||
output [show(size),"\n"]; |
output [show(size),"\n"]; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang=nanoquery>fruit = array(2) |
||
fruit[0] = "apple" |
fruit[0] = "apple" |
||
fruit[1] = "orange" |
fruit[1] = "orange" |
||
println len(fruit) |
println len(fruit) |
||
// outputs 2</ |
// outputs 2</syntaxhighlight> |
||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
< |
<syntaxhighlight lang=neko>var fruit = $array("apple", "orange"); |
||
$print($asize(fruit));</ |
$print($asize(fruit));</syntaxhighlight> |
||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang=newlisp>(println (length '("apple" "orange"))) |
||
; Nehal-Singhal 2018-05-25 |
; Nehal-Singhal 2018-05-25 |
||
(length '(apple orange))</ |
(length '(apple orange))</syntaxhighlight> |
||
=={{header|NGS}}== |
=={{header|NGS}}== |
||
< |
<syntaxhighlight lang=NGS>echo(len(['apple', 'orange'])) |
||
# same |
# same |
||
echo(['apple', 'orange'].len())</ |
echo(['apple', 'orange'].len())</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=nim>let fruit = ["apple", "orange"] |
||
echo "The length of the fruit array is ", len(fruit)</ |
echo "The length of the fruit array is ", len(fruit)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,107: | Line 2,107: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{works with|oo2c}} |
{{works with|oo2c}} |
||
< |
<syntaxhighlight lang=oberon2> |
||
MODULE ArrayLength; |
MODULE ArrayLength; |
||
IMPORT |
IMPORT |
||
Line 2,139: | Line 2,139: | ||
Out.String("length: ");Out.Int(Length(a),0);Out.Ln |
Out.String("length: ");Out.Int(Length(a),0);Out.Ln |
||
END ArrayLength. |
END ArrayLength. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,146: | Line 2,146: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang=objeck> |
||
class Test { |
class Test { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 2,152: | Line 2,152: | ||
fruit->Size()->PrintLine(); |
fruit->Size()->PrintLine(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang=OCaml> |
||
Array.length [|"apple"; "orange"|];; |
Array.length [|"apple"; "orange"|];; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang=Oforth>[ "apple", "orange" ] size</syntaxhighlight> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
All of these methods are equivalent. |
All of these methods are equivalent. |
||
< |
<syntaxhighlight lang=scheme> |
||
(print (vector-length (vector "apple" "orange"))) |
(print (vector-length (vector "apple" "orange"))) |
||
(print (vector-length #("apple" "orange"))) |
(print (vector-length #("apple" "orange"))) |
||
Line 2,170: | Line 2,170: | ||
(print (size #("apple" "orange"))) |
(print (size #("apple" "orange"))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Onyx}}== |
=={{header|Onyx}}== |
||
< |
<syntaxhighlight lang=onyx>[`apple' `orange'] length # leaves 2 on top of the stack</syntaxhighlight> |
||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
< |
<syntaxhighlight lang=oorexx> |
||
/* REXX */ |
/* REXX */ |
||
a = .array~of('apple','orange') |
a = .array~of('apple','orange') |
||
Line 2,184: | Line 2,184: | ||
say e |
say e |
||
End |
End |
||
Say "a[2]="a[2]</ |
Say "a[2]="a[2]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 elements |
<pre>2 elements |
||
Line 2,192: | Line 2,192: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang=parigp>array = ["apple", "orange"] |
||
length(array) \\ == 2 |
length(array) \\ == 2 |
||
#array \\ == 2</ |
#array \\ == 2</syntaxhighlight> |
||
The <code>#</code> syntax is a handy shorthand. It usually looks best just on variables but it works on expressions too, possibly with parens to control precedence. |
The <code>#</code> syntax is a handy shorthand. It usually looks best just on variables but it works on expressions too, possibly with parens to control precedence. |
||
Line 2,204: | Line 2,204: | ||
{{works with|Free Pascal|2.6.2}} |
{{works with|Free Pascal|2.6.2}} |
||
< |
<syntaxhighlight lang=Pascal> |
||
#!/usr/bin/instantfpc |
#!/usr/bin/instantfpc |
||
//program ArrayLength; |
//program ArrayLength; |
||
Line 2,219: | Line 2,219: | ||
WriteLn('Length of Fruits by bounds : ', High(Fruits) - Low(Fruits) + 1); |
WriteLn('Length of Fruits by bounds : ', High(Fruits) - Low(Fruits) + 1); |
||
END. |
END. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,233: | Line 2,233: | ||
The way to get the number of elements of an array in Perl is to put the array in scalar context. |
The way to get the number of elements of an array in Perl is to put the array in scalar context. |
||
< |
<syntaxhighlight lang=perl>my @array = qw "apple orange banana", 4, 42; |
||
scalar @array; # 5 |
scalar @array; # 5 |
||
Line 2,252: | Line 2,252: | ||
takes_a_scalar @array; |
takes_a_scalar @array; |
||
# the built-ins can also act like they have prototypes</ |
# the built-ins can also act like they have prototypes</syntaxhighlight> |
||
A common mistake is to use <code>length</code> which works on strings not arrays. |
A common mistake is to use <code>length</code> which works on strings not arrays. |
||
So using it on an array, as a side-effect, actually gives you a number which represents the order of magnitude. |
So using it on an array, as a side-effect, actually gives you a number which represents the order of magnitude. |
||
< |
<syntaxhighlight lang=perl>length '' . @array; # 1 |
||
length @array; # 1 |
length @array; # 1 |
||
Line 2,267: | Line 2,267: | ||
print 'the length of @array is on the order of '; |
print 'the length of @array is on the order of '; |
||
print 10 ** (length( @array )-1); # 100 |
print 10 ** (length( @array )-1); # 100 |
||
print " elements long\n";</ |
print " elements long\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
{{libheader|Phix/basics}} |
||
<!--< |
<!--<syntaxhighlight lang=Phix>--> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">fruits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #008000;">"apple"<span style="color: #0000FF;">,<span style="color: #008000;">"orange"<span style="color: #0000FF;">}</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">fruits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{<span style="color: #008000;">"apple"<span style="color: #0000FF;">,<span style="color: #008000;">"orange"<span style="color: #0000FF;">}</span> |
||
<span style="color: #0000FF;">?<span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">fruits<span style="color: #0000FF;">) |
<span style="color: #0000FF;">?<span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">fruits<span style="color: #0000FF;">) |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,281: | Line 2,281: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang=Phixmonti>"apple" "orange" stklen tolist len print</syntaxhighlight> |
||
With syntactic sugar |
With syntactic sugar |
||
< |
<syntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt |
||
( "apple" "orange" ) len print</ |
( "apple" "orange" ) len print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,292: | Line 2,292: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang=php>print count(['apple', 'orange']); // Returns 2</syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
Line 2,300: | Line 2,300: | ||
println(length(L)), |
println(length(L)), |
||
println(L.len), |
println(L.len), |
||
println(L.length).</ |
println(L.length).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,310: | Line 2,310: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>: (length '(apple orange)) |
||
-> 2 |
-> 2 |
||
:</ |
:</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang=pike>void main() |
||
{ |
{ |
||
array fruit = ({ "apple", "orange" }); |
array fruit = ({ "apple", "orange" }); |
||
write("%d\n", sizeof(fruit)); |
write("%d\n", sizeof(fruit)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,325: | Line 2,325: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang=pli> p: Proc Options(main); |
||
Dcl a(2) Char(6) Varying Init('apple','orange'); |
Dcl a(2) Char(6) Varying Init('apple','orange'); |
||
Put Edit('Array a has',(hbound(a)-lbound(a)+1),' elements.') |
Put Edit('Array a has',(hbound(a)-lbound(a)+1),' elements.') |
||
(Skip,a,f(2),a); |
(Skip,a,f(2),a); |
||
Put Skip Data(a); |
Put Skip Data(a); |
||
End;</ |
End;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,337: | Line 2,337: | ||
=={{header|Plorth}}== |
=={{header|Plorth}}== |
||
< |
<syntaxhighlight lang=plorth>["apple", "orange"] length println</syntaxhighlight> |
||
=={{header|Pony}}== |
=={{header|Pony}}== |
||
< |
<syntaxhighlight lang=pony> |
||
actor Main |
actor Main |
||
new create(env:Env)=> |
new create(env:Env)=> |
||
Line 2,347: | Line 2,347: | ||
c.push("orange") |
c.push("orange") |
||
env.out.print("Array c is " + c.size().string() + " elements long!") |
env.out.print("Array c is " + c.size().string() + " elements long!") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Potion}}== |
=={{header|Potion}}== |
||
< |
<syntaxhighlight lang=potion>("apple", "orange") length print</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang=PowerShell> |
||
$Array = @( "Apple", "Orange" ) |
$Array = @( "Apple", "Orange" ) |
||
$Array.Count |
$Array.Count |
||
$Array.Length</ |
$Array.Length</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,363: | Line 2,363: | ||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
< |
<syntaxhighlight lang=Processing> |
||
String[] arr = {"apple", "orange"}; |
String[] arr = {"apple", "orange"}; |
||
void setup(){ |
void setup(){ |
||
println(arr.length); |
println(arr.length); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,375: | Line 2,375: | ||
==={{header|Processing Python mode}}=== |
==={{header|Processing Python mode}}=== |
||
< |
<syntaxhighlight lang=python> |
||
arr = ['apple', 'orange'] # a list for an array |
arr = ['apple', 'orange'] # a list for an array |
||
def setup(): |
def setup(): |
||
println(len(arr)) |
println(len(arr)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,388: | Line 2,388: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang=Prolog>| ?- length(["apple", "orange"], X). |
||
X = 2 |
X = 2 |
||
yes</ |
yes</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang=PureBasic> |
||
EnableExplicit |
EnableExplicit |
||
Define Dim fruit$(1); defines array with 2 elements at indices 0 and 1 |
Define Dim fruit$(1); defines array with 2 elements at indices 0 and 1 |
||
Line 2,408: | Line 2,408: | ||
CloseConsole() |
CloseConsole() |
||
EndIf |
EndIf |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,415: | Line 2,415: | ||
An abbreviated form of the above, not printing to console/terminal |
An abbreviated form of the above, not printing to console/terminal |
||
< |
<syntaxhighlight lang=PureBasic> |
||
Dim fruit$(1); defines array with 2 elements at indices 0 and 1 |
Dim fruit$(1); defines array with 2 elements at indices 0 and 1 |
||
fruit$(0) = "apple" |
fruit$(0) = "apple" |
||
fruit$(1) = "orange" |
fruit$(1) = "orange" |
||
Debug ArraySize(fruit$()) + 1 ;the number of elements is equal to the size + 1. For example: Dim a(2) contains 3 elements from a(0) to a(2) for a size of 2. |
Debug ArraySize(fruit$()) + 1 ;the number of elements is equal to the size + 1. For example: Dim a(2) contains 3 elements from a(0) to a(2) for a size of 2. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,427: | Line 2,427: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang=python>>>> print(len(['apple', 'orange'])) |
||
2 |
2 |
||
>>> </ |
>>> </syntaxhighlight> |
||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
< |
<syntaxhighlight lang=QB64> |
||
Dim max As Integer, Index As Integer |
Dim max As Integer, Index As Integer |
||
Randomize Timer |
Randomize Timer |
||
Line 2,444: | Line 2,444: | ||
Print UBound(Stringar) |
Print UBound(Stringar) |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
<Lang Quackery> $ "apples" $ "oranges" 2 pack size echo</ |
<Lang Quackery> $ "apples" $ "oranges" 2 pack size echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,454: | Line 2,454: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang=R> |
|||
<lang R> |
|||
a <- c('apple','orange') # create a vector containing "apple" and "orange" |
a <- c('apple','orange') # create a vector containing "apple" and "orange" |
||
length(a) |
length(a) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,465: | Line 2,465: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
{{trans|EchoLisp}} |
{{trans|EchoLisp}} |
||
< |
<syntaxhighlight lang=racket>#lang racket/base |
||
(length '("apple" "orange")) ;; list |
(length '("apple" "orange")) ;; list |
||
(vector-length #("apple" "orange")) ;; vector</ |
(vector-length #("apple" "orange")) ;; vector</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 2,478: | Line 2,478: | ||
To get the number of elements of an array in Raku you put the array in a coercing Numeric context, or call <code>elems</code> on it. |
To get the number of elements of an array in Raku you put the array in a coercing Numeric context, or call <code>elems</code> on it. |
||
< |
<syntaxhighlight lang=perl6> |
||
my @array = <apple orange>; |
my @array = <apple orange>; |
||
Line 2,485: | Line 2,485: | ||
say + @array; # 2 |
say + @array; # 2 |
||
say @array + 0; # 2 |
say @array + 0; # 2 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Watch out for infinite/lazy arrays though. You can't get the length of those. |
Watch out for infinite/lazy arrays though. You can't get the length of those. |
||
< |
<syntaxhighlight lang=perl6>my @infinite = 1 .. Inf; # 1, 2, 3, 4, ... |
||
say @infinite[5000]; # 5001 |
say @infinite[5000]; # 5001 |
||
say @infinite.elems; # Throws exception "Cannot .elems a lazy list" |
say @infinite.elems; # Throws exception "Cannot .elems a lazy list" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rapira}}== |
=={{header|Rapira}}== |
||
< |
<syntaxhighlight lang=Rapira>arr := <* "apple", "orange" *> |
||
output: #arr</ |
output: #arr</syntaxhighlight> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang=Red>length? ["apples" "oranges"] |
||
== 2</ |
== 2</syntaxhighlight> |
||
=={{header|Relation}}== |
=={{header|Relation}}== |
||
< |
<syntaxhighlight lang=Relation> |
||
relation fruit |
relation fruit |
||
insert "apples" |
insert "apples" |
||
Line 2,510: | Line 2,510: | ||
project fruit count |
project fruit count |
||
print |
print |
||
</syntaxhighlight> |
|||
</lang> |
|||
{| border=1 |
{| border=1 |
||
Line 2,520: | Line 2,520: | ||
=={{header|ReScript}}== |
=={{header|ReScript}}== |
||
< |
<syntaxhighlight lang=rescript>let fruits = ["apple", "orange"] |
||
Js.log(Js.Array.length(fruits))</ |
Js.log(Js.Array.length(fruits))</syntaxhighlight> |
||
< |
<syntaxhighlight lang=html><!DOCTYPE html> |
||
<html> |
<html> |
||
<head> |
<head> |
||
Line 2,537: | Line 2,537: | ||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>// Generated by ReScript, PLEASE EDIT WITH CARE |
<pre>// Generated by ReScript, PLEASE EDIT WITH CARE |
||
Line 2,554: | Line 2,554: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang=rexx>/* REXX ---------------------------------------------- |
||
* The compond variable a. implements an array |
* The compond variable a. implements an array |
||
* By convention, a.0 contains the number of elements |
* By convention, a.0 contains the number of elements |
||
Line 2,570: | Line 2,570: | ||
a.z=arg(1) |
a.z=arg(1) |
||
a.0=z |
a.0=z |
||
Return</ |
Return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>There are 2 elements in the array: |
<pre>There are 2 elements in the array: |
||
Line 2,578: | Line 2,578: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang=python>See len(['apple', 'orange']) # output = 2 </syntaxhighlight> |
||
=={{header|Robotic}}== |
=={{header|Robotic}}== |
||
Line 2,584: | Line 2,584: | ||
Example 1: |
Example 1: |
||
< |
<syntaxhighlight lang=robotic> |
||
set "index" to 0 |
set "index" to 0 |
||
set "$array&index&" to "apple" |
set "$array&index&" to "apple" |
||
Line 2,590: | Line 2,590: | ||
set "$array&index&" to "orange" |
set "$array&index&" to "orange" |
||
* "Array length: ('index' + 1)" |
* "Array length: ('index' + 1)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example 2: |
Example 2: |
||
< |
<syntaxhighlight lang=robotic> |
||
set "index" to 0 |
set "index" to 0 |
||
set "local1" to random 1 to 99 |
set "local1" to random 1 to 99 |
||
Line 2,602: | Line 2,602: | ||
if "local1" > 1 then "rand" |
if "local1" > 1 then "rand" |
||
* "Array length: ('index')" |
* "Array length: ('index')" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang=ruby>puts ['apple', 'orange'].length # or .size</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 2,612: | Line 2,612: | ||
By default arrays are immutable in rust. |
By default arrays are immutable in rust. |
||
< |
<syntaxhighlight lang=rust> |
||
fn main() { |
fn main() { |
||
let array = ["foo", "bar", "baz", "biff"]; |
let array = ["foo", "bar", "baz", "biff"]; |
||
println!("the array has {} elements", array.len()); |
println!("the array has {} elements", array.len()); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang=scala> |
||
println(Array("apple", "orange").length) |
println(Array("apple", "orange").length) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 2,629: | Line 2,629: | ||
Using Scheme's vector type as an equivalent to an array: |
Using Scheme's vector type as an equivalent to an array: |
||
< |
<syntaxhighlight lang=scheme> |
||
(display (vector-length #("apple" "orange"))) |
(display (vector-length #("apple" "orange"))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
The function [http://seed7.sourceforge.net/libraries/array.htm#length(in_arrayType) length] |
The function [http://seed7.sourceforge.net/libraries/array.htm#length(in_arrayType) length] |
||
determines the length of an array. |
determines the length of an array. |
||
< |
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
||
const array string: anArray is [] ("apple", "orange"); |
const array string: anArray is [] ("apple", "orange"); |
||
Line 2,643: | Line 2,643: | ||
begin |
begin |
||
writeln(length(anArray)); |
writeln(length(anArray)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang=sensetalk>put ("apple", "orange", "pear", "banana", "aubergine") into fruits |
||
put the number of items in fruits</ |
put the number of items in fruits</syntaxhighlight> |
||
=={{header|Shen}}== |
=={{header|Shen}}== |
||
< |
<syntaxhighlight lang=shen> |
||
\\ Using a vector |
\\ Using a vector |
||
\\ @v creates the vector |
\\ @v creates the vector |
||
Line 2,661: | Line 2,661: | ||
\\ As an list |
\\ As an list |
||
(length [apple orange]) |
(length [apple orange]) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang=ruby>var arr = ['apple', 'orange']; |
||
say arr.len; #=> 2 |
say arr.len; #=> 2 |
||
say arr.end; #=> 1 (zero based)</ |
say arr.end; #=> 1 (zero based)</syntaxhighlight> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang=simula>COMMENT ARRAY-LENGTH; |
||
BEGIN |
BEGIN |
||
Line 2,685: | Line 2,685: | ||
OUTIMAGE; |
OUTIMAGE; |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,692: | Line 2,692: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang=smalltalk> |
||
a := #('apple' 'orange'). |
a := #('apple' 'orange'). |
||
a size</ |
a size</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang=SNOBOL4> ar = ARRAY('2,2') |
||
ar<1,1> = 'apple' |
ar<1,1> = 'apple' |
||
ar<1,2> = 'first' |
ar<1,2> = 'first' |
||
Line 2,703: | Line 2,703: | ||
ar<2,2> = 'second' |
ar<2,2> = 'second' |
||
OUTPUT = IDENT(DATATYPE(ar), 'ARRAY') PROTOTYPE(ar) |
OUTPUT = IDENT(DATATYPE(ar), 'ARRAY') PROTOTYPE(ar) |
||
end</ |
end</syntaxhighlight> |
||
{{out}}<pre>2,2</pre> |
{{out}}<pre>2,2</pre> |
||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
< |
<syntaxhighlight lang=spl>a = ["apple","orange"] |
||
#.output("Number of elements in array: ",#.size(a,1))</ |
#.output("Number of elements in array: ",#.size(a,1))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,716: | Line 2,716: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
< |
<syntaxhighlight lang=sql>SELECT COUNT() FROM (VALUES ('apple'),('orange'));</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang=sml>let |
||
val a = Array.fromList ["apple", "orange"] |
val a = Array.fromList ["apple", "orange"] |
||
in |
in |
||
Array.length a |
Array.length a |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
Line 2,729: | Line 2,729: | ||
=== Dimensions of a dataset === |
=== Dimensions of a dataset === |
||
< |
<syntaxhighlight lang=stata>clear |
||
input str10 fruit |
input str10 fruit |
||
apple |
apple |
||
Line 2,736: | Line 2,736: | ||
di _N |
di _N |
||
di c(N) " " c(k)</ |
di c(N) " " c(k)</syntaxhighlight> |
||
=== Length of a macro list === |
=== Length of a macro list === |
||
Line 2,742: | Line 2,742: | ||
Use either the '''[https://www.stata.com/help.cgi?macrolists sizeof]''' macro list function or the '''[https://www.stata.com/help.cgi?extended_fcn word count]''' extended macro function. Notice that the argument of the former is the macro ''name'', while the argument of the latter is the macro ''contents''. |
Use either the '''[https://www.stata.com/help.cgi?macrolists sizeof]''' macro list function or the '''[https://www.stata.com/help.cgi?extended_fcn word count]''' extended macro function. Notice that the argument of the former is the macro ''name'', while the argument of the latter is the macro ''contents''. |
||
< |
<syntaxhighlight lang=stata>local fruits apple orange |
||
di `: list sizeof fruits' |
di `: list sizeof fruits' |
||
di `: word count `fruits''</ |
di `: word count `fruits''</syntaxhighlight> |
||
=== Mata === |
=== Mata === |
||
For a Mata array, use '''[https://www.stata.com/help.cgi?mf_rows rows]''' and similar functions: |
For a Mata array, use '''[https://www.stata.com/help.cgi?mf_rows rows]''' and similar functions: |
||
< |
<syntaxhighlight lang=stata>mata |
||
a=st_sdata(.,"fruit") |
a=st_sdata(.,"fruit") |
||
rows(a) |
rows(a) |
||
cols(a) |
cols(a) |
||
length(a) |
length(a) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang=Swift>let fruits = ["apple", "orange"] // Declare constant array literal |
||
let fruitsCount = fruits.count // Declare constant array length (count) |
let fruitsCount = fruits.count // Declare constant array length (count) |
||
print(fruitsCount) // Print array length to output window</ |
print(fruitsCount) // Print array length to output window</syntaxhighlight> |
||
{{out}}<pre>2</pre> |
{{out}}<pre>2</pre> |
||
=={{header|Symsyn}}== |
=={{header|Symsyn}}== |
||
< |
<syntaxhighlight lang=Symsyn> |
||
| Symsyn does not support Array of Strings |
| Symsyn does not support Array of Strings |
||
| The following code for an Array of Integers |
| The following code for an Array of Integers |
||
Line 2,779: | Line 2,779: | ||
sz [] |
sz [] |
||
| shows 2 |
| shows 2 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
< |
<syntaxhighlight lang=tailspin> |
||
['apple', 'orange'] -> $::length -> !OUT::write |
['apple', 'orange'] -> $::length -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>2</pre> |
<pre>2</pre> |
||
Line 2,791: | Line 2,791: | ||
<!-- http://ideone.com/uotEvm --> |
<!-- http://ideone.com/uotEvm --> |
||
< |
<syntaxhighlight lang=tcl>;# not recommended: |
||
set mylistA {apple orange} ;# actually a string |
set mylistA {apple orange} ;# actually a string |
||
set mylistA "Apple Orange" ;# same - this works only for simple cases |
set mylistA "Apple Orange" ;# same - this works only for simple cases |
||
Line 2,804: | Line 2,804: | ||
set lenB [llength $mylistB] |
set lenB [llength $mylistB] |
||
puts "$mylistB : $lenB" |
puts "$mylistB : $lenB" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,813: | Line 2,813: | ||
{{works with|TI-83 2.55MP}} |
{{works with|TI-83 2.55MP}} |
||
Use function <code>dim()</code>. |
Use function <code>dim()</code>. |
||
< |
<syntaxhighlight lang=ti83b>{1,3,–5,4,–2,–1}→L1 |
||
dim(L1)</ |
dim(L1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,821: | Line 2,821: | ||
=={{header|Transd}}== |
=={{header|Transd}}== |
||
< |
<syntaxhighlight lang=scheme>#lang transd |
||
MainModule : { |
MainModule : { |
||
Line 2,830: | Line 2,830: | ||
(lout (size ["apple", "orange"])) |
(lout (size ["apple", "orange"])) |
||
) |
) |
||
}</ |
}</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
2 |
2 |
||
Line 2,837: | Line 2,837: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
< |
<syntaxhighlight lang=bash>#!/bin/bash |
||
array=("orange" "apple") |
array=("orange" "apple") |
||
echo "${#array[@]}"</ |
echo "${#array[@]}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,855: | Line 2,855: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang=vala>void main() { |
||
string[] fruit = {"apple", "orange"}; |
string[] fruit = {"apple", "orange"}; |
||
stdout.printf("%d\n", fruit.length); |
stdout.printf("%d\n", fruit.length); |
||
}</ |
}</syntaxhighlight> |
||
Note that any of the following array declarations could be used: |
Note that any of the following array declarations could be used: |
||
< |
<syntaxhighlight lang=vala> |
||
var fruit = new string[] { "apple", "orange" }; |
var fruit = new string[] { "apple", "orange" }; |
||
string[] fruit = new string[] { "apple", "orange" }; |
string[] fruit = new string[] { "apple", "orange" }; |
||
string[] fruit = { "apple", "orange" }; |
string[] fruit = { "apple", "orange" }; |
||
</syntaxhighlight> |
|||
</lang> |
|||
A shorter variant could also have been used: |
A shorter variant could also have been used: |
||
< |
<syntaxhighlight lang=vala> |
||
void main() { |
void main() { |
||
stdout.printf("%d\n", new string[] {"apples", "orange"}.length); |
stdout.printf("%d\n", new string[] {"apples", "orange"}.length); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
One-liner. Assumes array lower bound, which is not always safe. |
One-liner. Assumes array lower bound, which is not always safe. |
||
< |
<syntaxhighlight lang=vb>Debug.Print "Array Length: " & UBound(Array("apple", "orange")) + 1</syntaxhighlight> |
||
Works regardless of lower bound: |
Works regardless of lower bound: |
||
< |
<syntaxhighlight lang=vb>Dim funkyArray(7 to 8) As String |
||
Public Function SizeOfArray(ar As Variant) As Long |
Public Function SizeOfArray(ar As Variant) As Long |
||
Line 2,888: | Line 2,888: | ||
'call the function |
'call the function |
||
Debug.Print "Array Length: " & SizeOfArray(funkyArray)</ |
Debug.Print "Array Length: " & SizeOfArray(funkyArray)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,895: | Line 2,895: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang=vb>arr = Array("apple","orange") |
||
WScript.StdOut.WriteLine UBound(arr) - LBound(arr) + 1 </ |
WScript.StdOut.WriteLine UBound(arr) - LBound(arr) + 1 </syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,904: | Line 2,904: | ||
The amount of elements in an array in Visual Basic is computed via the upper bound and lower bound indices. In Visual Basic the indices of arrays have to be numeric, but it is even possible to have negative values for them. Of course the element numbering is continuous. |
The amount of elements in an array in Visual Basic is computed via the upper bound and lower bound indices. In Visual Basic the indices of arrays have to be numeric, but it is even possible to have negative values for them. Of course the element numbering is continuous. |
||
<syntaxhighlight lang=vb> |
|||
<lang vb> |
|||
' declared in a module |
' declared in a module |
||
Public Function LengthOfArray(ByRef arr As Variant) As Long |
Public Function LengthOfArray(ByRef arr As Variant) As Long |
||
Line 2,931: | Line 2,931: | ||
Debug.Print LengthOfArray(arr) ' prints 2 as result |
Debug.Print LengthOfArray(arr) ' prints 2 as result |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{works with|Visual Basic .NET|9.0+}} |
{{works with|Visual Basic .NET|9.0+}} |
||
< |
<syntaxhighlight lang=vbnet>Module ArrayLength |
||
Sub Main() |
Sub Main() |
||
Line 2,943: | Line 2,943: | ||
End Module |
End Module |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,952: | Line 2,952: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
A '''len''' property is maintained for all V arrays. |
A '''len''' property is maintained for all V arrays. |
||
< |
<syntaxhighlight lang=go>// V, array length |
||
// Tectonics: v run array-length.v |
// Tectonics: v run array-length.v |
||
module main |
module main |
||
Line 2,960: | Line 2,960: | ||
arr := ["apple", "orange"] |
arr := ["apple", "orange"] |
||
println(arr.len) |
println(arr.len) |
||
}</ |
}</syntaxhighlight> |
||
{{out}}<pre>prompt$ v run array-length.v |
{{out}}<pre>prompt$ v run array-length.v |
||
Line 2,966: | Line 2,966: | ||
=={{header|WDTE}}== |
=={{header|WDTE}}== |
||
< |
<syntaxhighlight lang=wdte>let io => import 'io'; |
||
let a => ['apple'; 'orange']; |
let a => ['apple'; 'orange']; |
||
len a -- io.writeln io.stdout;</ |
len a -- io.writeln io.stdout;</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang=ecmascript>var arr = ["apple", "orange"] |
||
System.print(arr.count)</ |
System.print(arr.count)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,980: | Line 2,980: | ||
=={{header|XLISP}}== |
=={{header|XLISP}}== |
||
< |
<syntaxhighlight lang=lisp>(vector-length #("apple" "orange"))</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang=yabasic> |
||
dim fruta$(3) |
dim fruta$(3) |
||
read fruta$(1), fruta$(2), fruta$(3) |
read fruta$(1), fruta$(2), fruta$(3) |
||
Line 2,992: | Line 2,992: | ||
print fruta$(2) |
print fruta$(2) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
3 |
3 |
||
Line 3,004: | Line 3,004: | ||
The simplest way to implement an array of variable-length strings is to have the array contain pointers to said strings rather than the strings themselves. That way, the elements of the array are of equal length, which makes any array <b>much</b> easier to work with. |
The simplest way to implement an array of variable-length strings is to have the array contain pointers to said strings rather than the strings themselves. That way, the elements of the array are of equal length, which makes any array <b>much</b> easier to work with. |
||
< |
<syntaxhighlight lang=z80> org &8000 |
||
ld hl,TestArray |
ld hl,TestArray |
||
call GetArrayLength_WordData_NullTerminated |
call GetArrayLength_WordData_NullTerminated |
||
Line 3,048: | Line 3,048: | ||
byte "Apple",0 |
byte "Apple",0 |
||
Orange: |
Orange: |
||
byte "Orange",0</ |
byte "Orange",0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,057: | Line 3,057: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
zkl doesn't support arrays natively, use lists instead. |
zkl doesn't support arrays natively, use lists instead. |
||
< |
<syntaxhighlight lang=zkl>List("apple", "orange").len().println() //-->2, == L("apple", "orange") |
||
T("apple", "orange").len().println() //-->2, read only list (ROList) </ |
T("apple", "orange").len().println() //-->2, read only list (ROList) </syntaxhighlight> |
||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang=zig>const std = @import("std"); |
||
pub fn main() !void { |
pub fn main() !void { |
||
Line 3,069: | Line 3,069: | ||
// slices and arrays have an len field |
// slices and arrays have an len field |
||
try stdout_wr.print("fruit.len = {d}\n", .{fruit.len}); |
try stdout_wr.print("fruit.len = {d}\n", .{fruit.len}); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Zoea}}== |
=={{header|Zoea}}== |
||
< |
<syntaxhighlight lang=Zoea> |
||
program: array_length |
program: array_length |
||
input: [a,b,c] |
input: [a,b,c] |
||
output: 3 |
output: 3 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Zoea Visual}}== |
=={{header|Zoea Visual}}== |
||
Line 3,082: | Line 3,082: | ||
=={{header|zonnon}}== |
=={{header|zonnon}}== |
||
< |
<syntaxhighlight lang=zonnon> |
||
module AryLength; |
module AryLength; |
||
type |
type |
||
Line 3,097: | Line 3,097: | ||
writeln(len(b,1):4) (* second dimension *) |
writeln(len(b,1):4) (* second dimension *) |
||
end AryLength. |
end AryLength. |
||
</syntaxhighlight> |
|||
</lang> |