Array length: Difference between revisions

→‎Forth: use forth highlighting
(→‎Forth: use forth highlighting)
(18 intermediate revisions by 15 users not shown)
Line 167:
=={{header|Ada}}==
 
<syntaxhighlight lang="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}}==
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}}===
Line 598 ⟶ 614:
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">DIM fruta$(2)
DIM fruta$(2)
READ fruta$(1), fruta$(2)
DATA "apple", "orange"
Line 606 ⟶ 621:
 
PRINT "La longitud del array fruta$ es" ; tamano
END</syntaxhighlight>
END
</syntaxhighlight>
 
{{out}}
<pre> La longitud del array fruta$ es 2 </pre>
Line 615 ⟶ 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}}==
Line 703 ⟶ 733:
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}}==
Line 1,320 ⟶ 1,356:
</syntaxhighlight>
 
=={{header|Dyalectdt}}==
<syntaxhighlight lang="dt">[ "apple" "orange" ] len</syntaxhighlight>
 
=={{header|Dyalect}}==
<syntaxhighlight lang="dyalect">var xs = ["apple", "orange"]
print(xs.Length())</syntaxhighlight>
Line 1,367 ⟶ 1,405:
|> Array.fromList
|> Array.length
|> BasicsString.toStringfromInt
|> Html.text
</syntaxhighlight>
Line 1,423 ⟶ 1,461:
{ "apple" "orange" } length
</syntaxhighlight>
 
=={{header|Fennel}}==
<syntaxhighlight lang="fennel">(length [:apple :orange])</syntaxhighlight>
 
=={{header|Forth}}==
Line 1,430 ⟶ 1,471:
The code is commented to explain what is going on for those unfamiliar with Forth.
 
<syntaxhighlight lang="textforth">: STRING, ( caddr len -- ) \ Allocate space & compile string into memory
HERE OVER CHAR+ ALLOT PLACE ;
 
Line 1,568 ⟶ 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'''
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.
 
The cardinality expression reduces to the number of subexpressions the given expression has, including if the expressions is a list:
In '''[https://formulae.org/?example=Array_length this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Array length 01.png]]
 
[[File:Fōrmulæ - Array length 02.png]]
 
=={{header|Gambas}}==
Line 1,656 ⟶ 1,701:
=={{header|Idris}}==
<syntaxhighlight lang="idris">length ["apple", "orange"]</syntaxhighlight>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(len ['apple' 'orange'])</syntaxhighlight>
 
=={{header|J}}==
Line 1,778 ⟶ 1,826:
length(a)
</syntaxhighlight>
 
=={{header|K}}==
<syntaxhighlight lang="k">#("apple";"orange")</syntaxhighlight>
{{out}}
<pre>2</pre>
 
=={{header|Klingphix}}==
Line 1,825 ⟶ 1,878:
 
<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 2,170 ⟶ 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}}==
Line 2,710 ⟶ 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}}==
Line 2,785 ⟶ 2,895:
=={{heaer|Slope}}==
<syntaxhighlight lang="slope">(length ["apple" "orange"])</syntaxhighlight>
 
=={{header|SmallBASIC}}==
<syntaxhighlight lang="SmallBASIC">
A = ["apple", "orange"]
print len(A)
</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 3,066 ⟶ 3,182:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var arr = ["apple", "orange"]
System.print(arr.count)</syntaxhighlight>
 
62

edits