Core Function List
<Scope> List( <expressions>, .. ) = <array> <Scope> List( <expressions>, .. ) += <array> <Scope> List( <expressions>, .. ) *= <array> ...
Contents |
Description
Extract some elements from an array into ready to use variables.
Optionally can be used as += etc.
Scope
Optional; Can be nothing or can be "my" (without quotes)
Choices:
my - The local scope
global - The global scope
expressions
A list of variables to be created separated by ,
array
The array to use
Return Value
None
Remarks
The default scope will be "my" ie LOCAL scope only.
You may add "my" anyway just to future proof your code should the default ever change....
You can use Global if you want the variables to be accessible by everything anytime.
Example
Get array items
my $Accounts = array(); $Accounts[] = array("John", 1111); $Accounts[] = array("Smith", 2222); foreach($Accounts as my $Account) { my List( $Name, $Password ) = $Account; println( "Name '$Name' Password '$Password'" ); }
Example of using +=
// Of course it can use all the operators // 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
Example of making List() of the array using Keys instead of Values to order the list
// Create an array $a = array("Cat" => "Meow", "Dog"=>"Woof"); // @ is used to get the KEY instead of VALUE list( @$Dog, @$Cat ) = $a; // Print the result say "Cat: $cat"; // Meow say "Dog: $dog"; // Woof // Prints: // Cat: Meow // Dog: Woof
Another key example
// Create the date array $date = date("*t"); // Use @ to extract values from the array by KEY name // instead of by index number like a normal List() List (@$sec, @$min, @$hour, @$day, @$month, @$year, @$wday, @$yday, @$isdst) = $date; 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"; // Prints // Sec: 19 // Min: 17 // Hour: 17 // Day: 19 // Month: 8 // Year: 2014 // WDay: 2 // YDay: 231 // IsDist: true
When using @$Key the current value is 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 $a = array("Cat" => "Meow", "Dog", "Fox"); list( @$Cat, $Other, $OtherAgain ) = $a; say "Cat: $cat"; // Meow say "Other: $Other"; say "OtherAgain: $OtherAgain"; // Prints // Cat: Meow // Other: Dog // OtherAgain: Fox
Of course the interesting part about this is it will allow the @$KeyName value to get used up if you continue the List() long enough example
// Create an array $a = array("Cat" => "Meow", "Dog", "Fox"); list( @$Cat, $Other, $OtherAgain, $CatAgain ) = $a; say "Cat: $cat"; // Meow say "Other: $Other"; say "OtherAgain: $OtherAgain"; say "CatAgain: $CatAgain"; // Prints // Cat: Meow // Other: Dog // OtherAgain: Fox // CatAgain: Meow
This can be even more interesting if you do @$0 example
// Create an array $a = array("One", "Two", "Three"); list( @$0, $Val1, $Val2, $Val3 ) = $a; say "0: $0"; // One say "Val1: $Val1"; say "Val2: $Val2"; say "Val3: $Val3"; // Prints // 0: One // Val1: One // Val2: Two // Val2: Three