Creating an Array: Difference between revisions

no edit summary
No edit summary
Line 5:
 
=={{header|ActionScript}}==
<lang actionscript>
// ActionScript arrays are zero-based
//
Line 16:
var u:Array = [];
var v:Array = [1,2,3];
</lang>
</actionscript>
 
=={{header|Ada}}==
Line 29:
Const : constant Arr := (1 .. 10 => 1, 11 .. 20 => 2, 21 | 22 => 3);
Centered : Arr (-50..50) := (0 => 1, Others => 0);
</adalang>
Ada arrays may be indexed by enumerated types, which are discrete non-numeric types
<lang ada>
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
type Activities is (Work, Fish);
type Daily_Activities is array(Days) of Activities;
This_Week : Daily_Activities := (Mon..Fri => Work, Others => Fish);
</adalang>
 
=={{header|ALGOL 68}}==
Line 632:
List are mutable arrays. You can put anything into a list, including other lists.
 
<lang python>
empty = []
numbers = [1, 2, 3, 4, 5]
Line 641:
evens = [x for x in range(10) if not x % 2] # same using list comprehension
words = 'perl style'.split()
</pythonlang>
 
Tuples are immutable arrays. Note that tuples are defined by the "," - the parenthesis are optional (except to disambiguate when creating an empty or single-element tuple):
 
<lang python>
empty = ()
numbers = (1, 2, 3, 4, 5)
Line 651:
zeros = (0,) * 10
anything = (1, 'foo', 2.57, None, zeros)
</pythonlang>
 
Both lists and tuples can be created from other iterateables:
 
<lang python>
>>> list('abc')
['a', 'b', 'c']
Line 665:
>>> list(open('file'))
['1\n', '2\n', '3\n']
</pythonlang>
 
Note: In Python 2.6 the ''collections.namedtuple'' factory was added to the standard libraries. This can be used to create classes of lightweight objects (c.f. the Flyweight design pattern) which are tuples that can additionally support named fields.