langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,13 @@
!include NSISArray.nsh
Function ArrayTest
Push $0
; Declaring an array
NSISArray::New TestArray 1 2
NSISArray::Push TestArray "Hello"
; NSISArray arrays are dynamic by default.
NSISArray::Push TestArray "World"
NSISArray::Read TestArray 1
Pop $0
DetailPrint $0
Pop $0
FunctionEnd

View file

@ -0,0 +1,20 @@
using System;
using System.Console;
using System.Collections;
module ArrayOps
{
Main() : void
{
def fives = array(10);
foreach (i in [1 .. 10]) fives[i - 1] = i * 5;
def ten = fives[1];
WriteLine($"Ten: $ten");
def dynamic = ArrayList();
dynamic.Add(1);
dynamic.Add(3);
dynamic[1] = 2;
foreach (i in dynamic) Write($"$i\t"); // Nemerle isn't great about displaying arrays, it's better with lists though
}
}

View file

@ -0,0 +1,34 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
array = int[10]
array[0] = 42
say array[0] array[3]
say
words = ['Ogof', 'Ffynnon', 'Ddu']
say words[0] words[1] words[2]
say
-- Dynamic arrays can be simulated via the Java Collections package
splk = ArrayList()
splk.add(words[0])
splk.add(words[1])
splk.add(words[2])
splk.add('Draenen')
say splk.get(0) splk.get(3)
say splk.get(0) splk.get(1) splk.get(2)
say
-- or by using NetRexx "indexed strings" (associative arrays)
cymru = ''
cymru[0] = 0
cymru[0] = cymru[0] + 1; cymru[cymru[0]] = splk.get(0) splk.get(1) splk.get(2)
cymru[0] = cymru[0] + 1; cymru[cymru[0]] = splk.get(0) splk.get(3)
loop x_ = 1 to cymru[0] by 1
say x_':' cymru[x_]
end x_

View file

@ -0,0 +1,2 @@
(array 5)
→ (nil nil nil nil nil)

View file

@ -0,0 +1,2 @@
(set 'myarray (array 3 4 (sequence 1 12)))
→ ((1 2 3 4) (5 6 7 8) (9 10 11 12))

View file

@ -0,0 +1,17 @@
# Array.make 6 'A' ;;
- : char array = [|'A'; 'A'; 'A'; 'A'; 'A'; 'A'|]
# Array.init 8 (fun i -> i * 10) ;;
- : int array = [|0; 10; 20; 30; 40; 50; 60; 70|]
# let arr = [|0; 1; 2; 3; 4; 5; 6 |] ;;
val arr : int array = [|0; 1; 2; 3; 4; 5; 6|]
# arr.(4) ;;
- : int = 4
# arr.(4) <- 65 ;;
- : unit = ()
# arr ;;
- : int array = [|0; 1; 2; 3; 65; 5; 6|]

View file

@ -0,0 +1,10 @@
bundle Default {
class Arithmetic {
function : Main(args : System.String[]), Nil {
array := Int->New[2];
array[0] := 13;
array[1] := 7;
(array[0] + array[1])->PrintLine();
}
}
}

View file

@ -0,0 +1,23 @@
// NSArrays are ordered collections of NSObject subclasses only.
// Create an array of NSString objects.
NSArray *firstArray = [[NSArray alloc] initWithObjects:@"Hewey", @"Louie", @"Dewey", nil];
// NSArrays are immutable; it does have a mutable subclass, however - NSMutableArray.
// Let's instantiate one with a mutable copy of our array.
// We can do this by sending our first array a -mutableCopy message.
NSMutableArray *secondArray = [firstArray mutableCopy];
// Replace Louie with Launchpad McQuack.
[secondArray replaceObjectAtIndex:1 withObject:@"Launchpad"];
// Display the first object in the array.
NSLog(@"%@", [secondArray objectAtIndex:0]);
// In non-ARC or non-GC environments, retained objects must be released later.
[firstArray release];
[secondArray release];
// There is also a modern syntax which allows convenient creation of autoreleased immutable arrays.
// No nil termination is then needed.
NSArray *thirdArray = @[ @"Hewey", @"Louie", @"Dewey", @1, @2, @3 ];

View file

@ -0,0 +1,17 @@
'CREATING AN ARRAY
float f[100]
'SETTING INDEX BASE
indexbase 1 'default
'FILLING PART OF AN ARRAY
f[20]<=1,2,3,4,5,1.25
'MAPPING AN ARRAY TO ANOTHER
float *g
@g=@f[20]
print g[6] 'result 1.25

8
Task/Arrays/Oz/arrays.oz Normal file
View file

@ -0,0 +1,8 @@
declare
Arr = {Array.new 1 %% lowest index
10 %% highest index
37} %% all 10 fields initialized to 37
in
{Show Arr.1}
Arr.1 := 64
{Show Arr.1}

View file

@ -0,0 +1,3 @@
v=[];
v=concat(v,7);
v[1]

View file

@ -0,0 +1,19 @@
/* Example of an array having fixed dimensions */
declare A(10) float initial (1, 9, 4, 6, 7, 2, 5, 8, 3, 10);
A(6) = -45;
/* Example of an array having dynamic bounds. */
get list (N);
begin;
declare B(N) float initial (9, 4, 7, 3, 8, 11, 0, 5, 15, 6);
B(3) = -11;
end;
/* Example of a dynamic array. */
declare C(N) float controlled;
get list (N);
allocate C;
C = 0;
c(7) = 12;

View file

@ -0,0 +1,33 @@
Program ArrayDemo;
uses
SysUtils;
var
StaticArray: array[0..9] of Integer;
DynamicArray: array of Integer;
StaticArrayText,
DynamicArrayText: string;
lcv: Integer;
begin
// Setting the length of the dynamic array the same as the static one
SetLength(DynamicArray, Length(StaticArray));
// Asking random numbers storing into the static array
for lcv := 0 to Pred(Length(StaticArray)) do
begin
write('Enter a integer random number for position ', Succ(lcv), ': ');
readln(StaticArray[lcv]);
end;
// Storing entered numbers of the static array in reverse order into the dynamic
for lcv := 0 to Pred(Length(StaticArray)) do
DynamicArray[Pred(Length(DynamicArray)) - lcv] := StaticArray[lcv];
// Concatenating the static and dynamic array into a single string variable
StaticArrayText := '';
DynamicArrayText := '';
for lcv := 0 to Pred(Length(StaticArray)) do
begin
StaticArrayText := StaticArrayText + IntToStr(StaticArray[lcv]) + ' ';
DynamicArrayText := DynamicArrayText + IntToStr(DynamicArray[lcv]) + ' ';
end;
// Displaying both arrays
writeln(StaticArrayText);
writeln(DynamicArrayText);
end.

View file

@ -0,0 +1,8 @@
my @arr;
push @arr, 1;
push @arr, 3;
@arr[0] = 2;
say @arr[0];

View file

@ -0,0 +1,8 @@
int main(){
// Initial array, few random elements.
array arr = ({3,"hi",84.2});
arr += ({"adding","to","the","array"}); // Lets add some elements.
write(arr[5] + "\n"); // And finally print element 5.
}

View file

@ -0,0 +1,16 @@
%Declaring array
/x [0 1] def
%Assigning value to an element, PostScript arrays are 0 based.
x 0 3 put
%Print array
x pstack
[3 1]
%Get an element
x 1 get

View file

@ -0,0 +1 @@
$a = @()

View file

@ -0,0 +1,2 @@
$a = ,2
$a = @(2) # alternative

View file

@ -0,0 +1 @@
$a = 1,2,3

View file

@ -0,0 +1 @@
$a += 5

View file

@ -0,0 +1 @@
$a[1]

View file

@ -0,0 +1 @@
$a[1] = 42

View file

@ -0,0 +1 @@
$r = 1..100

View file

@ -0,0 +1 @@
$r[0..9+25..27+80,85,90]

View file

@ -0,0 +1 @@
$r[-1] # last index

View file

@ -0,0 +1,5 @@
;Set up an Array of 23 cells, e.g. 0-22
Dim MyArray.i(22)
MyArray(0) = 7
MyArray(1) = 11
MyArray(7) = 23

View file

@ -0,0 +1,5 @@
;Extend the Array above to 56 items without affecting the already stored data
ReDim MyArray(55)
MyArray(22) = 7
MyArray(33) = 11
MyArray(44) = 23

View file

@ -0,0 +1,6 @@
;Find all 6 non-zero cells from the Array above
For i=0 To ArraySize(MyArray())
If MyArray(i)
PrintN(Str(i)+" differs from zero.")
EndIf
Next

View file

@ -0,0 +1,4 @@
; Now, set up a multi dimensional Array
Dim MultiArray.i(800, 600)
MultiArray(100, 200) = 640
MultiArray(130, 40) = 120

View file

@ -0,0 +1,2 @@
Dim MultiArray2.i(64, 128, 32)
PrintN( Str(ArraySize(MultiArray2(), 2)) ; Will tell that second dimension size is '128'

View file

@ -0,0 +1,2 @@
a: [] ; Empty.
b: ["foo"] ; Pre-initialized.

View file

@ -0,0 +1,2 @@
append a ["up" "down"] ; -> ["up" "down"]
insert a [left right] ; -> [left right "up" "down"]

View file

@ -0,0 +1,4 @@
first a ; -> left
third a ; -> "up"
last a ; -> "down"
a/2 ; -> right (Note: REBOL is 1-based.)

View 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"]

View file

@ -0,0 +1,29 @@
// 1-D (row- or column-vectors)
// Static:
// row-vector
x = [1:3];
x = zeros(1,3); x[1]=1; x[2]=2; x[3]=3;
// column-vector
x = [1:3]'; // or
x = [1;2;3]; // or
x = zeros(3,1); x[1]=1; x[2]=2; x[3]=3;
// Dynamic:
x = []; // create an empty array
x = [x; 1, 2]; // add a row to 'x' containing [1, 2], or
x = [x, [1; 2]]; // add a column to 'x' containing [1; 2]
// 2-D array
// Static:
x = zeros(3,5); // create an zero-filed matrix of size 3x5
x[1;1] = 1; // set the x(1,1) element to 1
x[2;] = [1,2,3,4,5]; // set the second row x(2,) to a row vector
x[3;4:5] = [2,3]; // set x(3,4) to 2 and x(3,5) to 3
// Dynamic
x = [1:5]; // create an row-vector x(1,1)=1, x(1,2)=2, ... x(1,5)=5
x = [x; 2, 3, 4, 6, 7]; // add to 'x' a row.
// Accessing an element of arrays:
// to retrieve/print element of matrix 'x' just put this in a single line in the script
i=1;
j=2;
x[i;j]

View file

@ -0,0 +1,27 @@
//-Static array
//--def of 10 el array of integers, initialised to zeros
D array...
D s 10i 0 dim(10)
D inz
//--def an el
D el_1...
D s 10i 0 inz
/free
//-assign first el
//--first element of RPG array is indexed with 1
array(1) = 111;
//-get first el of array
el_1 = array(1);
//--display it
dsply ('First el of array='+%char(el_1));
//--displays: First el of array=111
//---or shorter, without "el_1"
dsply ('First el of array='+%char(array(1)));
//--displays: First el of array=111
/end-free

View file

@ -0,0 +1,34 @@
needs array'
( Create an array with four elements )
^array'new{ 1 2 3 4 } constant a
( Add 10 to each element in an array and update the array with the results )
a [ 10 + ] ^array'map
( Apply a quote to each element in an array; leaves the contents alone )
a [ 10 + putn cr ] ^array'apply
( Display an array )
a ^array'display
( Look for a value in an array )
3 a ^array'in?
6 a ^array'in?
( Look for a string in an array )
"hello" a ^array'stringIn?
( Reverse the order of items in an array )
a ^array'reverse
( Append two arrays and return a new one )
^array'new{ 1 2 3 } constant a
^array'new{ 4 5 6 } constant b
a b ^array'append constant c
( Create an array from the values returned by a quote )
[ 1 2 "hello" "world" ] ^array'fromQuote constant d
( Create a quote from the values in an array )
d ^array'toQuote

View file

@ -0,0 +1,9 @@
print "Enter array 1 greater than 0"; : input a1
print "Enter array 2 greater than 0"; : input a2
dim chrArray$(max(a1,1),max(a2,1))
dim numArray(max(a1,1),max(a2,1))
chrArray$(1,1) = "Hello"
numArray(1,1) = 987.2
print chrArray$(1,1);" ";numArray(1,1)

View file

@ -0,0 +1,14 @@
slate[1]> #x := ##(1 2 3).
{1. 2. 3}
slate[2]> x
{1. 2. 3}
slate[3]> #y := {1 + 2. 3 + 4. 5}.
{3. 7. 5}
slate[4]> y at: 2 put: 99.
99
slate[5]> y
{3. 7. 99}
slate[6]> x first
1
slate[7]> x at: 0.
1

View file

@ -0,0 +1,5 @@
array = Object('zero', 'one', 'two')
array.Add('three')
array.Add('five', at: 5)
array[4] = 'four'
Print(array[3]) --> 'three'

View file

@ -0,0 +1 @@
{1,2,3,4,5}→L1

View file

@ -0,0 +1,3 @@
{1,2,3,4,5}→L1
Disp L1(3)
0→L1(4)

View file

@ -0,0 +1,7 @@
@(collect)
@line
@(bind f @(split-str line ","))
@(output)
@{f[-1]}@\t@{f[1..-1] "\t"}@\t@{f[0]}
@(end)
@(end)

View file

@ -0,0 +1,5 @@
$array[0] = "hi";
$array[1] = "hello";
for(%i=0;%i<2;%i++)
echo($array[%i]);

View file

@ -0,0 +1,5 @@
$array["Greet",0] = "hi";
$array["Greet",1] = "hello";
for(%i=0;%i<2;%i++)
echo($array["Greet",%i]);

View file

@ -0,0 +1,6 @@
alist=( item1 item2 item3 ) # creates a 3 item array called "alist"
declare -a list2 # declare an empty list called "list2"
declare -a list3[0] # empty list called "list3"; the subscript is ignored
# create a 4 item list, with a specific order
list5=([3]=apple [2]=cherry [1]=banana [0]=strawberry)

View file

@ -0,0 +1 @@
unset alist

View file

@ -0,0 +1,2 @@
count=${#alist[*]}
echo "The number of items in alist is ${#alist[*]}"

View file

@ -0,0 +1,5 @@
x=0
while [[ $x < ${#alist[*]} ]]; do
echo "Item $x = ${alist[$x]}"
: $((x++))
done

View file

@ -0,0 +1,5 @@
x=${#alist[*]} # start with the number of items in the array
while [[ $x > 0 ]]; do # while there are items left
: $((x--)) # decrement first, because indexing is zero-based
echo "Item $x = ${alist[$x]}" # show the current item
done

View file

@ -0,0 +1 @@
alist[${#alist[*]}]=new_item

View file

@ -0,0 +1,10 @@
# shell function to append values to an array
# push LIST VALUES ...
push() {
local var=${1:?'Missing variable name!'}
shift
eval "\$$var=( \"\${$var[@]}\" \"$@\" )"
}
push alist "one thing to add"
push alist many words to add

View file

@ -0,0 +1 @@
unset alist[0]

View file

@ -0,0 +1,23 @@
# pop ARRAY -- pop the last item on ARRAY and output it
pop() {
local var=${1:?'Missing array name'}
local x ; eval "x=\${#$var[*]}"
if [[ $x > 0 ]]; then
local val ; eval "val=\"\${$var[$((--x))]}\""
unset $var[$x]
else
echo 1>&2 "No items in $var" ; exit 1
fi
echo "$val"
}
alist=(a b c)
pop alist
a
pop alist
b
pop alist
c
pop alist
No items in alist

View file

@ -0,0 +1 @@
unset alist[*]

View file

@ -0,0 +1,6 @@
int[] array = new int[10];
array[0] = 1;
array[1] = 3;
stdout.printf("%d\n", array[0]);

View file

@ -0,0 +1,8 @@
var array = new ArrayList<int> ();
array.add(1);
array.add(3);
array[0] = 2;
stdout.printf("%d\n", array[0]);

View file

@ -0,0 +1,78 @@
section .text
global _start
_print:
mov ebx, 1
mov eax, 4
int 0x80
ret
_start:
;print out our byte array. ergo, String.
mov edx, sLen
mov ecx, sArray
call _print
mov edx, f_len
mov ecx, f_msg
call _print
mov edx, 6 ;our array members length.
xor ecx, ecx
mov ecx, 4
;turnicate through the array and print all it's members.
;At an offset of *4, each array member is referenced
;at 1,2,3 and so on.
_out_loops:
push ecx
mov ecx, [fArray+esi*4]
call _print
inc esi
pop ecx
loop _out_loops
mov edx, u_len
mov ecx, u_msg
call _print
;Let's populate 'uArray' with something from sArray.
;mov edi, uArray
mov ecx, 4
xor esi, esi
_read_loops:
push dword [fArray+esi*4]
pop dword [uArray+esi*4]
inc esi
loop _read_loops
mov ecx, 4
xor esi, esi
_out_loops2:
push ecx
mov ecx, [uArray+esi*4]
call _print
inc esi
pop ecx
loop _out_loops2
push 0x1
mov eax, 1
push eax
int 0x80
section .data
sArray db 'a','r','r','a','y','s',' ','a','r','e',' ','f','u','n',0xa
sLen equ $-sArray
crap1 db "crap1",0xa
crap2 db "crap2",0xa
crap3 db "crap3",0xa
crap4 db "crap4",0xa
fArray dd crap1,crap2
dd crap3,crap4
f_msg db "fArray contents",0xa,"----------------------",0xa
f_len equ $-f_msg
u_msg db "uArray now holds fArray contents.. dumping..",0xa,"----------------------",0xa
u_len equ $-u_msg
section .bss
uArray resd 1
resd 1
resd 1
resd 1

View file

@ -0,0 +1,8 @@
include c:\cxpl\codes;
char A(10); \creates a static array of 10 bytes, pointed to by "A"
char B; \declares a variable for a pointer to a dynamic array
[A(3):= 14;
B:= Reserve(10); \reserve 10 bytes and point to their starting address
B(7):= 28;
IntOut(0, A(3)+B(7)); \displays 42
]