Scalar:
This type represent a single value:You need to declare it use my keyword:
>> my $name = "lady gaga";
>> print "The world is full of: ", $name;
>> print "$name: ".$name; # output: lady gaga: lady gagaArrays:
This represent a list of values:
>> my @name = ("lady gaga", "luna maya", "larry page");
>> my @id = (45, 98, 75, 66, 22);
>> my @mixed = ("lady gaga", 24);To access an element, use its index:
>> print $name[0];
>> print $name[$#name]; # print the last elementMore manouver:
>> @name[0,2]; # gives the 1 and the 3 element.
>> @name[0..2]; # gives the 1 to 3 elements
>> @name[0..$#name]; # gives the first till the last element>> my @sorted = sort @name # sorts elements
>> my @reverse = reverse @name # reverse elementTo iterate to all elements, use:
>> foreach(@name){>> print $_,"\n";
>> }
Hashes:
Hashes is key/value pairs. It's like assoc arrays in PHP:>> my %name = ("lady"=>"gaga",
>> "larry"=>"page",
>> "aura"=>"kasih");
To access an element, use
>> print $name{"lady"}; # curly bracket, not squareTo get the keys and values as an array, use:
>> my @fname = keys %name;>> my @lname = values %name;
To iterate to all elements of the map, use:
>> foreach my $key (keys %name){>> print "The value of $key is $name{$key}\n";
>> }
No comments:
Post a Comment