"Sputnik" help  
Sputnik Help
<scope> List ( <args> ) = <variable>;
<scope> List ( <args> ) += <variable>;


This statement is used to unpack an array into variables in a variety of ways.


See Example.


The List Statement in Sputnik provides a powerful way to unpack arrays into variables, offering various options and functionalities for efficient assignment operations. It is commonly used in scenarios where array elements need to be assigned to individual variables or manipulated in a more granular manner.

The List Statement can be used to unpack array elements into variables, enabling direct access and manipulation. It offers several features, as demonstrated in the provided examples.

Firstly, the List Statement is often used in conjunction with the Foreach loop to unpack elements from an array. Within the loop, a List statement is used to assign array values to specific variables. This simplifies the code and enhances readability by providing meaningful variable names that correspond to the array elements being unpacked.

Additionally, the List Statement supports various operators, such as +=, enabling convenient operations on unpacked array elements. For example, you can use += to perform arithmetic operations or combine arrays, streamlining the code and avoiding the need for separate loops or assignments.

The List Statement also offers the flexibility to extract keys instead of values from an array. By using the "@" flag, you can retrieve the key names associated with the array elements, allowing for more precise data manipulation or indexing.

Nested List Statements provide a powerful way to work with complex data structures. You can nest multiple list statements within each other, creating hierarchical structures for efficient assignment operations. This enables the handling of multidimensional arrays or complex data formats with ease.

In addition to unpacking arrays, the List Statement can also be used in regex matching scenarios. It allows for the extraction of matched patterns into variables, facilitating further processing or analysis of the matched data.


Foreach Statement


Basic example of simply unpacking an array to individual variables:

my $array = array("Cat", "Dog");
my list($aniFirst, $aniSecond) = $array;
say "First=$aniFirst, Second=$aniSecond";
// First=Cat, Second=Dog

This statement can also be used to create variables that are null but exist in local scope for example:

my list($i, $j, $k);

Of course it can make them in global scope too:

global list($i, $j, $k);

These variables can have defaults too:

my list($i = 7, $j = array("Meow"), $k = "cat");
printr $i;
printr $j;
printr $k;
// 7
// Array
// (
//     [0] => Meow
// )
// cat

This statement has a rather extensive amount of features (It is even used by the Foreach loop) it is most commonly used to unpack elements from an array like so:

my $Accounts = array();
$Accounts[] = array("John", 1111);
$Accounts[] = array("Smith", 2222); 
foreach(my $Account in $Accounts)
    my List( $Name, $Password ) = $Account;
    say "Name '$Name' Password '$Password'";
// Name 'John' Password '1111'
// Name 'Smith' Password '2222'

Notice my can be placed before the List ? This will make sure variables it makes are local scope only.

A powerful features of the List is it can use pretty much any other operator such as += for example:

// Of course it can use all the operators
// well at least ones that end with a =
// but for this example we just use +=
my $a = 10;
my $b = 20;
my List ( $a, $b ) += array(5, 5);
say $a; // 15
say $b; // 25

This is similar to how normal Arrays can use any operator like this:

$a = array(10, 20, 30, 40, 50, 60);
$b = array(1, 2, 3, 4, 5, 6);
$a += $b;
printr $a;
// Array
// (
//     [0] => 11
//     [1] => 22
//     [2] => 33
//     [3] => 44
//     [4] => 55
//     [5] => 66
// )

You can also use the List statement to set multiple values to the same value instead of extracting bits from an array:

my List($a, $b, $c) = 10;
say $a;
say $b;
say $c;

// 10
// 10
// 10

Another powerful feature is making List extract Keys instead of Values from an array like:

// Create an array
my $array = array("Cat" => "Meow", "Dog" => "Woof");
// The @ flag is used to get the KEY instead of VALUE
my list( "@" $Dog, "@" $Cat ) = $array;
// Print the result
say "Cat: $cat"; // Meow
say "Dog: $dog"; // Woof
// Prints:
// Cat: Meow
// Dog: Woof

Another key example:

// Create the date array
my $date = Date("*t");
// Use @ flag to extract values from the array by KEY name
// instead of by index number like a normal List()
my List (   "@" $msec, "@" $sec, "@" $min, "@" $hour,
            "@" $day, "@" $month, "@" $year,
            "@" $wday, "@" $yday, "@" $isdst) = $date;
say "MSec: $msec";
say "Sec: $sec";
say "Min: $min";
say "Hour: $hour";
say "Day: $day";
say "Month: $month";
say "Year: $year";
say "WDay: $wday";
say "YDay: $yday";
say "IsDist: $isdst";
// MSec: 289
// Sec: 14
// Min: 34
// Hour: 22
// Day: 22
// Month: 4
// Year: 2018
// WDay: 7
// YDay: 365
// IsDist: true

One downside to using the keys is you have to set the variable name to the exact key so $Name wouldnt work here:

my $data = [
    "name" => "John",
    "age" => 25,
    "country" => "USA"

my list("@" $name, "@" $age, "@" $country) = $data;

say "Name: $name";
say "Age: $age";
say "Country: $country";
// Name: John
// Age: 25
// Country: USA

To solve this problem you can specifiy the key name along with the @ key option like so:

my $data = [
    "name" => "John",
    "age" => 25,
    "country" => "USA"

my list("@name" $n, "@" $age, "@country" $c) = $data;

print "Name: $n\n";
print "Age: $age\n";
print "Country: $c\n";

// Name: John
// Age: 25
// Country: USA

Notice now we can set the key name to exactly what it is while keeping the variable name however we want it to be.

Keep in mind however that the @ option must come last meaning if you want use a pointer it would need to be used like "&@Name" and not "@Name&".

When using @$Key the current value is NOT skipped. So for example when we do this below the @$Cat does not use up a value in the pool allowing $Other and $OtherAgain to take values as they normally would:

// Create an array
my $array = array("Cat" => "Meow", "Dog", "Fox");
my list( "@" $Cat, $Other, $OtherAgain, $OtherYetAgain) = $array;
say "Cat: $cat"; // Meow
say "Other: $Other";
say "OtherAgain: $OtherAgain";
say "OtherYetAgain: $OtherYetAgain";
// Prints
// Cat: Meow
// Other: Meow
// OtherAgain: Dog
// OtherYetAgain: Fox

It is interesting if you do @$0 example:

// Create an array
my $array = array("One", "Two", "Three");
my list("@" $0, $Val1, $Val2, $Val3) = $array;
say "0: $0";
say "Val1: $Val1";
say "Val2: $Val2";
say "Val3: $Val3";
// Prints
// 0: One
// Val1: One
// Val2: Two
// Val2: Three

This works because in the array element 0 is the key 0 so to acess it we can do @$0.

Nested list statements in Sputnik provide a powerful way to work with complex data structures and perform efficient assignment operations. Here's some additional information about nested list statements:

Here's an example that demonstrates the use of nested list statements:

my $a = 10;
my $b = 10;
my $c = 10;
my $d = 10;
list($a, list($b, $c), $d) += array(1, array(2, 3), 7);
say "First $a $b $c $d";

list($a, list($b, $c), $d) = array(1, array(2, 3), 7);
say "Second $a $b $c $d";

list($a, list($b, list($bb, $cc), $c), $d) = array(1, array(2, array(500, 1000), 3), 7);
say "Third $a $b $bb $cc $c $d";

// First 11 12 13 17
// Second 1 2 3 7
// Third 1 2 500 1000 3 7

With nested list statements, you can handle complex data structures and perform operations efficiently and conveniently in Sputnik.

Unpacking a regex match

my $a = array("One1", "Two77", "Three", "Four", "Five22", "Six");
foreach(list($v) in $a =~ m/(\d+)/)
    say "(First Loop) Found $v";
my $a = "One9-Two44-Three-Four-Five56-Six";
foreach(list($v) in $a =~ m/(\d+)/g)
    say "(Second Loop) Found $v";
// (First Loop) Found 1
// First Loop) Found 77
// First Loop) Found 22
// (Second Loop) Found 9
// (Second Loop) Found 44
// (Second Loop) Found 56

Another example of unpacking a regex match:

my $delimited = @"\G(.+)[\t\u007c](.+)";
my $input =     array(  "Mumbai, India|13,922,125",
                        "Shanghai, China\t13,831,900",
                        "Karachi, Pakistan|12,991,000",
                        "Dehli, India\t12,259,230",
                        "Istanbul, Turkey|11,372,613"
printf("Population of the World's Largest Cities, 2009\n");
printf(@"%-30s %s" . "\n", "City", "Population");
foreach( my list($m) in $input =~ m/$delimited/gk )
    my List( $City, $Pop ) = $m;
    printf("%-30s %s\n", $City, $Pop);
// Population of the World's Largest Cities, 2009
// City                           Population
// Mumbai, India                  13,922,125
// Shanghai, China                13,831,900
// Karachi, Pakistan              12,991,000
// Dehli, India                   12,259,230
// Istanbul, Turkey               11,372,613


Cheryl (uberfox@hotmail.com)