ARRAY NOTATION
my @array = (e, l ,e2, m, e3, n, t);
print $array[0];
prints “e”.
HASH NOTATION
my %hash = (key => element, key2 => element2);
print $hash{key};
prints “element”.
OOP
sub New {
my ($Caller, %Arguments) = @_;
my $Class = ref $Caller || $Caller;
my $Instance = bless { }, $Class;
}
EX:
In the constructor, new():
$Instance->{drool} = exists $Arguments{drool} ? $Arguments{drool} : 9001;
The accessors:
sub SetDrool {
my ($Instance, newValue) = @_;
$Instance->{drool} = newValue;
}
sub GetDrool { # ewwwwww
my $Instance = shift;
return $Instance->{drool}; # please don’t!
}
References:
my $arrayref = \@array;
my $hashref = \%hash;
my $functionref = \&subroutine;
You can omit the $ when dereferencing lists and hashes, functions are dereferenced with &$functionref. If preferred, dereference others with %$ and @$.
Elements of a list or hash:
1.
my $x = ${$hashRef}{'some-key'};
my $y = ${$listRef}[5];
2.
my $x = $hashRef->{'some-key'};
my $y = $listRef->[5];
my $z = $funcRef->();
Selection Sort
Find the minimum value in the list.
Swap it with the value in the first position.
Repeat the steps above for the remainder of the list (starting at the second position and advancing each time) .
Bubble Sort
Starting from the beginning of the list,compare every adjacent pair, swap their position if they are not in the right order(the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there is no more element left to be compared.
BFS
1 procedure BFS(Graph,source):
2 create a queue Q
3 enqueue source onto Q
4 mark source
5 while Q is not empty:
6 dequeue an item from Q into v
7 for each edge e incident on v in Graph:
8 let w be the other end of e
9 if w is not marked:
10 mark w
11 enqueue w onto Q
DFS
1 procedure DFS(G,v):
2 label v as explored
3 for all edges e in G.incidentEdges(v) do
4 if edge e is unexplored then
5 w ← G.opposite(v,e)
6 if vertex w is unexplored then
7 label e as a discovery edge
8 recursively call DFS(G,w)
9 else
10 label e as a back edge
Floyd-Warshall
Pretty complex, it just checks for the shortest path from vertex-to-vertex and takes the shortest route that it can.
Prim's Algorithm
This attempts to connect all the vertexes of a tree by selecting the cheapest NEIGHBOURING edge that doesn't create a loop.
Kruskal's Algorithm
This attempts to connect all the vertexes of a tree by selecting the cheapest edge OVERALL that doesn't cause a loop.
Filter/Grep:
Returns list of iteratable items that are true with a statement or subroutine.
Eg: find every line with the word “turtle”.
my @turtlesgalore = grep {m/turtle/} @lines.
Map:
If I had a list of @Goffs, and Depress() returns the volume of tears shed in cubic meters, then:
my @WetList = map {&Depress $_}, @Goffs;
Now “@Wetlist” contains the value of tears from each goff!