Creating an Array: Difference between revisions

merged back some stuff deleted by spammer in edit 6218 on 7/4/07
(merged back some stuff deleted by spammer in edit 6218 on 7/4/07)
Line 526:
[0, 1, 2, 3],
[qw(a b c d e f g)],
[qw(! $ % & *)],
];
print $mdref->[1][3];
# d
 
=={{header|PHP}}==
 
For a single dimension array with 10 elements:
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) //$array[3] == 3
$array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j") //$array[3] == "c"
 
For a multi-dimension array:
$array = array(
array(0, 0, 0, 0, 0, 0),
array(1, 1, 1, 1, 1, 1),
array(2, 2, 2, 2, 2, 2),
array(3, 3, 3, 3, 3, 3)
);
#You would call the array by this code. This will call the 3rd 1 on the second list
echo $array[1][3];
 
More:
<pre>
<?php
Line 546 ⟶ 566:
?>
</pre>
 
=={{header|Pike}}==
For a single dimension int array:
array(int) x = ({ 1, 2, 3 });
 
For a single dimension of any type you declare array(mixed) instead of array(int), or just array:
array x = ({ "a", 1, 5.2 });
 
For a multi-dimension array, you build an array of arrays:
mixed x = ({ ({ 5 }),({ 3, 2 }), ({ 1, 8 }) });
 
Note that inner arrays can be of different sizes, as are simply values of the outer array.
 
=={{header|Pop11}}==
Line 617 ⟶ 649:
 
=={{header|Ruby}}==
 
I've used the same examples as the Python-example above.
my_array = Array.new
.
# This is the most basic way to create an empty one-dimensional array in Ruby.
empty = []
 
#or:
my_array = 1, 2, 3, 4, 5
empty = Array.new
# Ruby treats comma separated values on the right hand side of assignment as array. You could optionally surround the list with square bracks
numbers = [1, 2, 3, 4, 5]
# zerosmy_array = [0] *1, 102, 3, 4, 5 ]
 
anything = [1, 'foo', 2.57, zeros]
array = [
[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3]
]
# You would call the array by this code. This will call the 4th 1 on the second list
array[1][3]
 
# You can also create a sequential array from a range using the 'splat' operator:
array = [*0..3]
# or use the .to_a method for Ranges
array = (0..3).to_a
#=> [0,1,2,3]
# This lets us create the above programmatically:
array = [*0..3].map {|i| [i] * 6}
# or use the .map (.collect which is the same) method for Ranges directly
# note also that arrays of length 6 with a default element are created using Array.new
array = (0..3).map {|i| Array.new(6,i)}
#=> [[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]]
 
=={{header|Scala}}==
val array = new Array[int](10) // a 10 element array
val stringArray = new Array[String](20) // a 20 element string array
List("Elwood", "Madeline", "Archer").toArray
(List(1,2,3) ::: List(4,5,6)).toArray
(1 :: 2 :: 3 :: 4 :: 5 :: Nil).toArray
 
=={{header|Scheme}}==
Line 640 ⟶ 703:
 
(make-vector 5 0)
 
=={{header|Smalltalk}}==
array := Array withAll: #('an' 'apple' 'a' 'day' 'keeps' 'the' 'doctor' 'away').
 
"Access the first element of the array"
elem := array at: 1.
 
"Replace apple with orange"
array at: 2 put: 'orange'.
 
=={{header|Tcl}}==
 
Tcl uses the <tt>list</tt> for what many other languages call "array". A list is an ordered, numerically indexable collection of values in a single variable. Each list entry itself can be a list.
 
set a [list 5 hello {} [expr 3*5]]
 
this creates a list with the name <tt>a</tt> and four elements - the number 5, the word "hello", an empty list, and the result of the expression "3*5".
 
Tcl does have an "<tt>array</tt>", though, which is really an "associative array":
 
array set b {foo 12 bar hello}
 
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>.
 
=={{header|Toka}}==
Line 677 ⟶ 763:
Splitting strings into arrays
Dim words() AS String = 'perl style'.split(" "c) ' You must tell VB that the space is a character by denoting c after the " "
 
=={{header|VBScript}}==
 
Dim myArray(2)
myArray(0) = "Hello"
myArray(1) = "World"
myArray(2) = "!"
Anonymous user