Difference between revisions of "Compute the number of elements in an array"

From CodeCodex

(Add Seed7 example)
Line 98: Line 98:
 
(length my-list)
 
(length my-list)
 
(vector-length my-vector)
 
(vector-length my-vector)
 +
</highlightsyntax>
 +
===Seed7===
 +
<highlightsyntax>
 +
length(myArray)
 
</highlightsyntax>
 
</highlightsyntax>
  
Line 111: Line 115:
 
[[Category:Perl]]
 
[[Category:Perl]]
 
[[Category:Python]]
 
[[Category:Python]]
[[Category:Scheme]]
 
 
[[Category:Ruby]]
 
[[Category:Ruby]]
 +
[[Category:Scheme]]
 +
[[Category:Seed7]]

Revision as of 11:54, 2 November 2010

Implementations

C/C++

There is a convenient compile-time trick to get the length of a statically-allocated array, within the scope of where it is declared. (This works because the compiler can just look up to where it is declared, and read the constant length, and hard-code the number.) The trick to computing the number of elements in an array is to divide the total size of an array by the size of a given element. For example, if the total size of the array is 80 bytes and each element takes up 8 bytes, there must be 10 elements in the array. <highlightsyntax language="c"> sizeof(array) / sizeof(array[0]); </highlightsyntax> Ideally, you would put this into a macro as follows: <highlightsyntax language="c">

  1. define NumElm(array) (sizeof(array) / sizeof((array)[0]))

</highlightsyntax> Note: this method will not work for arrays that are zero- or null-terminated (including strings).

Note: This method will not be able to "find" the length of an array given just the pointer to its first element, because the "length" is not stored anywhere at runtime. Arrays cannot be passed or returned from functions; only the pointer to its first element can. So for example, a function which takes in an array and which needs to know its length will need to take in both the pointer to the first argument and its length as separate arguments. Similarly, dynamically-allocated arrays, which exist only as pointers at compile-time, will need to have their lengths kept track of separately, if you need to know its length.

In C99, there are variable-length arrays (VLAs). The above trick should also work to find the length of VLAs. However, passing VLAs to functions also requires their lengths to be passed in separately or be known.

C++

In C++, people often use STL data structures, like std::vector, instead of arrays, because of their better object-oriented interface. <highlightsyntax language="cpp"> std::vector myVector; myVector.size(); </highlightsyntax>

Go

<highlightsyntax> // getting the length of an array is silly, because the length is part of the array's static type myArray := [3]int{1, 2, 3} fmt.Println(len(myArray)) // prints 3 // getting the length of a slice mySlice := []int{1, 2, 3} fmt.Println(len(mySlice)) // prints 3 </highlightsyntax>

Haskell

Haskell often uses lists instead of arrays: <highlightsyntax language="haskell"> length myList </highlightsyntax> If you must use arrays, Haskell's arrays can be bounded by any type that implements Ix, and are not necessarily integers. You can try to figure out the length from the bounds. <highlightsyntax language="haskell"> low, high = bounds myArray </highlightsyntax>

Java

<highlightsyntax language="java122"> int[] myArray; myArray.length; </highlightsyntax> for Collection's: <highlightsyntax language="java122"> Collection myCollection; myCollection.size(); </highlightsyntax>

JavaScript

<highlightsyntax language="javascript"> var myArray = new Array(5); myArray.length; </highlightsyntax>

MivaScript

<highlightsyntax language="php"> <mvassign name="l.arrays" index="1" value="{'one array'}"> <mvassign name="l.arrays" index="2" value="{'two array'}"> <mvassign name="l.length" value="{miva_array_max(l.arrays)}"> </highlightsyntax>

OCaml

<highlightsyntax language="ocaml"> Array.length myArray;; List.length myList;; </highlightsyntax>

Perl

<highlightsyntax language="perl"> @myArray = (); scalar(@myArray); </highlightsyntax>

PHP

<highlightsyntax language="php"> $myArray = array(); count($myArray); </highlightsyntax>

Python

<highlightsyntax language="python"> mySeq = [] len(mySeq) </highlightsyntax>

Ruby

<highlightsyntax language="python"> lst = [1,2,3,4,5] lst.size #returns 5 </highlightsyntax>

Scheme

<highlightsyntax language="scheme"> (length my-list) (vector-length my-vector) </highlightsyntax>

Seed7

<highlightsyntax> length(myArray) </highlightsyntax>