"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - Arrays

Arrays

An Array is a variable containing series of data elements. Each element in this variable can be accessed by an index number or the string key this means arrays can store multiple variables in a single variable accessible by it's [] index.

Arrays operate very much like PHPs in that it is an array and a dictionary at the same time.

The index can be numeric or a string so an array is basically an optimized ordered dictionary.

To learn more about the dictionary based features go here.

In this Sputnik documentation, you may notice arrays being created using different notations: array(...) and [...]. Both methods are equally valid and yield the same result. The array(...) syntax serves as a historical artifact from the time before Sputnik implemented the usage of square brackets [ ] as a standalone array constructor. Despite this distinction, developers are encouraged to use either notation interchangeably, as both approaches are considered valid and will continue to be supported in Sputnik. This flexibility allows users to choose the array notation that aligns with their preferences or existing code practices. In a side note, it's important to highlight that in Sputnik, no features are ever deleted, and nothing is deprecated. Rest assured that once a feature is introduced, it remains supported, ensuring consistency and backward compatibility across different versions of the language.

Lets make sure we understand what a List array is and an Associative (dictionary) array is:

my $a = array("cat", "dog");
my $b = array("cat" => "dog");

printr $a;
printr $b;

say "Is A list? " . IsList($a);
say "Is B list? " . IsList($b);

// Array
// (
//     [0] => cat
//     [1] => dog
// )
// Array
// (
//     [cat] => dog
// )
// Is A list? true
// Is B list? false

The IsList function in Sputnik is used to determine whether an array is a list or an associative array. It examines the keys of the array to make this determination.

In the provided example, we have two arrays: $a and $b.

$a is a list array since its keys are consecutive integers starting from 0 and counting upwards. When we pass $a to the IsList function, it returns true.

$b, on the other hand, is an associative array. It has a key-value pair where the key is "cat" and the value is "dog". Since it has a string key, it is considered an associative array. When we pass $b to the IsList function, it returns false.

By examining the keys of an array, the IsList function helps differentiate between list arrays and associative arrays, enabling you to handle them differently based on their structure and purpose.

Let's say you want to store these series of characters: "S", "P", "U", "T", "N", "I", "K" and "1".
You could use seven separate variables to do so, but using an Array is more efficient:

my $array = array("S", "P", "U", "T", "N", "I", "K", 1);
// or
my $array
$array[0] = "S";
$array[1] = "P";
$array[2] = "U";
$array[3] = "T";
$array[4] = "N";
$array[5] = "I";
$array[6] = "K";
$array[7] = "1";

To access a specific value in an Array, you only have to know the index number:

my $char = $array[2];

This results in $char containing the letter "U" (See also: 'operators').

The index number can also be substituted by another variable or an expression, so you can build complex ways to assign or access elements in an array.

Its important we learn how to access array elements from strings properly.

You can access array elements from strings like so:

my $array = array("S", "P", "U", "T", "N", "I", "K", 1);

say "Value is $array[0]";
// PRINTS
// Value is S

my $index = 1;
say "Value is $array[$index]";
// PRINTS
// Value is P

my $index = 1;
say "Value is $array[$index + 1]";
// PRINTS
// Value is U

my $index = 2;
say "Value is $array[3 + 1]";
// PRINTS
// Value is N

Spunik tries to read whats inside the bracket and figure out if its a string or integer or should be code.

It uses a few clues to try figure it out obviously if its digits it knows its an integer and if its non-digits it starts to suspect its a string but not straight away. It first tests if it contains a-z A-Z 0-9 along with the ANY of the following characters '_', ' ', ',', '+', '-', '*', '/', '%', ',', '^', '&', '|', '>', '<', '?' and if its a match then it executes it as physical code and not a string so for example $array[3 + 7] would be executed as the code as 3 + 7 and not a string like "3 + 7". However what if you really want to access an element located at a string key called "3 + 7" well for that case you can prefix the code with a '#' which is the stringify operator for the string parser like so $array[#3 + 7] this works because its illegal to use # anywhere in Sputnik code unless its on a new line all by itself therefor the parser immediately knows what you mean when it sees it.

my $array = array("3 + 7" => 10, 10 => "oh no");
say "Value correctly is $array[#3 + 7]";
say "Value incorrect is $array[3 + 7]";
// PRINTS
// Value correctly is 10
// Value incorrect is oh no

Notice the correct one worked since the # is telling Sputnik you want to stringify the bracket thus it turns into "3 + 7" where as the bottom one is executing it as code instead thus it comes out as 3 + 7 which resolves to an integer 10 which is read as "oh no" in the array is clearly not what we want.

Just like you can force reading the bracket as a string using # you can also force reading it as code by using ## for example:

my $array = array(10 => "correct");
say "Value is $array[##3 + 7]";
// PRINTS
// Value is correct

The ## forces the bracket to be read as physical code similar to having { } in the string.

What if you want to string string keys but dont want to prefix them:

my $array = array("cat" => "meow", "dog" => "woof");
say "Value is $array[cat]";
say "Value is $array[dog]";
// PRINTS
// Value is meow
// Value is woof

This works because cat and dog are read as "cat" and "dog" because they dont contain any special symbols that would force the string parser to assume its code. If your keys do indeed contain code then you must stringify them like so:

my $array = array("cat^1" => "meow", "dog^1" => "woof");
say "Value is $array[#cat^1]";
say "Value is $array[#dog^1]";
// PRINTS
// Value is meow
// Value is woof

Alternatively you can put them in quotations like so:

my $array = array("cat" => "meow", "dog" => "woof");
say "Value is $array['cat']";
say "Value is $array['dog']";
// PRINTS
// Value is meow
// Value is woof

Bare in mind you can also do do these:

my $array = array("cat" => "meow", "dog" => "woof");
say "Value is $array[\"cat\"]";
say "Value is $array[\'cat\']";
// PRINTS
// Value is meow
// Value is meow
// Value is meow
// Value is meow

They will be read as it was ' '.

A key caveat to keep in mind is that when doing quotations like this the end result is stringified as @"cat" so its a static string this means it does not further parse the string. Because array accessing inside strings is fairly limited compared to doing it outside the string however there is workaround.

Arrays can also be multi dimensional, when you use multiple series of index numbers, like:

// Make a blank array
my $array = array();
// Fill it with data
$array[0][0] = "Upper-Left";
$array[1][0] = "Lower-Left";
$array[0][1] = "Upper-Right";
$array[1][1] = "Lower-Right";
// Print it
printr $array;
// PRINTS
// Array
// (
//     [0] => Array
//         (
//             [0] => Upper-Left
//             [1] => Upper-Right
//         )
//     [1] => Array
//         (
//             [0] => Lower-Left
//             [1] => Lower-Right
//         )
// )

Alternative method using , for example:

// Make a blank array
my $array = array();
// Fill it with data
$array[0, 0] = "Upper-Left";
$array[1, 0] = "Lower-Left";
$array[0, 1] = "Upper-Right";
$array[1, 1] = "Lower-Right";
// Print it
printr $array;
// PRINTS
// Array
// (
//     [0] => Array
//         (
//             [0] => Upper-Left
//             [1] => Upper-Right
//         )
//     [1] => Array
//         (
//             [0] => Lower-Left
//             [1] => Lower-Right
//         )
// )

Using IndexOfValue to do multi-dimensional scanning:

my $a = array("CatX", array("cat" => "meow", "FoX"), "Dog");
printr IndexOfValue($a, "Cat");
printr IndexOfValue($a, "Dog");
printr IndexOfValue($a, "FoX", @InArrayRecursive);
printr IndexOfValue($a, "meow", @InArrayRecursive);
my $meowIndex = IndexOfValue($a, "meow", @InArrayRecursive);
say "Meow : " . $a[**$meowIndex];
say "Meow : " . $a[1, "cat"];
// PRINTS
// 2
// Array
// (
//     [0] => 1
//     [1] => 0
// )
// Array
// (
//     [0] => 1
//     [1] => cat
// )
// Meow : meow
// Meow : meow

You can also use string keys instead of index numbers when using an array example:

my $array = array("The", "FoX", "Cat" => "Meow");
$array["Kitten"] = "Cute";
say $array["Kitten"]; // Cute
printr $array;
// PRINTS
// Cute
// Array
// (
//     [0] => The
//     [1] => FoX
//     [Cat] => Meow
//     [Kitten] => Cute
// )

(These values are just examples)

You can as many dimensions in an Array as your ram can support. The total number of entries cannot be greater than 2^24 (16 777 216) per array however you can get around this limit by placing arrays inside arrays.

Sputnik arrays are dynamic and unlike other languages you do not need to redim (although if you want to use Dim and Redim you surely can) them instead of they resize/change on the fly you can just add/remove elements at any point.

You can place anything you wish into an Array. An SV in Sputnik can contain anything from a number to a boolean value. So a Sputnik-Array (AV as it is called internally) could also contain different types, even other Arrays:

my $array = array("The", "FoX", "Cat" => "Meow");
my $array2 = array("Uber" => "FoX", "Testy", $array);
printr $array2;
// PRINTS
// Array
// (
//     [0] => Testy
//     [1] => Array
//         (
//             [0] => The
//             [1] => FoX
//             [Cat] => Meow
//         )
//     [Uber] => FoX
// )

Heres a another simple example:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4); 
// Print the details of the array
say "array : " . $array;
say "array[0] : " . $array[0];
say "array[1] : " . $array[1];
say "array[2] : " . $array[2];
say "array[3] : " . $array[3];
// A better way to print an array
printr $array;
// PRINTS
// array : {ARRAY;Elements:4}
// array[0] : 1
// array[1] : 2
// array[2] : 3
// array[3] : 4
// Array
// (
//     [0] => 1
//     [1] => cat
//     [2] => 3
//     [3] => 4
// )

To set the last element (or any elemeny starting to the end) you can use the ^ operator for example:

my $array = array(1, 2, 3, 4); 
$array[^1] = 100;
printr $array;
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 100
// )

The ^1 means end index so ^2 would mean second to end and so on.

Arrays can be defined using [] instead of array() for example:

// Create a simple array with 4 elements
my $array = [1, "cat", 3, 4];
// Print the details of the array
printr $array;
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 4
// )

An example of adding stuff to end and beginning of an array:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4);
// Add 2 Elements to end of the array
$array .= array(5, 6);
// Add 1 Element to start of the array
$array ..= array(0); 
// Print the details of the array
say "array : " . $array;
say "array[0] : " . $array[0];
say "array[1] : " . $array[1];
say "array[2] : " . $array[2];
say "array[3] : " . $array[3];
say "array[4] : " . $array[4];
say "array[5] : " . $array[5];
say "array[6] : " . $array[6];
// PRINTS
// array : {ARRAY;Elements:7}
// array[0] : 0
// array[1] : 1
// array[2] : 2
// array[3] : 3
// array[4] : 4
// array[5] : 5
// array[6] : 6

To delete an array you can simply define it as anything including strings but the best way is:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4);
// Delete the array
$array = array(); 
// Print the details of the array
say "array : " . $array;
say "array[0] : " . $array[0];
say "array[1] : " . $array[1];
// PRINTS
// array : {ARRAY;Elements:0}
// array[0] : 
// array[1] : 

Copying an array is simple too however if you come from PHP you might expect this to work::

$array = array("cat", "dog");
$arrayCopy = $array; // would copy array in PHP but NOT Sputnik
$arrayCopy .= "fox";
printr $array;
printr $arrayCopy;
// PRINTS
// Array
// (
//     [0] => cat
//     [1] => dog
//     [2] => fox
// )
// Array
// (
//     [0] => cat
//     [1] => dog
//     [2] => fox
// )

As you can see it merely made uses the same internal array as the original so editing one will effect the other (since they are the same). It is NOT a reference and instead both variables share the SAME internal array.

To truly copy an array you must use the Clone function example:

$array = array("cat", "dog");
$arrayCopy = clone($array); // properly copy the array
$arrayCopy .= "fox";
printr $array;
printr $arrayCopy;
// PRINTS
// Array
// (
//     [0] => cat
//     [1] => dog
// )
// Array
// (
//     [0] => cat
//     [1] => dog
//     [2] => fox
// )

That was a full COPY of the array changing elements in one array will not effect the other unless your first array contained a reference to the other like so:

$array = array("cat", "dog");
$arrayRef = &$array; // reference the other
*$arrayRef .= "fox";  // use * to dereference the pointer
printr $array;
say $arrayRef; // will only print the ref
printr *$arrayRef; // use * to dereference the pointer
// PRINTS
// Array
// (
//     [0] => cat
//     [1] => dog
//     [2] => fox
// )
// {Ref;Depth=1}
// Array
// (
//     [0] => cat
//     [1] => dog
//     [2] => fox
// )

But we will get into references later.

Be careful when pulling arrays out of arrays since it will be linked to original and any changes will affect the original as shown here:

$a = ["cat" => ["dog"]];
my $b = $a["cat"];
$b .= "fox";
printr $a;

// PRINTS
// Array
// (
//     [cat] => Array
//         (
//             [0] => dog
//             [1] => fox
//         )
// )

Make sure to use clone if you wish to extract it without being linked to the original like so:

$a = ["cat" => ["dog"]];
my $b = clone($a["cat"]);
$b .= "fox";
printr $a;

// PRINTS
// Array
// (
//     [cat] => Array
//         (
//             [0] => dog
//         )
// )

The fact it works this way is useful when sending this stuff into a function since it could modify the original which may be what you want.

Arrays can also be modified directly example:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4);
// Modify it
$array[2] = "Hello";
$array[1] = "hmmm";
$array[99] = "hi there!"; 
// Print the details of the array
say "array : " . $array;
say "array[0] : " . $array[0];
say "array[1] : " . $array[1];
say "array[2] : " . $array[2];
say "array[98] : " . $array[98];
say "array[99] : " . $array[99];
printr $array;
// PRINTS
// array : {ARRAY;Elements:5}
// array[0] : 1
// array[1] : hmmm
// array[2] : Hello
// array[98] : 
// array[99] : hi there!
// Array
// (
//     [0] => 1
//     [1] => hmmm
//     [2] => Hello
//     [3] => 4
//     [99] => hi there!
// )

In the above example you can see simply typing $var[index] = will cause it to create that index (part of the array) and fill it with what you type, The array will create the new keys (indexes) as you use them you will notice that index 4 to 98 are missing? This is because Sputnik arrays only create keys if they need to and will never fill in gaps for you. Note when you create elements like 98 99 in middle of nowhere the array is no longer considered a list. However you can still use it as a list it just means functions that require an IsList compatibility will refuse to cooperate. You can use Order to snap an array back into list form.

You can of course force it to fix the array indexes so it goes from 0 upwards properly like:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4);
// Modify it
$array[2] = "Hello";
$array[1] = "hmmm";
$array[99] = "hi there!"; 
// Print the details of the array
order $array; // Smooth out the array
printr $array;
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => hmmm
//     [2] => Hello
//     [3] => 4
//     [4] => hi there!
// )

Add items to end of an array like:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4);
// Add elements to the end of the array
$array[] = 5;
$array[] = 6;
$array[] = 7;
 // Print the details of the array
printr $array;
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
//     [3] => 4
//     [4] => 5
//     [5] => 6
//     [6] => 7
// )

Add items to beginning of an array like:

// Create a simple array with 4 elements
my $array = array(1, 2, 3, 4);
// Add elements to the end of the array
$array[] = 5;
$array[] = 6;
$array[] = 7;
$array![] = 0;
 // Print the details of the array
printr $array;
// PRINTS
// Array
// (
//     [0] => 0
//     [1] => 1
//     [2] => 2
//     [3] => 3
//     [4] => 4
//     [5] => 5
//     [6] => 6
//     [7] => 7
// )

In the above example you can see simply typing $var[index] = will cause it to create that index (part of the array) and fill it with what you type, The array will be automatically expanded to the size you request all the elements in the gap that it fills in will be empty strings ready for use (As seen in index 98).

You can also use any operator such as += on arrays too for example:

$a = array(10, 20, 30, 40, 50, 60);
$b = array(1, 2, 3, 4, 5, 6);
$a += $b;
printr $a;
// PRINTS
// Array
// (
//     [0] => 11
//     [1] => 22
//     [2] => 33
//     [3] => 44
//     [4] => 55
//     [5] => 66
// )
Another example:
$a = [10, 20, 30, 40, 50, 60];
$b = [1, 2, 3, 4, 5, 6];
$a += $b;
$a += [100, 200, 300];
printr $a;
// PRINTS
// Array
// (
//     [0] => 111
//     [1] => 222
//     [2] => 333
//     [3] => 44
//     [4] => 55
//     [5] => 66
// )
Notice this one uses [ ] brackets instead of Array keyword? There is no differance between the two it simply a style choice.

You can also do Multi-dimensional arrays like:

// Make an array
my $array = array(0 => array(3, 4), 1 => array(3 =>array(8, 9)));
// Set a value in it
$array[0][0] = "Hello";
 // Print the details of the array
say "array : " . $array;
say "array[0] : " . $array[0];
say "array[0][0] : " . $array[0][0];
say "array[0][1] : " . $array[0][1];
say "array[1][3] : " . $array[1][3];
say "array[1][3][0] : " . $array[1][3][0];
say "array[1][3][1] : " . $array[1][3][1];
// PRINTS
// array : {Array;Elements:2;Bottom:0;Top:2}
// array[0] : {Array;Elements:2;Bottom:0;Top:2}
// array[0][0] : Hello
// array[0][1] : 4
// array[1][3] : {Array;Elements:2;Bottom:0;Top:2}
// array[1][3][0] : 8
// array[1][3][1] : 9
Multi-dimensional arrays can be accessed using commas instead of repeated brackets too like so:
// Make an array
my $array = array(0 => array(3, 4), 1 => array(3 =>array(8, 9)));
// Set a value in it
$array[0, 0] = "Hello";
 // Print the details of the array
say "array : " . $array;
say "array[0] : " . $array[0];
say "array[0, 0] : " . $array[0, 0];
say "array[0, 1] : " . $array[0, 1];
say "array[1, 3] : " . $array[1, 3];
say "array[1, 3, 0] : " . $array[1, 3, 0];
say "array[1, 3, 1] : " . $array[1, 3, 1];
// PRINTS
// array : {Array;Elements:2;Bottom:0;Top:2}
// array[0] : {Array;Elements:2;Bottom:0;Top:2}
// array[0, 0] : Hello
// array[0, 1] : 4
// array[1, 3] : {Array;Elements:2;Bottom:0;Top:2}
// array[1, 3, 0] : 8
// array[1, 3, 1] : 9

You can also access them inside strings the same way:

// Make an array
my $array = array(0 => array(3, 4), 1 => array(3 =>array(8, 9)));
// Set a value in it
$array[0, 0] = "Hello";
 // Print the details of the array
say "array : $array";
say "array[0] : $array[0]";
say "array[0][0] : $array[0][0]";
say "array[0][1] : $array[0][1]";
say "array[1][3] : $array[1][3]";
say "array[1][3][0] : $array[1][3][0]";
say "array[1][3][1] : $array[1][3][1]";
// PRINTS
// array : {Array;Elements:2;Bottom:0;Top:2}
// array[0] : {Array;Elements:2;Bottom:0;Top:2}
// array[0][0] : Hello
// array[0][1] : 4
// array[1][3] : {Array;Elements:2;Bottom:0;Top:2}
// array[1][3][0] : 8
// array[1][3][1] : 9


// Make an array
my $array = array(0 => array(3, 4), 1 => array(3 =>array(8, 9)));
// Set a value in it
$array[0, 0] = "Hello";
 // Print the details of the array
say "array : $array";
say "array[0] : $array[0]";
say "array[0, 0] : $array[0, 0]";
say "array[0, 1] : $array[0, 1]";
say "array[1, 3] : $array[1, 3]";
say "array[1, 3, 0] : $array[1, 3, 0]";
say "array[1, 3, 1] : $array[1, 3, 1]";
// PRINTS
// array : {Array;Elements:2;Bottom:0;Top:2}
// array[0] : {Array;Elements:2;Bottom:0;Top:2}
// array[0, 0] : Hello
// array[0, 1] : 4
// array[1, 3] : {Array;Elements:2;Bottom:0;Top:2}
// array[1, 3, 0] : 8
// array[1, 3, 1] : 9

You can also spread an array out across many lines like:

// Use Brackets
my $array = [
                    0 => [3, 4],
                    1 => [
                                3 =>[8, 9]
                                ]
                ];
printr $array;
// PRINTS
// Array
// (
//     [0] => Array
//         (
//             [0] => 3
//             [1] => 4
//         )
//     [1] => Array
//         (
//             [3] => Array
//                 (
//                     [0] => 8
//                     [1] => 9
//                 )
//         )
// )
As you can see [ ] brackets are purely a style thing.

If you want to place an array inside an array when creating it you do:

my $b = array( "cat", "dog" );
my $a = array(  1, $b[<>], 3, 4  );
printr $a;
// So $a becomes array(  1, "cat", "dog", 3, 4  );
// If we didnt include the [<>] then $b itself would
// be inserted directly into $a rather than just copying
// its values
//
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => cat
//     [2] => dog
//     [3] => 3
//     [4] => 4
// )

Same as above but also using ** to expand the an array inside an array:

my $b = array( "cat", "dog" );
my $c = array( "fox", "rabbit" );
my $a = array(  1, $b[<>], 3, 4, **$c  );
printr $a;
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => cat
//     [2] => dog
//     [3] => 3
//     [4] => 4
//     [5] => fox
//     [6] => rabbit
// )

If you want to place an array inside an array (including its dictionary keys) when creating it you do:

my $b = array( "cat" => "meow", "dog" => "woof" );
my $a = array(  1, $b, 3, 4  );
printr $a; 
// So $a becomes array(  1, array( "cat" => "meow", "dog" => "woof" ), 3, 4  );
// If we didnt include the [<>] then $b itself would
// be inserted directly into $a rather than just copying
// its values
//
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => Array
//         (
//             [cat] => meow
//             [dog] => woof
//         )
//     [2] => 3
//     [3] => 4
// )

You can use [<=>] to do this so it places the elements directly instead of placing the array directly like:

my $b = array( "cat" => "meow", "dog" => "woof" );
my $a = array(  1, $b[<=>], 3, 4  );
printr $a; 
// So $a becomes array(  1, array( "cat" => "meow", "dog" => "woof" ), 3, 4  );
// If we didnt include the [<>] then $b itself would
// be inserted directly into $a rather than just copying
// its values
//
// PRINTS
// Array
// (
//     [0] => 1
//     [1] => 3
//     [2] => 4
//     [cat] => meow
//     [dog] => woof
// )

You can also use the qw// to produce simple arrays example:

// the qw// accepts only alphanumeric letters separated by spaces
my $array = qw/test omg lol/;
foreach(my $elem in $array)
{
    say "First test : " . $elem;
}
 // Is equal to
my $array = array("test", "omg", "lol");
foreach(my $elem in $array)
{
    say "Second test : " . $elem;
}
// PRINTS
// First test : test
// First test : omg
// First test : lol
// Second test : test
// Second test : omg
// Second test : lol

// Alternatively you could use:
// $arr = qw(test omg lol);
// $arr = qw{test omg lol};
// Or any other two symbols to define the start and ending

Array elements dont exist until you use a = or += etc etc on them so for example:

my $arr = array();
$a = $arr["cat"];
$b = $arr["dog"];
$a = 10;
$b = 10;
printr $arr;
// PRINTS
// Array
// (
// )
Notice the array was empty? This is because accessing elements in an array does not create them unless you forced its creation like so:
my $arr = array();
$arr["cat"] = 0;
$a = $arr["cat"];
$b = $arr["dog"];
$a = 10;
$b = 10;
printr $arr;
// PRINTS
// Array
// (
//    [cat] => 0
// )

We can use the [?] creation operator to force creating a key on access if we want to like so:

my $arr = array();
$a = $arr["cat"?];
$b = $arr["dog"?];
printr $arr;
// PRINTS
// Array
// (
//     [cat] => Array
//         (
//         )
//     [dog] => Array
//         (
//         )
// )

It created the elements now since ? causes it to create them if they dont exist however it creates them as an array just encase you wanted to do multiple ? ? ? statements.

To set the values after creatoin you could load them to a local variable as a pointer and set it like so:

my $arr = array();
$cat = &$arr["cat"?];
$dog = &$arr["dog"?];
*$cat = "Meow";
*$dog = "Woof";
printr $arr;
// Array
// (
//     [cat] => Meow
//     [dog] => Woof
// )
So what did I mean by ? ? ? well observe:
my $arr = array();
$arr["cat"?]["dog"?]["fox"?]["rabbit"?];
printr $arr;
// PRINTS
// Array
// (
//     [cat] => Array
//         (
//             [dog] => Array
//                 (
//                     [fox] => Array
//                         (
//                             [rabbit] => Array
//                                 (
//                                 )
//                         )
//                 )
//         )
// )
See how it creates all the depths? This could be handy for rapid creation of such things.

You can also compare arrays against each other and it will check them properly including recursively like:

// Compare the full array and all elements requiring a perfect match
if ($firstarray == $secondarray)
// Reverse of above
if ($firstarray != $secondarray)
// Compare the full array and all elements requiring a even more perfect match
if ($firstarray === $secondarray)
// Reverse of above
if ($firstarray !== $secondarray)
// Compare if first is lower than second
if ($firstarray < $secondarray)
// Compare if first is greater than second
if ($firstarray > $secondarray)
// Compare if first is lower than or equal to second
if ($firstarray <= $secondarray)
// Compare if first is greater than or equal to second
if ($firstarray >= $secondarray)
// Compare if first is lower or greater than second
if ($firstarray <> $secondarray)
// Returns first if lower than second
if ($firstarray <? $secondarray)
// Returns first if higher than second
if ($firstarray >? $secondarray)

// All the below ones do the comparison by checking all the arrays elements as if they were strings

// ompare the full array and all elements requiring a perfect match
if ($firstarray eq $secondarray)
// Reverse of above
if ($firstarray neq $secondarray)
// Compare the full array and all elements requiring a perfect match (case insensitive)
if ($firstarray eqi $secondarray)
// Reverse of above
if ($firstarray neqi $secondarray)
// Compare if first is lower than second
if ($firstarray lt $secondarray)
// Compare if first is greater than second
if ($firstarray gt $secondarray)
// Compare if first is lower than or equal to second
if ($firstarray le $secondarray)
// Compare if first is greater than or equal to second
if ($firstarray ge $secondarray)
// Compare if first is lower than second (case insensitive)
if ($firstarray lti $secondarray)
// Compare if first is lower than or equal to second (case insensitive)
if ($firstarray gti $secondarray)
// Compare if first is lower than or equal to second (case insensitive)
if ($firstarray lei $secondarray)
// Compare if first is greater than second (case insensitive)
if ($firstarray gei $secondarray)
// Compare if first is lower or greater than second
if ($firstarray lg $secondarray)
// Compare if first is lower or greater than second (case insensitive)
if ($firstarray lgi $secondarray)
// Tests if two arrays are lower(-1), equal (0) or higher (1)
if ($firstarray cmp $secondarray)
// Tests if two arrays are lower(-1), equal (0) or higher (1) (case insensitive)
if ($firstarray cmpi $secondarray)

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org