Arrays: Difference between revisions

From Rosetta Code
Content deleted Content added
Eriksiers (talk | contribs)
m →‎BASIC: clarified what gets zeroed without PRESERVE
Added J.
Line 86:
(setf (aref array 0) 2)
(print array))</lang>
 
=={{header|J}}==
 
In J, all data occurs in the form of regular fixed-length arrays. This is true for both named and anonymous data.
1 NB. a stand-alone scalar value is an array without any axis
1
NB. invoking any array produces that array as the result
{. array=: 1 3, 8#0 NB. create, name, then get first element of array comprised of 1 3 0 0 0 0 0 0 0 0
1
0{array NB. another way to get the first element
1
aword=:'there' NB. a literal array
0 1 3 2 2 { aword NB. multiple elements can be drawn in a single action
three
]twoD=: 3 5 $ 'abcdefghijklmnopqrstuvwxyz'
abcde
fghij
klmno
1{twoD NB. item 1 in twoD as a list of three
fghij
(<2 2){twoD NB. bracket indexing is not used in J
m
Because arrays are so important in J, a large portion of the language applies to this topic.
 
=={{header|Perl}}==

Revision as of 19:33, 6 August 2009

Task
Arrays
You are encouraged to solve this task according to the task description, using any language you may know.

This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array.

In this task, the goal is to show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element. (if available, show both fixed-length arrays and dynamic arrays, pushing a value into it.)

Please discusss at Village Pump: Arrays.

BASIC

Static: <lang qbasic>DIM staticArray(10) AS INTEGER

staticArray(0) = -1 staticArray(10) = 1

PRINT staticArray(0), staticArray(10) </lang>

Dynamic (Note that BASIC dynamic arrays are not stack-based; instead, their size has to be changed via REDIM. QBASIC lacks the PRESERVE keyword found in some modern BASICs; resizing an array without PRESERVE zeros the values): <lang qbasic>REDIM dynamicArray(10) AS INTEGER

dynamicArray(0) = -1 PRINT dynamicArray(0)

REDIM dynamicArray(20)

dynamicArray(20) = 1 PRINT dynamicArray(0), dynamicArray(20) </lang>

C++

<lang cpp>int* myArray = new int[10];

myArray[0] = 1; myArray[1] = 3;

cout << myArray[1] << endl;</lang>

Dynamic

<lang cpp>vector<int> myArray2;

myArray2.push_back(1); myArray2.push_back(3);

myArray2[0] = 2;

cout << myArray2[0] << endl;</lang>

C#

<lang csharp>int[] array = new int[10];

array[0] = 1; array[1] = 3;

Console.WriteLine(array[0]);</lang>

Dynamic

<lang csharp>using System; using System.Collections;

ArrayList<int> array = new ArrayList<int>();

array.Add(1); array.Add(3);

array[0] = 2;

Console.WriteLine(array[0]);</lang>

Common Lisp

<lang lisp>(let ((array (make-array 10)))

 (setf (aref array 0) 1
            (aref array 1) 3)
 (print array))</lang>

Dynamic

<lang lisp>(let ((array (make-array 0 :adjustable t :fill-pointer 0)))

 (vector-push-extend 1 array)
 (vector-push-extend 3 array)
 (setf (aref array 0) 2)
 (print array))</lang>

J

In J, all data occurs in the form of regular fixed-length arrays. This is true for both named and anonymous data.

   1   NB. a stand-alone scalar value is an array without any axis
1
   NB. invoking any array produces that array as the result
   {. array=: 1 3, 8#0   NB. create, name, then get first element of array comprised of 1 3 0 0 0 0 0 0 0 0
1
   0{array   NB. another way to get the first element
1
   aword=:'there'      NB. a literal array
   0 1 3 2 2 { aword   NB. multiple elements can be drawn in a single action
three
   ]twoD=: 3 5 $ 'abcdefghijklmnopqrstuvwxyz'
abcde
fghij
klmno
   1{twoD   NB. item 1 in twoD as a list of three
fghij
   (<2 2){twoD  NB. bracket indexing is not used in J
m

Because arrays are so important in J, a large portion of the language applies to this topic.

Perl

Dynamic

<lang perl>my @arr;

push @arr, 1; push @arr, 3;

$arr[0] = 2;

print $arr[0];</lang>

Python

Dynamic

<lang python>array = []

array.append(1) array.append(3)

array[0] = 2

print array[0]</lang>

R

Dynamic

<lang R> arr <- array(1)

arr <- append(arr,3)

arr[1] <- 2

print(arr[1]) </lang>

Ruby

Dynamic

<lang ruby>arr = Array.new

arr << 1 arr << 3

arr[0] = 2

puts arr[0]</lang>

Tcl

Tcl's lists are really dynamic array values behind the scenes. (Note that Tcl uses the term “array” to refer to an associative collection of variables.) <lang tcl>set ary {}

lappend ary 1 lappend ary 3

lset ary 0 2

puts [lindex $ary 0]</lang> Note also that serialization is automatic on treating as a string: <lang tcl>puts $ary; # Print the whole array</lang>