Creating an Array: Difference between revisions
Content added Content deleted
(tag obsolete (Arrays) and remove {header}s) |
|||
Line 1: | Line 1: | ||
{{task|Basic language learning}} |
{{task|Basic language learning}} |
||
'''This task is obsolete. Please do not add new code, and merge existing code to the [[Arrays]] task.''' |
|||
This task is about numerically-indexed arrays. For '''hashes''' or '''associative arrays''', please see [[Creating an Associative Array]]. |
This task is about numerically-indexed arrays. For '''hashes''' or '''associative arrays''', please see [[Creating an Associative Array]]. |
||
Line 5: | Line 8: | ||
In addition, demonstrate how to initialize an array variable with data. |
In addition, demonstrate how to initialize an array variable with data. |
||
== |
==[[ActionScript]]== |
||
<lang actionscript> |
<lang actionscript> |
||
// ActionScript arrays are zero-based |
// ActionScript arrays are zero-based |
||
Line 19: | Line 22: | ||
</lang> |
</lang> |
||
== |
==[[Ada]]== |
||
'''Compiler:''' [[GCC]] 4.1.2 |
'''Compiler:''' [[GCC]] 4.1.2 |
||
Line 37: | Line 40: | ||
</lang> |
</lang> |
||
== |
==[[ALGOL 68]]== |
||
As with all |
As with all ALGOL 68 declarations the programmer has the choice of using the full declaration syntax, or using [[syntactic sugar]] - c.f. "sugared" variables below. |
||
Example of array of 10 integer types: |
Example of array of 10 integer types: |
||
Line 118: | Line 121: | ||
</lang> |
</lang> |
||
== |
==[[AmigaE]]== |
||
<lang amigae>DEF ai[100] : ARRAY OF CHAR, -> static |
<lang amigae>DEF ai[100] : ARRAY OF CHAR, -> static |
||
da: PTR TO CHAR, |
da: PTR TO CHAR, |
||
Line 137: | Line 140: | ||
ENDPROC</lang> |
ENDPROC</lang> |
||
== |
==[[AppleScript]]== |
||
AppleScript arrays are called lists: |
AppleScript arrays are called lists: |
||
<lang applescript> set empty to {} |
<lang applescript> set empty to {} |
||
Line 145: | Line 148: | ||
<lang applescript> set any to {1, "foo", 2.57, missing value, ints}</lang> |
<lang applescript> set any to {1, "foo", 2.57, missing value, ints}</lang> |
||
== |
==[[AutoHotkey]]== |
||
AutoHotkey does not have arrays yet. |
AutoHotkey does not have arrays yet. |
||
However, variables can be set to arbitrary size and pointer operations can be used, simulating arrays. Just without the syntactic sugar of '[]'. |
However, variables can be set to arbitrary size and pointer operations can be used, simulating arrays. Just without the syntactic sugar of '[]'. |
||
Line 154: | Line 157: | ||
</lang> |
</lang> |
||
== |
==[[AWK]]== |
||
"Numeric" arrays coincide with hashes in awk. Indices begin at 1. The <tt>split</tt> function can be used to initialize an array from a string with elements separated by a field separator (space by default) |
"Numeric" arrays coincide with hashes in awk. Indices begin at 1. The <tt>split</tt> function can be used to initialize an array from a string with elements separated by a field separator (space by default) |
||
<lang awk>BEGIN { |
<lang awk>BEGIN { |
||
Line 164: | Line 167: | ||
capital["Italy"]="Rome"</lang> |
capital["Italy"]="Rome"</lang> |
||
== |
==[[BASIC]]== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
Line 201: | Line 204: | ||
<lang freebasic> Dim myArray(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}} </lang> |
<lang freebasic> Dim myArray(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}} </lang> |
||
== |
==[[C]]== |
||
Dynamic |
Dynamic |
||
<lang c> #include <stdlib.h> /* for malloc */ |
<lang c> #include <stdlib.h> /* for malloc */ |
||
Line 220: | Line 223: | ||
<lang c> int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */</lang> |
<lang c> int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */</lang> |
||
== |
==[[C++]]== |
||
Using dynamically-allocated (i.e. [[Heap]]) memory: |
Using dynamically-allocated (i.e. [[Heap]]) memory: |
||
<lang cpp> const int n = 10; |
<lang cpp> const int n = 10; |
||
Line 260: | Line 263: | ||
myArray5.Add(2);</lang> |
myArray5.Add(2);</lang> |
||
== |
==[[C sharp|C#]]== |
||
Example of array of 10 int types: |
Example of array of 10 int types: |
||
Line 287: | Line 290: | ||
<lang csharp> string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };</lang> |
<lang csharp> string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };</lang> |
||
== |
==[[Clean]]== |
||
Array denotations are overloaded in Clean, therefore we explicitly specify the types. There are lazy, strict, and unboxed array. |
Array denotations are overloaded in Clean, therefore we explicitly specify the types. There are lazy, strict, and unboxed array. |
||
===Lazy array=== |
===Lazy array=== |
||
Line 308: | Line 311: | ||
array = {x \\ x <- ['a' .. 'z']}</lang> |
array = {x \\ x <- ['a' .. 'z']}</lang> |
||
== |
==[[ColdFusion]]== |
||
Creates a one-dimensional Array |
Creates a one-dimensional Array |
||
<lang coldfusion><cfset arr1 = ArrayNew(1)></lang> |
<lang coldfusion><cfset arr1 = ArrayNew(1)></lang> |
||
Line 317: | Line 320: | ||
''ColdFusion Arrays are '''NOT''' zero-based, they begin at index '''1''''' |
''ColdFusion Arrays are '''NOT''' zero-based, they begin at index '''1''''' |
||
== |
==[[Common Lisp]]== |
||
Creates a one-dimensional array of length 10. The initial contents are undefined. |
Creates a one-dimensional array of length 10. The initial contents are undefined. |
||
<lang lisp>(make-array 10)</lang> |
<lang lisp>(make-array 10)</lang> |
||
Line 328: | Line 331: | ||
(make-array 4 :element-type 'integer :initial-contents '(1 2 3 4) :adjustable t)</lang> |
(make-array 4 :element-type 'integer :initial-contents '(1 2 3 4) :adjustable t)</lang> |
||
== |
==[[D]]== |
||
{{works with|DMD}} |
{{works with|DMD}} |
||
Line 338: | Line 341: | ||
int[5] = [0,1,2,3,4];</lang> |
int[5] = [0,1,2,3,4];</lang> |
||
== |
==[[Delphi]]== |
||
'''Defining a variable-length array''' |
'''Defining a variable-length array''' |
||
<lang delphi>var |
<lang delphi>var |
||
Line 356: | Line 359: | ||
Delphi dynamic arrays have base 0 index. |
Delphi dynamic arrays have base 0 index. |
||
== |
==[[E]]== |
||
In accordance with its guarantees of determinism, you can never have an ''uninitialized'' array in E. |
In accordance with its guarantees of determinism, you can never have an ''uninitialized'' array in E. |
||
Line 368: | Line 371: | ||
If you want an array of some mutable objects, see [[N distinct objects#E]]. |
If you want an array of some mutable objects, see [[N distinct objects#E]]. |
||
== |
==[[Forth]]== |
||
Forth has a variety of ways to allocate arrays of data, though it has no built-in array handling words, favoring pointer manipulation. |
Forth has a variety of ways to allocate arrays of data, though it has no built-in array handling words, favoring pointer manipulation. |
||
Line 389: | Line 392: | ||
0 to MyArray</lang> |
0 to MyArray</lang> |
||
== |
==[[Fortran]]== |
||
In ANSI FORTRAN 77 or later, this is a default-indexing array declaration: |
In ANSI FORTRAN 77 or later, this is a default-indexing array declaration: |
||
Line 478: | Line 481: | ||
13, 14, 15, 16 /), (/ 4, 4 /) ) )</lang> |
13, 14, 15, 16 /), (/ 4, 4 /) ) )</lang> |
||
== |
==[[Groovy]]== |
||
In Groovy "arrays" are synonymous with "lists", and are thus not of a fixed size. Ranges are also a type of list. |
In Groovy "arrays" are synonymous with "lists", and are thus not of a fixed size. Ranges are also a type of list. |
||
<lang groovy> |
<lang groovy> |
||
Line 487: | Line 490: | ||
</lang> |
</lang> |
||
== |
==[[Haskell]]== |
||
Arrays are initialized either by a list of index-value-pairs or by a list of values: |
Arrays are initialized either by a list of index-value-pairs or by a list of values: |
||
Line 501: | Line 504: | ||
There are several flavours of arrays in Haskell, but creation looks very similar for the others. |
There are several flavours of arrays in Haskell, but creation looks very similar for the others. |
||
== |
==[[IDL]]== |
||
IDL doesn't really distinguish between scalars and arrays - the same operations that can create the one can ''usually'' create the other as well. |
IDL doesn't really distinguish between scalars and arrays - the same operations that can create the one can ''usually'' create the other as well. |
||
Line 515: | Line 518: | ||
print,a^2 |
print,a^2 |
||
9 25 64 49</lang> |
9 25 64 49</lang> |
||
== |
==[[J]]== |
||
"Creating an array" is topic seldom discussed in J as ''every'' verb takes array argument(s) and returns an array result (that is, ''everything'' creates an array). For example: |
"Creating an array" is topic seldom discussed in J as ''every'' verb takes array argument(s) and returns an array result (that is, ''everything'' creates an array). For example: |
||
Line 540: | Line 543: | ||
Both the <tt>a+b</tt>and the <tt>|."1 c</tt> expressions create arrays. |
Both the <tt>a+b</tt>and the <tt>|."1 c</tt> expressions create arrays. |
||
== |
==[[Java]]== |
||
For example for an array of 10 int values: |
For example for an array of 10 int values: |
||
<lang java>int[] intArray = new int[10];</lang> |
<lang java>int[] intArray = new int[10];</lang> |
||
Line 548: | Line 551: | ||
<lang java>Object[] obs = new Object[size];</lang> |
<lang java>Object[] obs = new Object[size];</lang> |
||
== |
==[[JavaScript]]== |
||
<lang javascript> var myArray = new Array(); |
<lang javascript> var myArray = new Array(); |
||
var myArray1 = new Array(5); // gotcha: preallocated array with five empty elements, not [ 5 ]. |
var myArray1 = new Array(5); // gotcha: preallocated array with five empty elements, not [ 5 ]. |
||
Line 554: | Line 557: | ||
var myArray3 = ["Item1", "Item2"];</lang> |
var myArray3 = ["Item1", "Item2"];</lang> |
||
== |
==[[Logo]]== |
||
<lang logo>array 5 ; default origin is 1 |
<lang logo>array 5 ; default origin is 1 |
||
(array 5 0) ; custom origin |
(array 5 0) ; custom origin |
||
{1 2 3 4 5} ; array literal</lang> |
{1 2 3 4 5} ; array literal</lang> |
||
== |
==[[LSE64]]== |
||
<lang lse64>10 myArray :array</lang> |
<lang lse64>10 myArray :array</lang> |
||
== |
==[[Mathematica]]== |
||
Creating an array is simply done with Set (=). An array can contain anything, and can have different Heads. A formule like 2+3x+x^2 is an array: Plus[2,Times[3,x],Times[2,Power[x,2]]]. Each list has different heads (Plus,Times, Power). Arrays are made as follows: |
Creating an array is simply done with Set (=). An array can contain anything, and can have different Heads. A formule like 2+3x+x^2 is an array: Plus[2,Times[3,x],Times[2,Power[x,2]]]. Each list has different heads (Plus,Times, Power). Arrays are made as follows: |
||
<lang Mathematica> |
<lang Mathematica> |
||
Line 572: | Line 575: | ||
Note that arrays can have any dimensions, and don't have to be rectangular (every (sub)element can have different length). |
Note that arrays can have any dimensions, and don't have to be rectangular (every (sub)element can have different length). |
||
== |
==[[MAXScript]]== |
||
{{works with|3D Studio Max|8}} |
{{works with|3D Studio Max|8}} |
||
Line 587: | Line 590: | ||
for i in 1 to 100 do arr[i] = 0</pre> |
for i in 1 to 100 do arr[i] = 0</pre> |
||
== |
==[[mIRC Scripting Language]]== |
||
{{works with|mIRC Script Editor}} |
{{works with|mIRC Script Editor}} |
||
Line 593: | Line 596: | ||
<lang mirc>alias creatmearray { .echo -a $array_create(MyArray, 5, 10) }</lang> |
<lang mirc>alias creatmearray { .echo -a $array_create(MyArray, 5, 10) }</lang> |
||
== |
==[[Modula-3]]== |
||
Modula-3 arrays include their range in the declaration. |
Modula-3 arrays include their range in the declaration. |
||
<lang modula3>VAR a: ARRAY [1..10] OF INTEGER;</lang> |
<lang modula3>VAR a: ARRAY [1..10] OF INTEGER;</lang> |
||
Line 608: | Line 611: | ||
Which creates a 2 dimensional array of 10 integers each. |
Which creates a 2 dimensional array of 10 integers each. |
||
== |
==[[Nial]]== |
||
To create an array of 5 elements with values from 1 to 5 |
To create an array of 5 elements with values from 1 to 5 |
||
<lang nial>| count 5 |
<lang nial>| count 5 |
||
Line 618: | Line 621: | ||
<lang nial>| newarr := [2 4 6]</lang> |
<lang nial>| newarr := [2 4 6]</lang> |
||
== |
==[[Oberon-2]]== |
||
Create an array of 10 integers. Initial values are undefined. All arrays are zero-indexed. |
Create an array of 10 integers. Initial values are undefined. All arrays are zero-indexed. |
||
<lang oberon2>VAR a: ARRAY 10 OF INTEGER;</lang> |
<lang oberon2>VAR a: ARRAY 10 OF INTEGER;</lang> |
||
Line 634: | Line 637: | ||
creates a 3 dimensional array of 10 integers each. |
creates a 3 dimensional array of 10 integers each. |
||
== |
==[[OCaml]]== |
||
Using an array literal: |
Using an array literal: |
||
Line 653: | Line 656: | ||
let array = Array.init 5 callback</lang> |
let array = Array.init 5 callback</lang> |
||
== |
==[[Pascal]]== |
||
'''Using a fixed length array as global or stack variable''' |
'''Using a fixed length array as global or stack variable''' |
||
<lang pascal>var |
<lang pascal>var |
||
Line 698: | Line 701: | ||
Note that also the lower index may be given as dynamic argument. |
Note that also the lower index may be given as dynamic argument. |
||
== |
==[[Perl]]== |
||
{{works with|Perl|5}} |
{{works with|Perl|5}} |
||
<lang perl> my @empty; |
<lang perl> my @empty; |
||
Line 733: | Line 736: | ||
</lang> |
</lang> |
||
== |
==[[PHP]]== |
||
For a single dimension array with 10 elements: |
For a single dimension array with 10 elements: |
||
Line 767: | Line 770: | ||
?></lang> |
?></lang> |
||
== |
==[[Pike]]== |
||
For a single dimension int array: |
For a single dimension int array: |
||
<lang pike> array(int) x = ({ 1, 2, 3 });</lang> |
<lang pike> array(int) x = ({ 1, 2, 3 });</lang> |
||
Line 779: | Line 782: | ||
Note that inner arrays can be of different sizes, as are simply values of the outer array. |
Note that inner arrays can be of different sizes, as are simply values of the outer array. |
||
== |
==[[Pop11]]== |
||
Pop11 distinguishes between vectors and arrays. Vectors are one dimensional and the lowest index is 1. There is special shorthand syntax to create vectors: |
Pop11 distinguishes between vectors and arrays. Vectors are one dimensional and the lowest index is 1. There is special shorthand syntax to create vectors: |
||
Line 796: | Line 799: | ||
vars a1 = newarray([2 5 -1 1], 0);</lang> |
vars a1 = newarray([2 5 -1 1], 0);</lang> |
||
== |
==[[Python]]== |
||
List are mutable arrays. You can put anything into a list, including other lists. |
List are mutable arrays. You can put anything into a list, including other lists. |
||
Line 835: | Line 838: | ||
The [http://docs.python.org/library/ctypes.html#arrays ctypes module] can construct specialised arrays for interfacing with external C functions. The [http://docs.python.org/library/struct.html struct module] allows a string of characters to be interpreted as an array of values of C types such as 32 bit ints, float, etc. |
The [http://docs.python.org/library/ctypes.html#arrays ctypes module] can construct specialised arrays for interfacing with external C functions. The [http://docs.python.org/library/struct.html struct module] allows a string of characters to be interpreted as an array of values of C types such as 32 bit ints, float, etc. |
||
== |
==[[R]]== |
||
<lang R>a <- vector("numeric", 10) # a is a vector (array) of 10 numbers |
<lang R>a <- vector("numeric", 10) # a is a vector (array) of 10 numbers |
||
b <- vector("logical", 10) # b is an array of logical (boolean) values |
b <- vector("logical", 10) # b is an array of logical (boolean) values |
||
Line 854: | Line 857: | ||
== |
==[[Raven]]== |
||
<lang raven>[ 1 2 3.14 'a' 'b' 'c' ] as a_list |
<lang raven>[ 1 2 3.14 'a' 'b' 'c' ] as a_list |
||
a_list print</lang> |
a_list print</lang> |
||
Line 866: | Line 869: | ||
5 => "c" |
5 => "c" |
||
== |
==[[Ruby]]== |
||
This is the most basic way to create an empty one-dimensional array in Ruby: |
This is the most basic way to create an empty one-dimensional array in Ruby: |
||
<lang ruby> my_array = Array.new |
<lang ruby> my_array = Array.new |
||
Line 910: | Line 913: | ||
array = Array.new(4) {|i| Array.new(6,i)}</lang> |
array = Array.new(4) {|i| Array.new(6,i)}</lang> |
||
== |
==[[Scala]]== |
||
<lang scala> val array = new Array[int](10) // a 10 element array |
<lang scala> val array = new Array[int](10) // a 10 element array |
||
val stringArray = new Array[String](20) // a 20 element string array |
val stringArray = new Array[String](20) // a 20 element string array |
||
Line 917: | Line 920: | ||
(1 :: 2 :: 3 :: 4 :: 5 :: Nil).toArray</lang> |
(1 :: 2 :: 3 :: 4 :: 5 :: Nil).toArray</lang> |
||
== |
==[[Scheme]]== |
||
Lists are more often used in Scheme than vectors. |
Lists are more often used in Scheme than vectors. |
||
Line 931: | Line 934: | ||
<lang scheme> (make-vector 5 0)</lang> |
<lang scheme> (make-vector 5 0)</lang> |
||
== |
==[[Script3D]]== |
||
Script3D has dynamic arrays allowing to store any datatype and a special type for storing vectors of floating point values, typically used in 3D applications. |
Script3D has dynamic arrays allowing to store any datatype and a special type for storing vectors of floating point values, typically used in 3D applications. |
||
Line 940: | Line 943: | ||
var myVector = Vector(2000); // 2000 elements vector of floats</lang> |
var myVector = Vector(2000); // 2000 elements vector of floats</lang> |
||
== |
==[[Slate]]== |
||
<lang slate> |
<lang slate> |
||
define: #array -> (Array newSize: 20). |
define: #array -> (Array newSize: 20). |
||
Line 949: | Line 952: | ||
</lang> |
</lang> |
||
== |
==[[Smalltalk]]== |
||
<lang smalltalk>|array| |
<lang smalltalk>|array| |
||
"creates an array that holds up to 20 elements" |
"creates an array that holds up to 20 elements" |
||
Line 963: | Line 966: | ||
array at: 2 put: 'orange'.</lang> |
array at: 2 put: 'orange'.</lang> |
||
== |
==[[Standard ML]]== |
||
Converting from a list: |
Converting from a list: |
||
Line 982: | Line 985: | ||
<lang sml> val vector = #[1,2,3,4,5]</lang> |
<lang sml> val vector = #[1,2,3,4,5]</lang> |
||
== |
==[[Tcl]]== |
||
Tcl uses the <tt>list</tt> for what many other languages call "array". A list is an ordered, numerically indexable (zero based) collection of values in a single variable. Each list entry itself can be a list. |
Tcl uses the <tt>list</tt> for what many other languages call "array". A list is an ordered, numerically indexable (zero based) collection of values in a single variable. Each list entry itself can be a list. |
||
Line 1,002: | Line 1,005: | ||
this creates an array with the name <tt>b</tt> with two elements. The keys of the elements are "foo" and "bar" and the values are <tt>b(foo) == 12</tt> and <tt>b(bar) == hello</tt>. |
this creates an array with the name <tt>b</tt> with two elements. The keys of the elements are "foo" and "bar" and the values are <tt>b(foo) == 12</tt> and <tt>b(bar) == hello</tt>. |
||
== |
==[[Toka]]== |
||
Toka allows creation of an array using is-array. Access to the elements is done using get-element, put-element, get-char-element, and put-char-element functions. You can not initialize the values automatically using the core array functions. |
Toka allows creation of an array using is-array. Access to the elements is done using get-element, put-element, get-char-element, and put-char-element functions. You can not initialize the values automatically using the core array functions. |
||
Line 1,008: | Line 1,011: | ||
100 chars is-array bar</lang> |
100 chars is-array bar</lang> |
||
== |
==[[Visual Basic .NET]]== |
||
Implicit Size |
Implicit Size |
||
Line 1,039: | Line 1,042: | ||
<lang vbnet>Dim words() AS String = 'perl style'.split(" "c) ' You must tell VB that the space is a character by denoting c after the " "</lang> |
<lang vbnet>Dim words() AS String = 'perl style'.split(" "c) ' You must tell VB that the space is a character by denoting c after the " "</lang> |
||
== |
==[[VBScript]]== |
||
<lang vbscript>Dim myArray(2) |
<lang vbscript>Dim myArray(2) |