Core Function List

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(3 intermediate revisions by one user not shown)
Line 43: Line 43:
  
 
=== Example ===
 
=== Example ===
 +
 +
Get array items
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
Line 68: Line 70:
 
say $a; // 15
 
say $a; // 15
 
say $b; // 25
 
say $b; // 25
 +
</syntaxhighlight>
 +
 +
Example of making List() of the array using Keys instead of Values to order the list
 +
 +
<syntaxhighlight lang="sputnik">
 +
// 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
 +
</syntaxhighlight>
 +
 +
Another key example
 +
 +
<syntaxhighlight lang="sputnik">
 +
// 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
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 16:19, 19 August 2014

<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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox