Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -1,9 +1,22 @@
|
|||
This task is about arrays.
|
||||
|
||||
For hashes or associative arrays, please see [[Creating an Associative Array]].
|
||||
|
||||
For a definition and in-depth discussion of what an array is, see [[Array]].
|
||||
;Related tasks:
|
||||
|
||||
* [[Arrays]]
|
||||
** [[Array]]
|
||||
** [[Sum and product of an array]]
|
||||
** [[Collections]]
|
||||
** [[Creating an Associative Array]]
|
||||
** [[Two-dimensional array (runtime)]]
|
||||
* [[Vector]]
|
||||
* [[Matrices]]
|
||||
* [[Bivector]]
|
||||
* [[Antivector]]
|
||||
* [[Tensor]]
|
||||
* [[Quaternion]]
|
||||
* [[Rotor]]
|
||||
* [[Motor]]
|
||||
* [[Sedenion]]
|
||||
* [[Octonion]]
|
||||
<br>
|
||||
|
||||
;Task:
|
||||
Show basic array syntax in your language.
|
||||
|
|
@ -14,17 +27,8 @@ dynamic arrays, pushing a value into it).
|
|||
Please discuss at Village Pump: {{vp|Arrays}}.
|
||||
|
||||
Please merge code in from these obsolete tasks:
|
||||
:::* [[Creating an Array]]
|
||||
:::* [[Assigning Values to an Array]]
|
||||
:::* [[Retrieving an Element of an Array]]
|
||||
|
||||
;Related tasks:
|
||||
|
||||
* [[Array]]
|
||||
* [[Vector]]
|
||||
* [[Sum and product of an array]]
|
||||
* [[Collections]]
|
||||
* [[Creating an Associative Array]]
|
||||
* [[Two-dimensional array (runtime)]]
|
||||
<br><br>
|
||||
* [[Creating an Array]]
|
||||
* [[Assigning Values to an Array]]
|
||||
* [[Retrieving an Element of an Array]]
|
||||
<br>
|
||||
|
||||
|
|
|
|||
44
Task/Arrays/Ada/arrays.adb
Normal file
44
Task/Arrays/Ada/arrays.adb
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
procedure Array_Test is
|
||||
|
||||
type Example_Array_Type is array (1..20) of Integer;
|
||||
A, B : Example_Array_Type;
|
||||
|
||||
-- Ada array indices may begin at any value, not just 0 or 1
|
||||
C : array (-37..20) of Integer;
|
||||
|
||||
-- Ada arrays may be indexed by enumerated types, which are
|
||||
-- discrete non-numeric types
|
||||
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);
|
||||
|
||||
-- Or any numeric type
|
||||
type Fingers is range 1..4; -- exclude thumb
|
||||
type Fingers_Extended_Type is array(fingers) of Boolean;
|
||||
Fingers_Extended : Fingers_Extended_Type;
|
||||
|
||||
-- Array types may be unconstrained. The variables of the type
|
||||
-- must be constrained
|
||||
type Arr is array (Integer range <>) of Integer;
|
||||
Uninitialized : Arr (1 .. 10);
|
||||
Initialized_1 : Arr (1 .. 20) := (others => 1);
|
||||
Initialized_2 : Arr := (1 .. 30 => 2);
|
||||
Const : constant Arr := (1 .. 10 => 1, 11 .. 20 => 2, 21 | 22 => 3);
|
||||
Centered : Arr (-50..50) := (0 => 1, Others => 0);
|
||||
|
||||
Result : Integer;
|
||||
begin
|
||||
|
||||
A := (others => 0); -- Assign whole array
|
||||
B := (1 => 1, 2 => 1, 3 => 2, others => 0);
|
||||
-- Assign whole array, different values
|
||||
A (1) := -1; -- Assign individual element
|
||||
A (2..4) := B (1..3); -- Assign a slice
|
||||
A (3..5) := (2, 4, -1); -- Assign a constant slice
|
||||
A (3..5) := A (4..6); -- It is OK to overlap slices when assigned
|
||||
|
||||
Fingers_Extended(Fingers'First) := False; -- Set first element of array
|
||||
Fingers_Extended(Fingers'Last) := False; -- Set last element of array
|
||||
|
||||
end Array_Test;
|
||||
14
Task/Arrays/AutoIt/arrays.au3
Normal file
14
Task/Arrays/AutoIt/arrays.au3
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <Array.au3> ;Include extended Array functions (_ArrayDisplay)
|
||||
|
||||
Local $aInputs[1] ;Create the Array with just 1 element
|
||||
|
||||
While True ;Endless loop
|
||||
$aInputs[UBound($aInputs) - 1] = InputBox("Array", "Add one value") ;Save user input to the last element of the Array
|
||||
If $aInputs[UBound($aInputs) - 1] = "" Then ;If an empty string is entered, then...
|
||||
ReDim $aInputs[UBound($aInputs) - 1] ;...remove them from the Array and...
|
||||
ExitLoop ;... exit the loop!
|
||||
EndIf
|
||||
ReDim $aInputs[UBound($aInputs) + 1] ;Add an empty element to the Array
|
||||
WEnd
|
||||
|
||||
_ArrayDisplay($aInputs) ;Display the Array
|
||||
|
|
@ -2,7 +2,9 @@ import ballerina/io;
|
|||
|
||||
public function main() {
|
||||
// fixed length array
|
||||
int[4] a = [1, 4, 9, 16];
|
||||
int[4] a = [1, 3, 9, 16];
|
||||
// assign a value
|
||||
a[1] = 4;
|
||||
// retrieve and print element at index 1 (zero indexing)
|
||||
io:println(a[1]);
|
||||
|
||||
|
|
@ -10,6 +12,6 @@ public function main() {
|
|||
int[] b = [];
|
||||
// push an element into it
|
||||
b.push(42);
|
||||
// retrieve and print last element added
|
||||
// etrieve and print last element added
|
||||
io:println(b[b.length() - 1]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,5 @@
|
|||
int myArray2[10] = { 1, 2, 0 }; /* the rest of elements get the value 0 */
|
||||
float myFloats[] ={1.2, 2.5, 3.333, 4.92, 11.2, 22.0 }; /* automatically sizes */
|
||||
#ifdef _MSC_VER
|
||||
#define INLINE __force_inline __flatten __declspec(nothrow) __declspec(noalias) inline
|
||||
#else
|
||||
#define INLINE __attribute__((always_inline,flatten,nothrow,const)) inline
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,59 +1,3 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
typedef struct node{
|
||||
char value;
|
||||
struct node* next;
|
||||
} node;
|
||||
typedef struct charList{
|
||||
node* first;
|
||||
int size;
|
||||
} charList;
|
||||
|
||||
charList createList(){
|
||||
charList foo = {.first = NULL, .size = 0};
|
||||
return foo;
|
||||
}
|
||||
int addEl(charList* list, char c){
|
||||
if(list != NULL){
|
||||
node* foo = (node*)malloc(sizeof(node));
|
||||
if(foo == NULL) return -1;
|
||||
foo->value = c; foo->next = NULL;
|
||||
if(list->first == NULL){
|
||||
list->first = foo;
|
||||
}else{
|
||||
node* it= list->first;
|
||||
while(it->next != NULL)it = it->next;
|
||||
it->next = foo;
|
||||
}
|
||||
list->size = list->size+1;
|
||||
return 0;
|
||||
}else return -1;
|
||||
}
|
||||
int removeEl(charList* list, int index){
|
||||
if(list != NULL && list->size > index){
|
||||
node* it = list->first;
|
||||
for(int i = 0; i < index-1; i++) it = it->next;
|
||||
node* el = it->next;
|
||||
it->next = el->next;
|
||||
free(el);
|
||||
list->size--;
|
||||
return 0;
|
||||
}else return -1;
|
||||
}
|
||||
char getEl(charList* list, int index){
|
||||
if(list != NULL && list->size > index){
|
||||
node* it = list->first;
|
||||
for(int i = 0; i < index; i++) it = it->next;
|
||||
return it->value;
|
||||
}else return '\0';
|
||||
}
|
||||
static void cleanHelp(node* el){
|
||||
if(el != NULL){
|
||||
if(el->next != NULL) cleanHelp(el->next);
|
||||
free(el);
|
||||
}
|
||||
}
|
||||
void clean(charList* list){
|
||||
cleanHelp(list->first);
|
||||
list->size = 0;
|
||||
}
|
||||
int *array = malloc (sizeof(int) * 20);
|
||||
....
|
||||
array = realloc(array, sizeof(int) * 40);
|
||||
|
|
|
|||
59
Task/Arrays/C/arrays-11.c
Normal file
59
Task/Arrays/C/arrays-11.c
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
typedef struct node{
|
||||
char value;
|
||||
struct node* next;
|
||||
} node;
|
||||
typedef struct charList{
|
||||
node* first;
|
||||
int size;
|
||||
} charList;
|
||||
|
||||
charList createList(){
|
||||
charList foo = {.first = NULL, .size = 0};
|
||||
return foo;
|
||||
}
|
||||
int addEl(charList* list, char c){
|
||||
if(list != NULL){
|
||||
node* foo = (node*)malloc(sizeof(node));
|
||||
if(foo == NULL) return -1;
|
||||
foo->value = c; foo->next = NULL;
|
||||
if(list->first == NULL){
|
||||
list->first = foo;
|
||||
}else{
|
||||
node* it= list->first;
|
||||
while(it->next != NULL)it = it->next;
|
||||
it->next = foo;
|
||||
}
|
||||
list->size = list->size+1;
|
||||
return 0;
|
||||
}else return -1;
|
||||
}
|
||||
int removeEl(charList* list, int index){
|
||||
if(list != NULL && list->size > index){
|
||||
node* it = list->first;
|
||||
for(int i = 0; i < index-1; i++) it = it->next;
|
||||
node* el = it->next;
|
||||
it->next = el->next;
|
||||
free(el);
|
||||
list->size--;
|
||||
return 0;
|
||||
}else return -1;
|
||||
}
|
||||
char getEl(charList* list, int index){
|
||||
if(list != NULL && list->size > index){
|
||||
node* it = list->first;
|
||||
for(int i = 0; i < index; i++) it = it->next;
|
||||
return it->value;
|
||||
}else return '\0';
|
||||
}
|
||||
static void cleanHelp(node* el){
|
||||
if(el != NULL){
|
||||
if(el->next != NULL) cleanHelp(el->next);
|
||||
free(el);
|
||||
}
|
||||
}
|
||||
void clean(charList* list){
|
||||
cleanHelp(list->first);
|
||||
list->size = 0;
|
||||
}
|
||||
|
|
@ -1 +1,20 @@
|
|||
#define MYFLOAT_SIZE (sizeof(myFloats)/sizeof(myFloats[0]))
|
||||
#include "arrays.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
arr (int , 10) A2 = { 1, 2, 0 }; /* the rest of elements get the value 0 */
|
||||
vla (float ) FV = {1.2, 2.5, 3.333, 4.92, 11.2, 22.0 }; /* automatically sizes */
|
||||
vec_ext(float, 5) simd = {1,2,3,4,5}; /* aligned as 8x4=32bytes */
|
||||
vec_ext(int , 3) B2 = perm3(A2,2,1,0);
|
||||
|
||||
/* print elements of B2 */
|
||||
printf("B2 : %d %d %d\n", B2[0],B2[1],B2[2]);
|
||||
/* print size and alignment of array A2 */
|
||||
printf("arr : %zu/%zu/%2zu sum: %d\n", countof(A2 ), alignof(int ), alignof(A2 ), sum(A2 ,3,0));
|
||||
/* print size and alignment of VLA FV */
|
||||
printf("vla : %zu/%zu/%2zu sum: %f\n", countof(FV ), alignof(float), alignof(FV ), sum(FV ,6,0));
|
||||
/* print size and alignment of Vecor Extension simd */
|
||||
printf("vec_ext: %zu/%zu/%2zu sum: %f\n", countof(simd), alignof(float), alignof(simd), sum(simd,5,0));
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,114 @@
|
|||
long a2D_Array[3][5]; /* 3 rows, 5 columns. */
|
||||
float my2Dfloats[][3] = {
|
||||
1.0, 2.0, 0.0,
|
||||
5.0, 1.0, 3.0 };
|
||||
#define FLOAT_ROWS (sizeof(my2Dfloats)/sizeof(my2dFloats[0]))
|
||||
#pragma once
|
||||
/* gcc/clang flags
|
||||
CFLAGS="
|
||||
-march=native
|
||||
-mfpmath=<your SIMD>
|
||||
-O3
|
||||
-ftree-vectorize
|
||||
-fopenmp
|
||||
-fopenmp-simd
|
||||
-std=gnu23"
|
||||
|
||||
For MSVC see: https://learn.microsoft.com/en-us/cpp/parallel/openmp/openmp-simd?view=msvc-180
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdalign.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic ignored "-Wgnu-alignof-expression"
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define INLINE __force_inline __flatten __declspec(nothrow) __declspec(noalias) inline
|
||||
#else
|
||||
#define INLINE __attribute__((always_inline,flatten,nothrow,const)) inline
|
||||
#endif
|
||||
|
||||
/* define missing bit ceil function first for power of 2 memory alignment */
|
||||
#define BITOP_RUP01__(x) ( (x) | ( (x) >> 1))
|
||||
#define BITOP_RUP02__(x) (BITOP_RUP01__(x) | (BITOP_RUP01__(x) >> 2))
|
||||
#define BITOP_RUP04__(x) (BITOP_RUP02__(x) | (BITOP_RUP02__(x) >> 4))
|
||||
#define BITOP_RUP08__(x) (BITOP_RUP04__(x) | (BITOP_RUP04__(x) >> 8))
|
||||
#define BITOP_RUP16__(x) (BITOP_RUP08__(x) | (BITOP_RUP08__(x) >> 16))
|
||||
|
||||
#define bitceil(x) (BITOP_RUP16__(((uint32_t)(x)) - 1) + 1)
|
||||
|
||||
/* define different array types */
|
||||
#define arr(T,N) typeof(T[N]) /* fixed static array of arbitrary length and type */
|
||||
#define vla(T ) typeof(T[ ]) /* variable length array - VLA */
|
||||
|
||||
/* define vector extension power of 2 sized SIMD vector types for non MSVC */
|
||||
#ifndef _MSC_VER
|
||||
#ifdef __clang__
|
||||
#define vec_ext(T,N) typeof(T __attribute__((ext_vector_type(N))))
|
||||
#else
|
||||
#define vec_ext(T,N) typeof(T __attribute__((vector_size(bitceil(alignof(T) * N)))))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* sum using OpenMP */
|
||||
#define sum(a,n,i) \
|
||||
({ \
|
||||
typeof((a)[0]) dst = i; \
|
||||
_Pragma("omp simd reduction(+:dst)") \
|
||||
for(size_t j = 0; j < MIN(countof(a),n); j++) \
|
||||
dst += (a)[j]; \
|
||||
dst; \
|
||||
})
|
||||
|
||||
/* dot using OpenMP */
|
||||
#define dot(a,b,n,i) \
|
||||
({ \
|
||||
typeof((a)[0]) dst = i; \
|
||||
_Pragma("omp simd reduction(+:dst)") \
|
||||
for(size_t j = 0; j < MIN(MIN(countof(a),countof(b)),n); j++) \
|
||||
dst += (a)[j] * (b)[j]; \
|
||||
dst; \
|
||||
})
|
||||
|
||||
/* define array verification and element count */
|
||||
#undef countof
|
||||
#define countof(a ) (sizeof((a))/sizeof((a)[0]))
|
||||
#define isarray(a ) __builtin_choose_expr(__builtin_types_compatible_p(typeof((a)[0]) [], typeof((a))), true, false)
|
||||
/* define variable argument macros */
|
||||
#define countargs(...) (0 __VA_OPT__(+sizeof((typeof(__VA_ARGS__)[]){__VA_ARGS__})/sizeof(__VA_ARGS__)))
|
||||
#define emptyargs(...) (true __VA_OPT__(-1))
|
||||
|
||||
/* add missing parens and expand for permute */
|
||||
#define PARENS ()
|
||||
#define EXPAND(...) EXPAND4(EXPAND4(EXPAND4(EXPAND4(__VA_ARGS__))))
|
||||
#define EXPAND4(...) EXPAND3(EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__))))
|
||||
#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__))))
|
||||
#define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__))))
|
||||
#define EXPAND1(...) __VA_ARGS__
|
||||
|
||||
/* add permute */
|
||||
#define perm(a,...) { __VA_OPT__(EXPAND(perm_helper(a,__VA_ARGS__))) }
|
||||
#define perm_helper(a,i,...) (a)[i], __VA_OPT__(perm_again PARENS (a,__VA_ARGS__))
|
||||
#define perm_again() perm_helper
|
||||
|
||||
#define perm3(a,x,y,z ) (vec_ext(typeof((a)[0]),3))perm(a,x,y,z )
|
||||
#define perm4(a,x,y,z,w) (vec_ext(typeof((a)[0]),4))perm(a,x,y,z,w)
|
||||
|
||||
/* cross3 - 3-dimensional vector product for vector extension types only
|
||||
* TODO:
|
||||
* - support vector/array types without operators
|
||||
* - n-dimensional hodge star operator vector product based on
|
||||
* levi-civita, laplace expansion and determinant as a
|
||||
* left contraction grade projection operator. See Clifford, Hodge,
|
||||
*/
|
||||
#define cross3(a,b) \
|
||||
perm3(a,1,2,0) * perm3(b,2,0,1) \
|
||||
- perm3(a,2,1,0) * perm3(b,1,2,0)
|
||||
|
||||
/* TODO: https://github.com/HolyBlackCat/macro_sequence_for
|
||||
* duplicate array using initializer list and
|
||||
* make indexed sequence unrolling for loop based on
|
||||
* boilerplates.
|
||||
*/
|
||||
#define dup(a ) { (a)[0]...(a)[countof(a)-1] };
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
int numElements = 10;
|
||||
int *myArray = malloc(sizeof(int) * numElements); /* array of 10 integers */
|
||||
if ( myArray != NULL ) /* check to ensure allocation succeeded. */
|
||||
{
|
||||
/* allocation succeeded */
|
||||
/* at the end, we need to free the allocated memory */
|
||||
free(myArray);
|
||||
}
|
||||
/* calloc() additionally pre-initializes to all zeros */
|
||||
short *myShorts = calloc( numElements, sizeof(short)); /* array of 10 */
|
||||
if (myShorts != NULL)....
|
||||
When defining autosized multidimensional arrays, all the dimensions except the first (leftmost) need to be defined. This is required in order for the compiler to generate the proper indexing for the array.
|
||||
<syntaxhighlight lang="c">long a2D_Array[3][5]; /* 3 rows, 5 columns. */
|
||||
float my2Dfloats[][3] = {
|
||||
1.0, 2.0, 0.0,
|
||||
5.0, 1.0, 3.0 };
|
||||
printf("rows: %zu\n",countof(my2Dfloats)); /* print row count */
|
||||
|
|
|
|||
|
|
@ -1,2 +1,11 @@
|
|||
myArray[0] = 1;
|
||||
myArray[1] = 3;
|
||||
int numElements = 10;
|
||||
int *myArray = malloc(sizeof(int) * numElements); /* array of 10 integers */
|
||||
if ( myArray != NULL ) /* check to ensure allocation succeeded. */
|
||||
{
|
||||
/* allocation succeeded */
|
||||
/* at the end, we need to free the allocated memory */
|
||||
free(myArray);
|
||||
}
|
||||
/* calloc() additionally pre-initializes to all zeros */
|
||||
short *myShorts = calloc( numElements, sizeof(short)); /* array of 10 */
|
||||
if (myShorts != NULL)....
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
printf("%d\n", myArray[1]);
|
||||
myArray[0] = 1;
|
||||
myArray[1] = 3;
|
||||
|
|
|
|||
|
|
@ -1,3 +1 @@
|
|||
*(array + index) = 1;
|
||||
printf("%d\n", *(array + index));
|
||||
3[array] = 5;
|
||||
printf("%d\n", myArray[1]);
|
||||
|
|
|
|||
|
|
@ -1,10 +1,3 @@
|
|||
#define XSIZE 20
|
||||
double *kernel = malloc(sizeof(double)*2*XSIZE+1);
|
||||
if (kernel) {
|
||||
kernel += XSIZE;
|
||||
for (ix=-XSIZE; ix<=XSIZE; ix++) {
|
||||
kernel[ix] = f(ix);
|
||||
....
|
||||
free(kernel-XSIZE);
|
||||
}
|
||||
}
|
||||
*(array + index) = 1;
|
||||
printf("%d\n", *(array + index));
|
||||
3[array] = 5;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
int *array = malloc (sizeof(int) * 20);
|
||||
....
|
||||
array = realloc(array, sizeof(int) * 40);
|
||||
#define XSIZE 20
|
||||
double *kernel = malloc(sizeof(double)*2*XSIZE+1);
|
||||
if (kernel) {
|
||||
kernel += XSIZE;
|
||||
for (ix=-XSIZE; ix<=XSIZE; ix++) {
|
||||
kernel[ix] = f(ix);
|
||||
....
|
||||
free(kernel-XSIZE);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
39
Task/Arrays/COBOL/arrays.cob
Normal file
39
Task/Arrays/COBOL/arrays.cob
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. arrays.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 fixed-length-table.
|
||||
03 fixed-table-elt PIC X OCCURS 5 TIMES.
|
||||
|
||||
01 table-length PIC 9(5) VALUE 1.
|
||||
01 variable-length-table.
|
||||
03 variable-table-elt PIC X OCCURS 1 TO 5 TIMES
|
||||
DEPENDING ON table-length.
|
||||
|
||||
01 initial-value-area.
|
||||
03 initial-values.
|
||||
05 FILLER PIC X(10) VALUE "One".
|
||||
05 FILLER PIC X(10) VALUE "Two".
|
||||
05 FILLER PIC X(10) VALUE "Three".
|
||||
03 initial-value-table REDEFINES initial-values.
|
||||
05 initial-table-elt PIC X(10) OCCURS 3 TIMES.
|
||||
|
||||
01 indexed-table.
|
||||
03 indexed-elt PIC X OCCURS 5 TIMES
|
||||
INDEXED BY table-index.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
*> Assigning the contents of an entire table.
|
||||
MOVE "12345" TO fixed-length-table
|
||||
|
||||
*> Indexing an array (using an index)
|
||||
MOVE 1 TO table-index
|
||||
MOVE "1" TO indexed-elt (table-index)
|
||||
|
||||
*> Pushing a value into a variable-length table.
|
||||
ADD 1 TO table-length
|
||||
MOVE "1" TO variable-table-elt (2)
|
||||
|
||||
GOBACK
|
||||
.
|
||||
29
Task/Arrays/Euphoria/arrays.eu
Normal file
29
Task/Arrays/Euphoria/arrays.eu
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
--Arrays task for Rosetta Code wiki
|
||||
--User:Lnettnay
|
||||
|
||||
atom dummy
|
||||
--Arrays are sequences
|
||||
-- single dimensioned array of 10 elements
|
||||
sequence xarray = repeat(0,10)
|
||||
xarray[5] = 5
|
||||
dummy = xarray[5]
|
||||
? dummy
|
||||
|
||||
--2 dimensional array
|
||||
--5 sequences of 10 elements each
|
||||
sequence xyarray = repeat(repeat(0,10),5)
|
||||
xyarray[3][6] = 12
|
||||
dummy = xyarray[3][6]
|
||||
? dummy
|
||||
|
||||
--dynamic array use (all sequences can be modified at any time)
|
||||
sequence dynarray = {}
|
||||
for x = 1 to 10 do
|
||||
dynarray = append(dynarray, x * x)
|
||||
end for
|
||||
? dynarray
|
||||
|
||||
for x = 1 to 10 do
|
||||
dynarray = prepend(dynarray, x)
|
||||
end for
|
||||
? dynarray
|
||||
22
Task/Arrays/Haxe/arrays.hx
Normal file
22
Task/Arrays/Haxe/arrays.hx
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Main {
|
||||
static public function main():Void {
|
||||
// Array (dynamic length)
|
||||
var a = new Array<Int>();
|
||||
for (i in 1...4)
|
||||
a.push(i);
|
||||
// alt: var a = [1, 2, 3];
|
||||
// alt2: var a = [for (i in 1...4)];
|
||||
|
||||
for (i in 0...a.length)
|
||||
trace(a[i]);
|
||||
|
||||
// Vector (fixed-length)
|
||||
var v = new haxe.ds.Vector(3);
|
||||
v[0] = 1;
|
||||
v[1] = 2;
|
||||
v[2] = 3;
|
||||
|
||||
for (i in 0...v.length)
|
||||
trace(v[i]);
|
||||
}
|
||||
}
|
||||
1
Task/Arrays/PowerShell/arrays-1.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-1.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$a = @()
|
||||
2
Task/Arrays/PowerShell/arrays-2.ps1
Normal file
2
Task/Arrays/PowerShell/arrays-2.ps1
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$a = ,2
|
||||
$a = @(2) # alternative
|
||||
1
Task/Arrays/PowerShell/arrays-3.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-3.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$a = 1,2,3
|
||||
1
Task/Arrays/PowerShell/arrays-4.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-4.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$a += 5
|
||||
1
Task/Arrays/PowerShell/arrays-5.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-5.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$a[1]
|
||||
1
Task/Arrays/PowerShell/arrays-6.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-6.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$a[1] = 42
|
||||
1
Task/Arrays/PowerShell/arrays-7.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-7.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$r = 1..100
|
||||
1
Task/Arrays/PowerShell/arrays-8.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-8.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$r[0..9+25..27+80,85,90]
|
||||
1
Task/Arrays/PowerShell/arrays-9.ps1
Normal file
1
Task/Arrays/PowerShell/arrays-9.ps1
Normal file
|
|
@ -0,0 +1 @@
|
|||
$r[-1] # last index
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
-- 23 Aug 2025
|
||||
-- 21 Feb 2026
|
||||
include Setting
|
||||
|
||||
say 'ARRAYS BASIC USAGE'
|
||||
|
|
@ -15,7 +15,7 @@ say 'Mimic indexing...'
|
|||
say 'Square of' 3 'is' a(3)
|
||||
say 'Square of' 7 'is' a(7)
|
||||
say
|
||||
call DumpVariables
|
||||
call Showvars
|
||||
exit
|
||||
|
||||
A:
|
||||
|
|
@ -23,4 +23,5 @@ procedure expose a.
|
|||
arg xx
|
||||
return a.xx
|
||||
|
||||
-- Showvars
|
||||
include Math
|
||||
|
|
|
|||
7
Task/Arrays/ReScript/arrays.res
Normal file
7
Task/Arrays/ReScript/arrays.res
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
let arr = [1, 2, 3]
|
||||
|
||||
let _ = Js.Array2.push(arr, 4)
|
||||
|
||||
arr[3] = 5
|
||||
|
||||
Js.log(Js.Int.toString(arr[3]))
|
||||
2
Task/Arrays/Rebol/arrays-1.rebol
Normal file
2
Task/Arrays/Rebol/arrays-1.rebol
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a: [] ; Empty.
|
||||
b: ["foo"] ; Pre-initialized.
|
||||
2
Task/Arrays/Rebol/arrays-2.rebol
Normal file
2
Task/Arrays/Rebol/arrays-2.rebol
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
append a ["up" "down"] ; -> ["up" "down"]
|
||||
insert a [left right] ; -> [left right "up" "down"]
|
||||
4
Task/Arrays/Rebol/arrays-3.rebol
Normal file
4
Task/Arrays/Rebol/arrays-3.rebol
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
first a ; -> left
|
||||
third a ; -> "up"
|
||||
last a ; -> "down"
|
||||
a/2 ; -> right (Note: Rebolis 1-based.)
|
||||
10
Task/Arrays/Rebol/arrays-4.rebol
Normal file
10
Task/Arrays/Rebol/arrays-4.rebol
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
a ; -> [left right "up" "down"]
|
||||
next a ; -> [right "up" "down"]
|
||||
skip a 2 ; -> ["up" "down"]
|
||||
|
||||
a: next a ; -> [right "up" "down"]
|
||||
head a ; -> [left right "up" "down"]
|
||||
|
||||
copy a ; -> [left right "up" "down"]
|
||||
copy/part a 2 ; -> [left right]
|
||||
copy/part skip a 2 2 ; -> ["up" "down"]
|
||||
3
Task/Arrays/Rye/arrays.rye
Normal file
3
Task/Arrays/Rye/arrays.rye
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var 'arr ref { 1 2 3 4 }
|
||||
append! 5 'arr
|
||||
print arr -> 4
|
||||
4
Task/Arrays/Uiua/arrays.uiua
Normal file
4
Task/Arrays/Uiua/arrays.uiua
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[1 3 4 7]
|
||||
⍜⊡◌ 2 :5
|
||||
&s.
|
||||
&p ⊡1
|
||||
|
|
@ -46,9 +46,23 @@ pub fn main() {
|
|||
println("array4: $array4")
|
||||
|
||||
// Arrays can be sliced, creating a copy
|
||||
mut array5 := array4[0..3]
|
||||
mut array5 := array4[0..3].clone()
|
||||
println("array5: $array5")
|
||||
array5[2] = 10
|
||||
println("array4: $array4")
|
||||
println("array5: $array5")
|
||||
|
||||
// Multidimensional Arrays
|
||||
// adjust details: {len: , cap: , init:}
|
||||
mut multi_array_a := [][]int{len: 2, init: []int{len: 3}}
|
||||
multi_array_a[0][1] = 2
|
||||
// easy initialization of fixed-size multidimensional arrays
|
||||
mut multi_array_b := [2][3]int{}
|
||||
multi_array_b[0][1] = 2
|
||||
// dynamic
|
||||
mut multi_array_c := [][]int{}
|
||||
multi_array_c << [[0, 2, 0], [0, 0, 0]] // appended by using push operator `<<`
|
||||
println("multi_array_a: $multi_array_a") // [[0, 2, 0], [0, 0, 0]]
|
||||
println("multi_array_b: $multi_array_b") // [[0, 2, 0], [0, 0, 0]]
|
||||
println("multi_array_c: $multi_array_c") // [[0, 2, 0], [0, 0, 0]]
|
||||
}
|
||||
|
|
|
|||
45
Task/Arrays/VBScript/arrays.vbs
Normal file
45
Task/Arrays/VBScript/arrays.vbs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
'Arrays - VBScript - 08/02/2021
|
||||
|
||||
'create a static array
|
||||
Dim a(3) ' 4 items : a(0), a(1), a(2), a(3)
|
||||
'assign a value to elements
|
||||
For i = 1 To 3
|
||||
a(i) = i * i
|
||||
Next
|
||||
'and retrieve elements
|
||||
buf=""
|
||||
For i = 1 To 3
|
||||
buf = buf & a(i) & " "
|
||||
Next
|
||||
WScript.Echo buf
|
||||
|
||||
'create a dynamic array
|
||||
Dim d()
|
||||
ReDim d(3) ' 4 items : d(0), d(1), d(2), d(3)
|
||||
For i = 1 To 3
|
||||
d(i) = i * i
|
||||
Next
|
||||
buf=""
|
||||
For i = 1 To 3
|
||||
buf = buf & d(i) & " "
|
||||
Next
|
||||
WScript.Echo buf
|
||||
|
||||
d(0) = 0
|
||||
'expand the array and preserve existing values
|
||||
ReDim Preserve d(4) ' 5 items : d(0), d(1), d(2), d(3), d(4)
|
||||
d(4) = 16
|
||||
buf=""
|
||||
For i = LBound(d) To UBound(d)
|
||||
buf = buf & d(i) & " "
|
||||
Next
|
||||
WScript.Echo buf
|
||||
|
||||
'create and initialize an array dynamicaly
|
||||
b = Array(1, 4, 9)
|
||||
'and retrieve all elements
|
||||
WScript.Echo Join(b,",")
|
||||
|
||||
'Multi-Dimensional arrays
|
||||
'The following creates a 5x4 matrix
|
||||
Dim mat(4,3)
|
||||
57
Task/Arrays/VHDL/arrays.vhd
Normal file
57
Task/Arrays/VHDL/arrays.vhd
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
entity Array_Test is
|
||||
end entity Array_Test;
|
||||
|
||||
architecture Example of Array_test is
|
||||
|
||||
-- Array type have to be defined first
|
||||
type Integer_Array is array (Integer range <>) of Integer;
|
||||
|
||||
-- Array index range can be ascending...
|
||||
signal A : Integer_Array (1 to 20);
|
||||
|
||||
-- or descending
|
||||
signal B : Integer_Array (20 downto 1);
|
||||
|
||||
-- VHDL array index ranges may begin at any value, not just 0 or 1
|
||||
signal C : Integer_Array (-37 to 20);
|
||||
|
||||
-- VHDL arrays may be indexed by enumerated types, which are
|
||||
-- discrete non-numeric types
|
||||
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
|
||||
type Activities is (Work, Fish);
|
||||
type Daily_Activities is array (Days) of Activities;
|
||||
signal This_Week : Daily_Activities := (Mon to Fri => Work, Others => Fish);
|
||||
|
||||
type Finger is range 1 to 4; -- exclude thumb
|
||||
type Fingers_Extended is array (Finger) of Boolean;
|
||||
signal Extended : Fingers_Extended;
|
||||
|
||||
-- Array types may be unconstrained.
|
||||
-- Objects of the type must be constrained
|
||||
type Arr is array (Integer range <>) of Integer;
|
||||
signal Uninitialized : Arr (1 to 10);
|
||||
signal Initialized_1 : Arr (1 to 20) := (others => 1);
|
||||
constant Initialized_2 : Arr := (1 to 30 => 2);
|
||||
constant Const : Arr := (1 to 10 => 1, 11 to 20 => 2, 21 | 22 => 3);
|
||||
signal Centered : Arr (-50 to 50) := (0 => 1, others => 0);
|
||||
|
||||
signal Result : Integer;
|
||||
|
||||
begin
|
||||
|
||||
A <= (others => 0); -- Assign whole array
|
||||
B <= (1 => 1, 2 => 1,
|
||||
3 => 2, others => 0); -- Assign whole array, different values
|
||||
A (1) <= -1; -- Assign individual element
|
||||
A (2 to 4) <= B (3 downto 1); -- Assign a slice
|
||||
A (3 to 5) <= (2, 4, -1); -- Assign an aggregate
|
||||
A (3 to 5) <= A (4 to 6); -- It is OK to overlap slices when assigned
|
||||
|
||||
-- VHDL arrays does not have 'first' and 'last' elements,
|
||||
-- but have 'Left' and 'Right' instead
|
||||
Extended (Extended'Left) <= False; -- Set leftmost element of array
|
||||
Extended (Extended'Right) <= False; -- Set rightmost element of array
|
||||
|
||||
Result <= A (A'Low) + B (B'High);
|
||||
|
||||
end architecture Example;
|
||||
Loading…
Add table
Add a link
Reference in a new issue