Last time I went though scalars and how to use operators. Today is arrays, hashes and lists.
As from last time all my variables will now be prefixed with my to help with debugging and general flow. The usefulness of my will become apparent when we explore loops.
Lists
Lists are quick ways of defining scalars.
1
2
3
4
5
|
use strict;
my ($a, $b, $c) = (1,2,3);
print $a; #Prints out 1.
my ($d, $e, $f) = ("Socks", "Milly", "Poppy");
print $f; #Prints out Poppy.
|
This is essentially quicker than declaring $a = 1, $ a=2 etc.
Some functions for strings:
1
2
3
4
5
6
7
8
9
10
|
use strict;
my ($a, $b, $c) = ("Socks", "Milly", "Poppy");
#Join, Merges strings
my $newstring = join ( ", " , $a,$b,$c);
print $newstring; #Prints "Socks, Milly, Poppy"
#Reverse, reverses the string
my $d = reverse ($a);
print $d; #Prints "skcoS"
|
Arrays – @
Arrays are an ordered set of strings. These are very useful for calling known values.
To define an array use my @arrayname. To access a single element in an array use $arrayname[number]. All arrays are zero numbered so 0 is the start number. Perl will automatically switch between arrays and scalars as needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/usr/bin/perl -w
use strict;
my @cats = ("Socks" ,"Milly" , "Poppy");
print @cats; #Will print "SocksMillyPoppy"
print $cats[2]; #Prints Poppy.
my @cats2;
$cats2[0] = "Socks";
$cats2[1] = "Milly";
$cats2[2] = "Poppy";
print @cats2; #Will print "SocksMillyPoppy"
my $favcat = $cats2[0]; #You can save elements to scalars if you need to.
#List context
my ($a, $b, $c, $d) = @cats;
print $a; #Prints out Socks.
#print $d; Will cause the an error since $d is undefined and uninitialised.
my ($e, $f, $g, @h) = (@cats, @cats2);
|
As you can see there is “More than one way to do it.” The top example @cats shows the more compact way, while @cats2 is a bit easier to read, you may choose to do whatever is the easiest for you.
The final lines above shows that you can create lists based on arrays. Declaring $a, $b and $c will implicitly call $cats[0], $cats[1] and $cats[2]. However, if an element doesn’t exist in an array it will be undefined.
The final line shows how Perl fills up lists from arrays. First it fills $e, $f and $g with the values in @cats. After that @h fills up with values from @cats2. This could be extended to catch values that you may not have expected however, that isn’t relevant now.
Functions for arrays and lists are the same as for strings.
Some examples of functions being used:
1
2
3
4
5
6
7
8
9
10
11
12
|
use strict;
my @cats = ("Socks" ,"Milly" , "Poppy");
my @cats2;
$cats2[0] = "Rosey";
$cats2[1] = "Sparrow";
$cats2[2] = "Patch";
print reverse (@cats); #Prints reversed order @cats
print sort (@cats2); #prints a sorted list of @cats2
my @sortedcats = sort {$b cmp $a} (@cats); #Use a condition to sort @cats, descending order here
print @sortedcats;
|
However there are Array based functions as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#!/usr/bin/perl -w
use strict;
my @cats = ("Socks" ,"Milly" , "Poppy");
my @cats2;
$cats2[0] = "Rosey";
$cats2[1] = "Sparrow";
$cats2[2] = "Patch";
#unshift
unshift @cats, "Oliver"; #Will add "Oliver" to the beginning of @cats
print @cats;
#Default output of unshift is to return the length of the new array
#push
push @cats2, ("Megan" , "Tiddles"); #Will add "Megan" and "Tiddles" to the end of @cats2
print @cats2;
#Default output of push is to return the length of the new array
#pop
print pop @cats; #Will remove the last element.
#shift
print shift @cats; #Will remove the first element.
#delete
print delete $cats2[2]; #Deletes and returns the 3th element.
#exists
print exists $cats[12]; #Check if 13th element exists returns 1 if true. Returns nothing here (false)
#splice (@array,offset,length,list)
my @names = ("Ben", "Thomas" , "Joe");
my @names2 = ("James", "Harry", "Dave");
my @splicednames = splice (@names,1,3,@names2); #Will remove elements 1, 2 and 3 in @names and insert @names2
print @splicednames; #Stores the spliced out names
print @names; #Newly spliced array
|
Hashes – %
Hashes can be thought of as an unordered set of data accessed by keys. Define a hash with %hashname and access elements in hashes by calling the “key” $hashname{key}. Keys must be unique. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/usr/bin/perl -w
use strict;
#1
my %cats = ( "Joe's" , "Socks" , "Matt's" , "Poppy", "Pete's", "Milly");
#2
my %cats2 = (
"Joe's" => "Socks",
"Matt's"=> "Poppy",
"Pete's"=> "Milly"
);
#3
my %cats3;
$cats3{"Joe's"} = "Socks";
$cats3{"Matt's"}= "Poppy";
$cats3{"Pete's"}= "Milly";
print $cats3{"Joe's"}; #Calling a key, prints "Socks"
|
Whatever way you want to define a hash is fine but #2 is probably the easiest to read and type.
Hash functions work like array functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
use strict;
my %cats = (
"Joe's" => "Socks",
"Matt's"=> "Poppy",
"Pete's"=> "Milly"
);
#Delete removes "Socks" from the hash
print delete ($cats{"Joe's"}); # Returns "Socks"
#Each returns each key value pair
print each (%cats); #Will choose a "random" key pair since Perl is unordered
#Exists, checks for existence of a key or value
exists($cats{"Socks"});
#Keys, Returns just the keys
print keys (%cats); # Returns "Matt'sPete's"
#Values, Returns values from a hash
print values (%cats); #Returns "PoppyMilly"
|
So there we have a quick run down on how hashes work.
Have fun!