Array length: Difference between revisions

→‎Forth: use forth highlighting
m (syntax highlighting fixup automation)
(→‎Forth: use forth highlighting)
(38 intermediate revisions by 28 users not shown)
Line 1:
{{task}}
[[Category:Simple]]
{{task}}
 
;Task:
Line 13:
 
=={{header|11l}}==
<syntaxhighlight lang="11l">print([‘apple’, ‘orange’].len)</syntaxhighlight>
{{out}}
<pre>
Line 21:
=={{header|360 Assembly}}==
Array length is computed at compilation time with the formula : (AEND-A)/L'A
<syntaxhighlight lang="360asm">* Array length 22/02/2017
ARRAYLEN START
USING ARRAYLEN,12
Line 40:
{{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.
<syntaxhighlight lang="6502asm">start:
LDA #(Array_End-Array) ;evaluates to 13
RTS
Line 55:
 
 
<syntaxhighlight lang="68000devpac">start:
MOVE.B #(MyArray_End-MyArray)/4 ;evaluates to 2
RTS
Line 72:
 
=={{header|8th}}==
<syntaxhighlight lang="forth">
["apples", "oranges"] a:len . cr
</syntaxhighlight>
Line 82:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program lenAreaString64.s */
Line 152:
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"abap">
report z_array_length.
 
Line 167:
=={{header|Ada}}==
 
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with System;
 
procedure Array_Length is
 
Fruits : constant array (Positive range <>) of access constant String
:= (new String'("orange"),
new String'("apple"));
 
Memory_Size : constant Integer := Fruits'Size / System.Storage_Unit;
begin
for Fruit of Fruits loop
Ada.Text_IO.Put (Integer'Image (Fruit'Length));
end loop;
 
begin
Ada.Text_IO.Put_Line (" Array Size : " & Integer'Image (Fruits'Length));
Put_Line ("Number of elements : " & Fruits'Length'Image);
Put_Line ("Array memory Size : " & Memory_Size'Image & " bytes" );
Put_Line (" " & Integer'Image (Memory_Size * System.Storage_Unit / System.Word_Size) & " words" );
end Array_Length;</syntaxhighlight>
 
{{out}}
<pre>
<pre> 6 5 Array Size: 2</pre>
Number of elements : 2
Array memory Size : 32 bytes
4 words
</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68"># UPB returns the upper bound of an array, LWB the lower bound #
[]STRING fruits = ( "apple", "orange" );
print( ( ( UPB fruits - LWB fruits ) + 1, newline ) ) # prints 2 #</syntaxhighlight>
 
=={{header|AntLang}}==
<syntaxhighlight lang=AntLang"antlang">array: seq["apple"; "orange"]
length[array]
/Works as a one liner: length[seq["apple"; "orange"]]</syntaxhighlight>
 
=={{header|Apex}}==
<syntaxhighlight lang="apex">System.debug(new String[] { 'apple', 'banana' }.size()); // Prints 2</syntaxhighlight>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">⍴'apple' 'orange'</syntaxhighlight>
Output:
<pre>2</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang=AppleScript"applescript">
set theList to {"apple", "orange"}
count theList
Line 222 ⟶ 229:
<pre>fold (λx n -> 1 + n) 0</pre>
 
<syntaxhighlight lang=AppleScript"applescript">on run
set xs to ["alpha", "beta", "gamma", "delta", "epsilon", ¬
Line 271 ⟶ 278:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program lenAreaString.s */
Line 411 ⟶ 418:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">fruit: ["apple" "orange"]
 
print ["array length =" size fruit]</syntaxhighlight>
Line 420 ⟶ 427:
 
=={{header|ATS}}==
<syntaxhighlight lang=ATS"ats">
#include
"share/atspre_staload.hats"
Line 436 ⟶ 443:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">MsgBox % ["apple","orange"].MaxIndex()</syntaxhighlight>
{{Out}}<pre>2</pre>
 
=={{header|AutoIt}}==
<syntaxhighlight lang=AutoIt"autoit">Opt('MustDeclareVars',1) ; 1 = Variables must be pre-declared.
 
Local $aArray[2] = ["Apple", "Orange"]
Line 459 ⟶ 466:
Using Avail's tuples and the `|_|` method:
 
<syntaxhighlight lang=Avail"avail">|<"Apple", "Orange">|</syntaxhighlight>
 
=={{header|AWK}}==
Line 468 ⟶ 475:
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) {
Line 492 ⟶ 499:
=={{header|BaCon}}==
BaCon knows three types of arrays, the UBOUND function can query all.
<syntaxhighlight lang="bacon">' Static arrays
 
DECLARE fruit$[] = { "apple", "orange" }
Line 513 ⟶ 520:
=={{header|Bash}}==
 
<syntaxhighlight lang="bash">
fruit=("apple" "orange" "lemon")
echo "${#fruit[@]}"</syntaxhighlight>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">DIM X$(1 TO 2)
X$(1) = "apple"
X$(2) = "orange"
Line 524 ⟶ 531:
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic">10 DIM A$(2)
20 A$(1) = "ORANGE"
30 A$(2) = "APPLE"
Line 560 ⟶ 567:
 
==={{header|BASIC256}}===
<syntaxhighlight lang=BASIC256"basic256">
fruta$ = {"apple", "orange", "pear"}
Line 573 ⟶ 580:
orange
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
Unless modified with OPTION BASE 1 or MAT ORIGIN 1, the lower limit of an array is 1.
<syntaxhighlight lang="qbasic">10 dim fruta$(2)
20 read fruta$(0),fruta$(1),fruta$(2)
30 data "apple","orange","lemon"
40 print "The length of the array 'fruit$' is ";ubound(fruta$)+1
50 end</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
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}}
<syntaxhighlight lang="basic">10 DIM A$(1):REM 1=LAST -> ROOM FOR 2
20 A$(0) = "ORANGE"
30 A$(1) = "APPLE"
Line 591 ⟶ 607:
{{Out}}
<pre>A$ HAS 2 ELEMENTS.</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 STRING X$(1 TO 2)
110 LET X$(1)="apple":LET X$(2)="orange"
120 PRINT "The length of the array 'X$' is";SIZE(X$)</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">DIM fruta$(2)
DIM fruta$(2)
READ fruta$(1), fruta$(2)
DATA "apple", "orange"
Line 601 ⟶ 621:
 
PRINT "La longitud del array fruta$ es" ; tamano
END</syntaxhighlight>
END
</syntaxhighlight>
 
{{out}}
<pre> La longitud del array fruta$ es 2 </pre>
Line 610 ⟶ 628:
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$
DATA "apple", "orange"
 
PRINT "The length of the array 'fruit$' is "; SIZE(fruit$)
END</syntaxhighlight>
END
{{out}}
</syntaxhighlight>
<pre> The length of the array 'fruit$' is 2 </pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Array length"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
DIM F$[2]
F$[0] = "apple"
F$[1] = "orange"
F$[2] = "pear"
 
PRINT "The length of the fruit array is "; UBOUND(F$[])
PRINT F$[1]
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre> The length of the fruit array 'fruit$' is 2 </pre>2
orange</pre>
 
=={{header|Batch File}}==
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
 
Line 667 ⟶ 702:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> DIM array$(1)
array$() = "apple", "orange"
PRINT "Number of elements in array = "; DIM(array$(), 1) + 1
Line 676 ⟶ 711:
<pre>Number of elements in array = 2
Number of bytes in all elements combined = 11</pre>
 
=={{header|Beef}}==
<syntaxhighlight lang="csharp">using System;
 
namespace ArrayLength
{
class Program
{
public static void Main()
{
var array = new String[]("apple", "orange");
Console.WriteLine(array.Count);
delete(array);
}
}
}
</syntaxhighlight>
 
=={{header|Brat}}==
<syntaxhighlight lang="brat">
p ["apple", "orange"].length
</syntaxhighlight>
 
=={{header|Binary Lambda Calculus}}==
 
BLC has no arrays, so here's a function to compute the length of a given list (as a church numeral) instead, corresponding to https://github.com/tromp/AIT/blob/master/lists/length.lam :
 
<pre>010001101000000101100000000000011100101010111111110111111101111011010000010</pre>
 
=={{header|BQN}}==
 
<code>≠</code> gives the length of an array in BQN.
<syntaxhighlight lang="bqn">≠ 1‿"a"‿+</syntaxhighlight>
<syntaxhighlight lang="bqn">3</syntaxhighlight>
[https://mlochbaum.github.io/BQN/try.html#code=4omgIDHigL8iYSLigL8r Try It!]
 
Line 697 ⟶ 755:
For static arrays:
 
<syntaxhighlight lang="c">
#include <stdio.h>
 
Line 723 ⟶ 781:
A C pre-processor macro may be created for ease of use:
 
<syntaxhighlight lang="c">
#define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0]))
</syntaxhighlight>
Line 744 ⟶ 802:
</p>
====Solution====
<syntaxhighlight lang=C"c">#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS
 
Line 827 ⟶ 885:
 
====An example why sizeof(A)/sizeof(E) may be a bad idea in C====
<syntaxhighlight lang=C"c">#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS
 
Line 1,005 ⟶ 1,063:
=={{header|C sharp|C#}}==
 
<syntaxhighlight lang="csharp">
using System;
 
Line 1,020 ⟶ 1,078:
Note that any of the following array declarations could be used:
 
<syntaxhighlight lang="csharp">
var fruit = new[] { "apple", "orange" };
var fruit = new string[] { "apple", "orange" };
Line 1,030 ⟶ 1,088:
A shorter variant could also have been used:
 
<syntaxhighlight lang="csharp">
using static System.Console;
 
Line 1,048 ⟶ 1,106:
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 <string>
Line 1,063 ⟶ 1,121:
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.
<syntaxhighlight lang="cpp"> std::vector<std::string> fruitV({ "apples", "oranges" });
std::list<std::string> fruitL({ "apples", "oranges" });
std::deque<std::string> fruitD({ "apples", "oranges" });
Line 1,071 ⟶ 1,129:
 
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">shared void run() {
value array = ["apple", "orange"];
print(array.size);
Line 1,078 ⟶ 1,136:
=={{header|Clipper/XBase++}}==
 
<syntaxhighlight lang=Clipper"clipper/XBasexbase++">/*
* nizchka: March - 2016
* This is a Clipper/XBase++ of RosettaCode Array_Length
Line 1,094 ⟶ 1,152:
=={{header|Clojure}}==
 
<syntaxhighlight lang="clojure">; using count:
(count ["apple" "orange"])
 
Line 1,103 ⟶ 1,161:
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"cobol"> identification division.
program-id. array-length.
 
Line 1,151 ⟶ 1,209:
=={{header|ColdFusion}}==
 
<syntaxhighlight lang="coldfusion">
<cfset testArray = ["apple","orange"]>
<cfoutput>Array Length = #ArrayLen(testArray)#</cfoutput>
Line 1,159 ⟶ 1,217:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang=Lisp"lisp">
(print (length #("apple" "orange")))
</syntaxhighlight>
Line 1,165 ⟶ 1,223:
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1]
 
<syntaxhighlight lang="lisp">
;; Project : Array length
 
Line 1,182 ⟶ 1,240:
=={{header|Component Pascal}}==
{{works with|BlackBox Component Builder}}
<syntaxhighlight lang="oberon2">
MODULE AryLen;
IMPORT StdLog;
Line 1,224 ⟶ 1,282:
=={{header|Crystal}}==
 
<syntaxhighlight lang="ruby">
puts ["apple", "orange"].size
</syntaxhighlight>
Line 1,234 ⟶ 1,292:
 
=={{header|D}}==
<syntaxhighlight lang="d">
import std.stdio;
 
Line 1,246 ⟶ 1,304:
 
Or a somewhat shorter...
<syntaxhighlight lang="d">
import std.stdio;
 
Line 1,256 ⟶ 1,314:
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">
arrLength(arr) {
return arr.length;
Line 1,269 ⟶ 1,327:
 
=={{header|DataWeave}}==
<syntaxhighlight lang=DataWeave"dataweave">var arr = ["apple", "orange"]
sizeOf(arr)
</syntaxhighlight>
 
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">
showmessage( length(['a','b','c']).ToString );
</syntaxhighlight>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">set_namespace(rosettacode)_me();
 
me_msg()_array()_values(apple,orange)_length();
Line 1,290 ⟶ 1,348:
 
=={{header|Dragon}}==
<syntaxhighlight lang="dragon">select "std"
 
a = ["apple","orange"]
Line 1,298 ⟶ 1,356:
</syntaxhighlight>
 
=={{header|Dyalectdt}}==
<syntaxhighlight lang="dt">[ "apple" "orange" ] len</syntaxhighlight>
 
=={{header|Dyalect}}==
<syntaxhighlight lang=dyalect>var xs = ["apple", "orange"]
<syntaxhighlight lang="dyalect">var xs = ["apple", "orange"]
print(xs.Length())</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">fruit$[] = [ "apples" "oranges" ]
print len fruit$[]</syntaxhighlight>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(length '("apple" "orange")) ;; list
→ 2
Line 1,314 ⟶ 1,374:
→ 2
</syntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">String[] array = ["apple", "orange"];
Int length = array.size;</syntaxhighlight>
 
=={{header|Ela}}==
<syntaxhighlight lang="ela">length [1..10]</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 5.0 :
<syntaxhighlight lang="elena">
var array := new string[]{"apple", "orange"};
var length := array.Length;
Line 1,326 ⟶ 1,390:
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">iex(1)> length( ["apple", "orange"] ) # List
2
iex(2)> tuple_size( {"apple", "orange"} ) # Tuple
Line 1,332 ⟶ 1,396:
 
=={{header|Elm}}==
<syntaxhighlight lang="elm">
import Array
import Html
Line 1,341 ⟶ 1,405:
|> Array.fromList
|> Array.length
|> BasicsString.toStringfromInt
|> Html.text
</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang=Lisp"lisp">(length ["apple" "orange"])
=> 2</syntaxhighlight>
 
<code>length</code> also accepts a list or a string.
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
writeLine(text["apple", "orange"].length)
</syntaxhighlight>
 
=={{header|Erlang}}==
<syntaxhighlight lang=Erlang"erlang">
1> length(["apple", "orange"]). %using a list
2
Line 1,361 ⟶ 1,430:
 
=={{header|Euphoria}}==
<syntaxhighlight lang=Euphoria"euphoria">
sequence s = {"apple","orange",2.95} -- Euphoria doesn't care what you put in a sequence
 
Line 1,384 ⟶ 1,453:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">[|1;2;3|].Length |> printfn "%i"</syntaxhighlight>
Or:
<syntaxhighlight lang="fsharp">[|1;2;3|] |> Array.length |> printfn "%i"</syntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">
{ "apple" "orange" } length
</syntaxhighlight>
 
=={{header|Fennel}}==
<syntaxhighlight lang="fennel">(length [:apple :orange])</syntaxhighlight>
 
=={{header|Forth}}==
Line 1,399 ⟶ 1,471:
The code is commented to explain what is going on for those unfamiliar with Forth.
 
<syntaxhighlight lang="forth">: STRING, ( caddr len -- ) \ Allocate space & compile string into memory
HERE OVER CHAR+ ALLOT PLACE ;
 
Line 1,427 ⟶ 1,499:
R> ; \ return counter to data stack </syntaxhighlight>
Test code at Forth console
<syntaxhighlight lang="forth">CREATE Q { " Apples" " Oranges" } q {len} . 2 ok</syntaxhighlight>
 
=={{header|Fortran}}==
Line 1,440 ⟶ 1,512:
For a simple example, the WRITE(6,*) suffices: write to standard output (the 6), in free-format (the *).
 
<syntaxhighlight lang=Fortran"fortran">
MODULE EXAMPLE
CONTAINS
Line 1,475 ⟶ 1,547:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim fruit(1) As String = {"apple", "orange"}
Line 1,490 ⟶ 1,562:
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">
a = ["apple", "orange"]
println[length[a]]
Line 1,496 ⟶ 1,568:
 
=={{header|FurryScript}}==
<syntaxhighlight lang="furryscript">THE_LIST( <apple> <orange> )
COUNT[ 0 SW ~| COUNT_STEP# 0 SW SU ]
COUNT_STEP[ DR 1 SU ]
Line 1,506 ⟶ 1,578:
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"futhark">
fun length(as: []int): int = (shape as)[0]
</syntaxhighlight>
Line 1,512 ⟶ 1,584:
=={{header|FutureBasic}}==
NSUInteger count = fn ArrayCount( ''array'' ). Example:
<syntaxhighlight lang="futurebasic">
window 1
print fn ArrayCount( @[@"apple",@"orange",@"cherry",@"grape",@"lemon"] )
HandleEvents
</syntaxhighlight>
 
We can also use FB's len() function to get the length of an array.
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFArrayRef array = @[@"apple",@"orange",@"cherry",@"grape",@"lemon"]
print len(array)
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
Line 1,524 ⟶ 1,609:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Array_length}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
The cardinality expression reduces to the number of subexpressions the given expression has, including if the expressions is a list:
 
[[File:Fōrmulæ - Array length 01.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edite/d, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Array length 02.png]]
In '''[https://formulae.org/?example=Array_length this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
'''[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]
 
Line 1,544 ⟶ 1,633:
 
=={{header|Genie}}==
<syntaxhighlight lang="genie">[indent=4]
/* Array length, in Genie */
init
Line 1,558 ⟶ 1,647:
=={{header|Go}}==
 
<syntaxhighlight lang=Go"go">package main
 
import "fmt"
Line 1,576 ⟶ 1,665:
=={{header|Groovy}}==
 
<syntaxhighlight lang=Groovy"groovy">
def fruits = ['apple','orange']
println fruits.size()
Line 1,583 ⟶ 1,672:
=={{header|Harbour}}==
 
<syntaxhighlight lang="visualfoxpro">
LOCAL aFruits := {"apple", "orange"}
Qout( Len( aFruits ) ) // --> 2
Line 1,590 ⟶ 1,679:
=={{header|Haskell}}==
 
<syntaxhighlight lang=Haskell"haskell">-- [[Char]] -> Int
length ["apple", "orange"]</syntaxhighlight>
 
=={{header|hexiscript}}==
<syntaxhighlight lang="hexiscript">let a arr 2
let a[0] "apple"
let a[1] "orange"
Line 1,600 ⟶ 1,689:
 
=={{header|Hoon}}==
<syntaxhighlight lang=Hoon"hoon">|= arr=(list *) (lent arr)</syntaxhighlight>
 
=={{header|i}}==
<syntaxhighlight lang="i">main: print(#["apple", "orange"])</syntaxhighlight>
 
=={{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.
 
<syntaxhighlight lang="unicon">write(*["apple", "orange"])</syntaxhighlight>
 
=={{header|Idris}}==
<syntaxhighlight lang=Idris"idris">length ["apple", "orange"]</syntaxhighlight>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(len ['apple' 'orange'])</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="j"> # 'apple';'orange'
2
$ 'apple';'orange'
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.
<syntaxhighlight lang="j"> $#'apple';'orange'
 
$$'apple';'orange'
1</syntaxhighlight>
This might be a clearer concept with a few more examples. Here's an array with two dimensions:
<syntaxhighlight lang="j"> >'apple';'orange'
apple
orange
Line 1,633 ⟶ 1,725:
2</syntaxhighlight>
And, here's an array with no dimensions:
<syntaxhighlight lang="text"> 9001
9001
#9001
Line 1,640 ⟶ 1,732:
</syntaxhighlight>
You can count the number of dimensions of an array (the length of the list of lengths) using <code>#$array</code>:
<syntaxhighlight lang="j">
#$9001
0
Line 1,650 ⟶ 1,742:
=={{header|Janet}}==
 
<syntaxhighlight lang="janet">
(def our-array @["apple" "orange"])
(length our-array)
Line 1,661 ⟶ 1,753:
 
=={{header|Java}}==
The resulting ''array'' object will have a ''length'' field.
<syntaxhighlight lang=java>public class ArrayLength {
<syntaxhighlight lang="java">
public static void main(String[] args) {
String[] strings = { System.out.println(new String[]{"apple", "orange" }.length);
int length = strings.length;
}
}</syntaxhighlight>
Additionally, you can do this in one line, if need be.
<syntaxhighlight lang="java">
int length = new String[] { "apple", "orange" }.length;
</syntaxhighlight>
 
=={{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.
Line 1,675 ⟶ 1,771:
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"javascript">function last(lst) {
return lst[lst.length - 1];
}</syntaxhighlight>
Line 1,683 ⟶ 1,779:
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"javascript">function last(lst) {
return lst.reduce(function (a, x) {
return x;
Line 1,691 ⟶ 1,787:
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"javascript">function last(list, defaultValue) {
return list.length ?list[list.length-1] :defaultValue;
}</syntaxhighlight>
Line 1,697 ⟶ 1,793:
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"javascript">function last(list, defaultValue) {
return list.slice(-1)[0] || defaultValue;
}</syntaxhighlight>
 
=={{header|jqJoy}}==
<syntaxhighlight lang="joy">["apple" "orange"] size.</syntaxhighlight>
{{out}}
<pre>2</pre>
 
=={{header|jq}}==
<syntaxhighlight lang=jq>["apple","orange"] | length</syntaxhighlight>
<syntaxhighlight lang="jq">["apple","orange"] | length</syntaxhighlight>
Output:
<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.
 
=={{header|Jsish}}==
<syntaxhighlight lang="javascript">/* Array length, in jsish */
var arr = new Array('apple', 'orange');
puts(arr.length);
Line 1,722 ⟶ 1,822:
=={{header|Julia}}==
 
<syntaxhighlight lang=Julia"julia">
a = ["apple","orange"]
length(a)
</syntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">#("apple";"orange")</syntaxhighlight>
{{out}}
<pre>2</pre>
 
=={{header|Klingphix}}==
<syntaxhighlight lang=Klingphix"klingphix">include ..\Utilitys.tlhy
 
( "apple" "orange" ) len print
Line 1,738 ⟶ 1,843:
=={{header|Klong}}==
 
<syntaxhighlight lang=K"k">
#["apple" "orange"]
</syntaxhighlight>
Line 1,744 ⟶ 1,849:
=={{header|Kotlin}}==
 
<syntaxhighlight lang="scala">fun main(args: Array<String>) {
println(arrayOf("apple", "orange").size)
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{A.length {A.new 1 2 3}}
-> 3
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
&arr $= [apple, orange]
 
# Array length function
fn.println(fn.arrayLength(&arr))
 
# Length operator function
fn.println(fn.len(&arr))
 
# Length operator
fn.println(parser.op(@&arr))
</syntaxhighlight>
 
Line 1,758 ⟶ 1,877:
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|LDPL}}==
<syntaxhighlight lang="ldpl">data:
fruits is text list
len is number
 
procedure:
push "apple" to fruits
push "orange" to fruits
get length of fruits in len
display len lf
</syntaxhighlight>
{{out}}
<pre>
2
</pre>
 
=={{header|Liberty BASIC}}==
Line 1,770 ⟶ 1,905:
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}}
<syntaxhighlight lang="lb">
FruitList$(0)="apple" 'assign 2 cells of a list array
FruitList$(1)="orange"
Line 1,893 ⟶ 2,028:
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"]
append a "orange"
Line 1,905 ⟶ 2,040:
 
=={{header|Limbo}}==
<syntaxhighlight lang=Limbo"limbo">implement Command;
 
include "sys.m";
Line 1,923 ⟶ 2,058:
 
=={{header|Lingo}}==
<syntaxhighlight lang="lingo">fruits = ["apple", "orange"]
put fruits.count
-- 2</syntaxhighlight>
 
=={{header|Little}}==
<syntaxhighlight lang=C"c">string fruit[] = {"apples", "oranges"};
puts(length(fruit));</syntaxhighlight>
 
=={{header|LiveCode}}==
 
<syntaxhighlight lang=LiveCode"livecode">put "apple","orange" into fruit
split fruit using comma
answer the number of elements of fruit</syntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang=Lua"lua">-- For tables as simple arrays, use the # operator:
fruits = {"apple", "orange"}
print(#fruits)
Line 1,964 ⟶ 2,099:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
\\ A is a pointer to array
A=("Apple", "Orange")
Line 2,006 ⟶ 2,141:
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">a := Array(["apple", "orange"]);
numelems(a);</syntaxhighlight>
{{out}}
Line 2,015 ⟶ 2,150:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Length[{"apple", "orange"}]</syntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang=Matlab"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.
<syntaxhighlight lang=Matlab"matlab">numel({'apple', 'orange'; 'pear', 'banana'})</syntaxhighlight>
 
=={{header|Modula-3}}==
<syntaxhighlight lang="modula3">MODULE ArrayLength EXPORTS Main;
 
IMPORT IO;
Line 2,035 ⟶ 2,170:
 
=={{header|Mercury}}==
<syntaxhighlight lang=Mercury"mercury">:- module array_length.
:- interface.
 
Line 2,051 ⟶ 2,186:
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang="min">("apple" "orange") size print</syntaxhighlight>
{{out}}
<pre>
Line 2,058 ⟶ 2,193:
 
=={{header|MiniScript}}==
<syntaxhighlight lang=MiniScript"miniscript">
fruits = ["apple", "orange"]
print fruits.len
</syntaxhighlight>
=={{header|MiniZinc}}==
<syntaxhighlight lang=MiniZinc"minizinc">
array[int] of int: arr = [1,2,3];
var int: size = length(arr);
Line 2,073 ⟶ 2,208:
 
=={{header|Nanoquery}}==
<syntaxhighlight lang="nanoquery">fruit = array(2)
fruit[0] = "apple"
fruit[1] = "orange"
Line 2,081 ⟶ 2,216:
 
=={{header|Neko}}==
<syntaxhighlight lang="neko">var fruit = $array("apple", "orange");
 
$print($asize(fruit));</syntaxhighlight>
 
=={{header|NewLISP}}==
<syntaxhighlight lang="newlisp">(println (length '("apple" "orange")))
 
; Nehal-Singhal 2018-05-25
Line 2,092 ⟶ 2,227:
 
=={{header|NGS}}==
<syntaxhighlight lang=NGS"ngs">echo(len(['apple', 'orange']))
# same
echo(['apple', 'orange'].len())</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">let fruit = ["apple", "orange"]
echo "The length of the fruit array is ", len(fruit)</syntaxhighlight>
 
Line 2,104 ⟶ 2,239:
The length of the fruit array is 2
</pre>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
[apple orange] | length
</syntaxhighlight>
{{out}}
<pre>
2
</pre>
 
=={{header|Nutt}}==
<syntaxhighlight lang="Nutt">
module main
imports native.io.output.say
 
say(#{"apple","orange"})
 
end
</syntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<syntaxhighlight lang="oberon2">
MODULE ArrayLength;
IMPORT
Line 2,146 ⟶ 2,300:
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">
class Test {
function : Main(args : String[]) ~ Nil {
Line 2,155 ⟶ 2,309:
 
=={{header|OCaml}}==
<syntaxhighlight lang=OCaml"ocaml">
Array.length [|"apple"; "orange"|];;
</syntaxhighlight>
 
=={{header|Oforth}}==
<syntaxhighlight lang=Oforth"oforth">[ "apple", "orange" ] size</syntaxhighlight>
 
=={{header|Ol}}==
All of these methods are equivalent.
<syntaxhighlight lang="scheme">
(print (vector-length (vector "apple" "orange")))
(print (vector-length #("apple" "orange")))
Line 2,174 ⟶ 2,328:
=={{header|Onyx}}==
 
<syntaxhighlight lang="onyx">[`apple' `orange'] length # leaves 2 on top of the stack</syntaxhighlight>
 
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
/* REXX */
a = .array~of('apple','orange')
Line 2,192 ⟶ 2,346:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">array = ["apple", "orange"]
length(array) \\ == 2
#array \\ == 2</syntaxhighlight>
Line 2,204 ⟶ 2,358:
{{works with|Free Pascal|2.6.2}}
 
<syntaxhighlight lang=Pascal"pascal">
#!/usr/bin/instantfpc
//program ArrayLength;
Line 2,229 ⟶ 2,383:
</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="pascal">
// Array length. Nigel Galloway: September 2nd., 2022
var n:array of string:=('apple','orange');
begin
writeln(Length(n));
end.
</syntaxhighlight>
{{out}}
<pre>
2
</pre>
=={{header|Perl}}==
 
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
Line 2,257 ⟶ 2,423:
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
 
Line 2,271 ⟶ 2,437:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang=Phix"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: #0000FF;">?<span style="color: #7060A8;">length<span style="color: #0000FF;">(<span style="color: #000000;">fruits<span style="color: #0000FF;">)
Line 2,281 ⟶ 2,447:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">"apple" "orange" stklen tolist len print</syntaxhighlight>
With syntactic sugar
<syntaxhighlight lang=Phixmonti"phixmonti">include ..\Utilitys.pmt
( "apple" "orange" ) len print</syntaxhighlight>
{{out}}
Line 2,292 ⟶ 2,458:
=={{header|PHP}}==
 
<syntaxhighlight lang="php">print count(['apple', 'orange']); // Returns 2</syntaxhighlight>
 
=={{header|Picat}}==
<syntaxhighlight lang="text">main =>
L = ["apple", "orange"],
println(len(L))),
Line 2,310 ⟶ 2,476:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">: (length '(apple orange))
-> 2
:</syntaxhighlight>
 
=={{header|Pike}}==
<syntaxhighlight lang="pike">void main()
{
array fruit = ({ "apple", "orange" });
Line 2,325 ⟶ 2,491:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli"> p: Proc Options(main);
Dcl a(2) Char(6) Varying Init('apple','orange');
Put Edit('Array a has',(hbound(a)-lbound(a)+1),' elements.')
Line 2,337 ⟶ 2,503:
 
=={{header|Plorth}}==
<syntaxhighlight lang="plorth">["apple", "orange"] length println</syntaxhighlight>
 
=={{header|Pony}}==
<syntaxhighlight lang="pony">
actor Main
new create(env:Env)=>
Line 2,350 ⟶ 2,516:
 
=={{header|Potion}}==
<syntaxhighlight lang="potion">("apple", "orange") length print</syntaxhighlight>
 
=={{header|PowerShell}}==
<syntaxhighlight lang=PowerShell"powershell">
$Array = @( "Apple", "Orange" )
$Array.Count
Line 2,363 ⟶ 2,529:
 
=={{header|Processing}}==
<syntaxhighlight lang=Processing"processing">
String[] arr = {"apple", "orange"};
void setup(){
Line 2,375 ⟶ 2,541:
 
==={{header|Processing Python mode}}===
<syntaxhighlight lang="python">
arr = ['apple', 'orange'] # a list for an array
 
Line 2,388 ⟶ 2,554:
 
=={{header|Prolog}}==
<syntaxhighlight lang=Prolog"prolog">| ?- length(["apple", "orange"], X).
 
X = 2
Line 2,395 ⟶ 2,561:
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">
EnableExplicit
Define Dim fruit$(1); defines array with 2 elements at indices 0 and 1
Line 2,415 ⟶ 2,581:
 
An abbreviated form of the above, not printing to console/terminal
<syntaxhighlight lang=PureBasic"purebasic">
Dim fruit$(1); defines array with 2 elements at indices 0 and 1
fruit$(0) = "apple"
Line 2,427 ⟶ 2,593:
 
=={{header|Python}}==
<syntaxhighlight lang="python">>>> print(len(['apple', 'orange']))
2
>>> </syntaxhighlight>
Line 2,433 ⟶ 2,599:
=={{header|QB64}}==
 
<syntaxhighlight lang=QB64"qb64">
Dim max As Integer, Index As Integer
Randomize Timer
Line 2,447 ⟶ 2,613:
=={{header|Quackery}}==
 
<Langsyntaxhighlight Quackerylang="quackery"> $ "apples" $ "oranges" 2 pack size echo</syntaxhighlight>
 
{{out}}
Line 2,454 ⟶ 2,620:
 
=={{header|R}}==
<syntaxhighlight lang=R"r">
a <- c('apple','orange') # create a vector containing "apple" and "orange"
length(a)
Line 2,465 ⟶ 2,631:
=={{header|Racket}}==
{{trans|EchoLisp}}
<syntaxhighlight lang="racket">#lang racket/base
(length '("apple" "orange")) ;; list
(vector-length #("apple" "orange")) ;; vector</syntaxhighlight>
Line 2,478 ⟶ 2,644:
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"raku" line>
my @array = <apple orange>;
 
Line 2,489 ⟶ 2,655:
Watch out for infinite/lazy arrays though. You can't get the length of those.
 
<syntaxhighlight lang=perl6"raku" line>my @infinite = 1 .. Inf; # 1, 2, 3, 4, ...
 
say @infinite[5000]; # 5001
Line 2,496 ⟶ 2,662:
 
=={{header|Rapira}}==
<syntaxhighlight lang=Rapira"rapira">arr := <* "apple", "orange" *>
output: #arr</syntaxhighlight>
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
>> length? ["apples" "oranges"]
== 2
</syntaxhighlight>
 
=={{header|Red}}==
<syntaxhighlight lang=Red"red">length? ["apples" "oranges"]
== 2</syntaxhighlight>
 
=={{header|Relation}}==
<syntaxhighlight lang=Relation"relation">
relation fruit
insert "apples"
Line 2,520 ⟶ 2,692:
 
=={{header|ReScript}}==
<syntaxhighlight lang="rescript">let fruits = ["apple", "orange"]
 
Js.log(Js.Array.length(fruits))</syntaxhighlight>
 
<syntaxhighlight lang="html"><!DOCTYPE html>
<html>
<head>
Line 2,554 ⟶ 2,726:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/* REXX ----------------------------------------------
* The compond variable a. implements an array
* By convention, a.0 contains the number of elements
Line 2,578 ⟶ 2,750:
=={{header|Ring}}==
 
<syntaxhighlight lang="python">See len(['apple', 'orange']) # output = 2 </syntaxhighlight>
 
=={{header|Robotic}}==
Line 2,584 ⟶ 2,756:
 
Example 1:
<syntaxhighlight lang="robotic">
set "index" to 0
set "$array&index&" to "apple"
Line 2,593 ⟶ 2,765:
 
Example 2:
<syntaxhighlight lang="robotic">
set "index" to 0
set "local1" to random 1 to 99
Line 2,603 ⟶ 2,775:
* "Array length: ('index')"
</syntaxhighlight>
 
=={{header|RPL}}==
The <code>SIZE</code> instruction can be used for arrays (e.g. vectors) and lists. RPL arrays can only contain real or complex numbers, so we will use a list here.
{ "apple" "orange" } SIZE
{{out}}
<pre>
1: 2
</pre>
 
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby">puts ['apple', 'orange'].length # or .size</syntaxhighlight>
 
=={{header|Rust}}==
Line 2,612 ⟶ 2,792:
By default arrays are immutable in rust.
 
<syntaxhighlight lang="rust">
fn main() {
let array = ["foo", "bar", "baz", "biff"];
Line 2,618 ⟶ 2,798:
}
</syntaxhighlight>
 
=={{header|S-BASIC}}==
Finding the size of an S-BASIC array at run-time is convoluted, to say the least, but it can be done. (It would also generally be pointless, since the size of an array is fixed - and thus presumably known - at compile time.) Each array has an associated data structure (referred to in the documentation as "SPEC") containing information such as the number of dimensions, the size of an array element, the size of each dimension, and so on. The address of the SPEC for an array can be obtained using the LOCATION statement. For a single-dimension array, the number of elements will be found five bytes into the structure, at a point described in the documentation as the "dope vector".
<syntaxhighlight lang = "BASIC">
dim string animals(2) rem here is our array
var array_struct_address = integer
based array_size = integer
 
animals(1) = "ardvark"
animals(2) = "bison"
 
location spec array_struct_address = animals
base array_size at array_struct_address + 5
 
print "Size of array ="; array_size
 
end
</syntaxhighlight>
{{out}}
<pre>
Size of array = 2
</pre>
 
=={{header|Scala}}==
 
<syntaxhighlight lang="scala">
println(Array("apple", "orange").length)
</syntaxhighlight>
Line 2,629 ⟶ 2,831:
Using Scheme's vector type as an equivalent to an array:
 
<syntaxhighlight lang="scheme">
(display (vector-length #("apple" "orange")))
</syntaxhighlight>
Line 2,636 ⟶ 2,838:
The function [http://seed7.sourceforge.net/libraries/array.htm#length(in_arrayType) length]
determines the length of an array.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const array string: anArray is [] ("apple", "orange");
Line 2,646 ⟶ 2,848:
 
=={{header|SenseTalk}}==
<syntaxhighlight lang="sensetalk">put ("apple", "orange", "pear", "banana", "aubergine") into fruits
put the number of items in fruits</syntaxhighlight>
 
=={{header|Shen}}==
<syntaxhighlight lang="shen">
\\ Using a vector
\\ @v creates the vector
Line 2,664 ⟶ 2,866:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var arr = ['apple', 'orange'];
say arr.len; #=> 2
say arr.end; #=> 1 (zero based)</syntaxhighlight>
 
=={{header|Simula}}==
<syntaxhighlight lang="simula">COMMENT ARRAY-LENGTH;
BEGIN
 
Line 2,690 ⟶ 2,892:
2
</pre>
 
=={{heaer|Slope}}==
<syntaxhighlight lang="slope">(length ["apple" "orange"])</syntaxhighlight>
 
=={{header|SmallBASIC}}==
<syntaxhighlight lang="SmallBASIC">
A = ["apple", "orange"]
print len(A)
</syntaxhighlight>
 
=={{header|Smalltalk}}==
<syntaxhighlight lang="smalltalk">
a := #('apple' 'orange').
a size</syntaxhighlight>
 
=={{header|SNOBOL4}}==
<syntaxhighlight lang=SNOBOL4"snobol4"> ar = ARRAY('2,2')
ar<1,1> = 'apple'
ar<1,2> = 'first'
Line 2,707 ⟶ 2,918:
 
=={{header|SPL}}==
<syntaxhighlight lang="spl">a = ["apple","orange"]
#.output("Number of elements in array: ",#.size(a,1))</syntaxhighlight>
{{out}}
Line 2,716 ⟶ 2,927:
=={{header|SQL}}==
 
<syntaxhighlight lang="sql">SELECT COUNT() FROM (VALUES ('apple'),('orange'));</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">let
val a = Array.fromList ["apple", "orange"]
in
Line 2,729 ⟶ 2,940:
 
=== Dimensions of a dataset ===
<syntaxhighlight lang="stata">clear
input str10 fruit
apple
Line 2,742 ⟶ 2,953:
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 `: word count `fruits''</syntaxhighlight>
Line 2,749 ⟶ 2,960:
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")
rows(a)
Line 2,758 ⟶ 2,969:
=={{header|Swift}}==
 
<syntaxhighlight lang=Swift"swift">let fruits = ["apple", "orange"] // Declare constant array literal
let fruitsCount = fruits.count // Declare constant array length (count)
 
Line 2,765 ⟶ 2,976:
 
=={{header|Symsyn}}==
<syntaxhighlight lang=Symsyn"symsyn">
| Symsyn does not support Array of Strings
| The following code for an Array of Integers
Line 2,782 ⟶ 2,993:
 
=={{header|Tailspin}}==
<syntaxhighlight lang="tailspin">
['apple', 'orange'] -> $::length -> !OUT::write
</syntaxhighlight>
Line 2,791 ⟶ 3,002:
<!-- http://ideone.com/uotEvm -->
 
<syntaxhighlight lang="tcl">;# not recommended:
set mylistA {apple orange} ;# actually a string
set mylistA "Apple Orange" ;# same - this works only for simple cases
Line 2,813 ⟶ 3,024:
{{works with|TI-83 2.55MP}}
Use function <code>dim()</code>.
<syntaxhighlight lang="ti83b">{1,3,–5,4,–2,–1}→L1
dim(L1)</syntaxhighlight>
{{out}}
Line 2,821 ⟶ 3,032:
 
=={{header|Transd}}==
<syntaxhighlight lang="scheme">#lang transd
 
MainModule : {
Line 2,837 ⟶ 3,048:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="bash">#!/bin/bash
array=("orange" "apple")
echo "${#array[@]}"</syntaxhighlight>
Line 2,855 ⟶ 3,066:
 
=={{header|Vala}}==
<syntaxhighlight lang="vala">void main() {
string[] fruit = {"apple", "orange"};
stdout.printf("%d\n", fruit.length);
Line 2,862 ⟶ 3,073:
Note that any of the following array declarations could be used:
 
<syntaxhighlight lang="vala">
var fruit = new string[] { "apple", "orange" };
string[] fruit = new string[] { "apple", "orange" };
Line 2,870 ⟶ 3,081:
A shorter variant could also have been used:
 
<syntaxhighlight lang="vala">
void main() {
stdout.printf("%d\n", new string[] {"apples", "orange"}.length);
Line 2,878 ⟶ 3,089:
=={{header|VBA}}==
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:
<syntaxhighlight lang="vb">Dim funkyArray(7 to 8) As String
 
Public Function SizeOfArray(ar As Variant) As Long
Line 2,895 ⟶ 3,106:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">arr = Array("apple","orange")
WScript.StdOut.WriteLine UBound(arr) - LBound(arr) + 1 </syntaxhighlight>
 
Line 2,904 ⟶ 3,115:
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">
' declared in a module
Public Function LengthOfArray(ByRef arr As Variant) As Long
Line 2,935 ⟶ 3,146:
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|9.0+}}
<syntaxhighlight lang="vbnet">Module ArrayLength
 
Sub Main()
Line 2,950 ⟶ 3,161:
</pre>
 
=={{header|V (Vlang)}}==
A '''len''' property is maintained for all V (Vlang) arrays.
<syntaxhighlight lang="go">// V, array length
// Tectonics: v run array-length.v
module main
Line 2,966 ⟶ 3,177:
 
=={{header|WDTE}}==
<syntaxhighlight lang="wdte">let io => import 'io';
let a => ['apple'; 'orange'];
len a -- io.writeln io.stdout;</syntaxhighlight>
 
=={{header|Wren}}==
<syntaxhighlight lang=ecmascript"wren">var arr = ["apple", "orange"]
System.print(arr.count)</syntaxhighlight>
 
Line 2,980 ⟶ 3,191:
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">(vector-length #("apple" "orange"))</syntaxhighlight>
 
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">
dim fruta$(3)
read fruta$(1), fruta$(2), fruta$(3)
Line 3,004 ⟶ 3,215:
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
call GetArrayLength_WordData_NullTerminated
Line 3,057 ⟶ 3,268:
=={{header|zkl}}==
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) </syntaxhighlight>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() !void {
Line 3,072 ⟶ 3,283:
 
=={{header|Zoea}}==
<syntaxhighlight lang=Zoea"zoea">
program: array_length
input: [a,b,c]
Line 3,082 ⟶ 3,293:
 
=={{header|zonnon}}==
<syntaxhighlight lang="zonnon">
module AryLength;
type
62

edits