Perl programming Data Structures
Jump to navigation
Jump to search
Perl: Data Structures
The contents of this page has recently been imported from an older version of this Wiki. This page may contain outdated information, information that is irrelevant for this Wiki, information that needs to be differently structured, outdated syntax, and/or broken links. Use with caution!
A synopsis of simple and more advanced data structures in Perl.
Contents
Contents
- Simple and advanced data structures in Perl
Primitives
Data structure: scalar
- Sigil: "$"
- Type: Integer, float or string.
Data structure array
- Also called "list"
- Sigil: "@"
- Contains elements of any data type, or references to structures
#!/usr/bin/perl
# Array examples
use warnings;
use strict;
# Construct
my @a;
# Initialize:
# (use the split function to split a string into its letters.)
@a = split("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
# access elements by their index ...
print($a[1], $a[1], $a[2], $a[3], $a[5], $a[8], $a[13], $a[21], "\n");
# ... or from the back (note: last element is [-1], but first element is [0])
print($a[-1], $a[-1], $a[-2], $a[-3], $a[-5], $a[-8], $a[-13], $a[-21], "\n");
# print the entire array
print (@a, "\n");
# iterate over the array, element by element using foreach ...
foreach my $el (@a) {
print($el, " ");
}
print("\n");
# ... or using a for-loop
for (my $i = scalar(@a)-1; $i >= 0; $i--) {
print($a[$i]);
}
print("\n");
# push() and pop() operate on the end of an array;
# unshift() and shift() operate on the beginning of an array;
# add an element to the end of the array
push (@a, ")" );
# add an element to the beginning of the array
unshift (@a, "(" );
print(@a, "\n");
# access and remove elements from the beginning of an array with shift()
print ( shift(@a), ' ', @a, "\n" );
print ( shift(@a), ' ', @a, "\n" );
print ( shift(@a), ' ', @a, "\n" );
print ( shift(@a), ' ', @a, "\n" );
print ( shift(@a), ' ', @a, "\n" );
# access and remove elements from the end of an array with pop()
print ( pop(@a), ' ', @a, "\n" );
print ( pop(@a), ' ', @a, "\n" );
print ( pop(@a), ' ', @a, "\n" );
print ( pop(@a), ' ', @a, "\n" );
print ( pop(@a), ' ', @a, "\n" );
exit();
Data structure hash
- Also called "associative array"
- Sigil: "%"
- Contains key/value pairs
- keys are strings
- values are elements of any data type, or references to structures
#!/usr/bin/perl
# Hash examples
use warnings;
use strict;
# Construct
my %h;
# Initialize
# Iterate over the elements of a list. Every element is used
# as a key. If the key does not yet exist in the list, it is created.
# Then we increment the value. Incrementing an undefined value by one sets it to "1".
# In effect, this counts the number of occurrences of characters.
foreach my $k (split('', "entia non sunt multiplicanda praeter necessitatem")) {
$h{$k}++;
}
# access values by their key ...
print($h{"a"}, " ", $h{"e"}, "\n");
# print the entire hash
print (%h, "\n");
# iterate over the entire hash by key
foreach my $k ( keys(%h)) {
print("$k: $h{$k}\n");
}
print("\n====\n");
# print keys sorted by values - descending
# The sort function operates on a list. The input list is the result of the keys().
# The output is the list of keys, sorted by the result of comparing two values $h{$a} $h{$b}.
# Since we swap $a,$b in the comparison statement, we sort _descending_.
foreach my $k (sort { $h{$b} <=> $h{$a} } ( keys(%h) ) ) {
print("$k: $h{$k}\n");
}
print("\n");
exit();
Mixed structures
Two-dimensional array
Also referred to as an array of arrays.
#!/usr/bin/perl
use warnings;
use strict;
# Construct
my @a2 = ( () );
# SET
$a2[0][0] = 4;
$a2[0][1] = 5;
$a2[1][0] = "A";
$a2[1][1] = "B";
# GET - assignment
print ($a2[0][0] . " " . $a2[0][1] . "\n");
print ($a2[1][0] . " " . $a2[1][1]);
print "\n===\n";
# GET - iteration with foreach
foreach my $d1 ( @a2 ) {
print $d1 . " ";
}
print "\n===\n";
foreach my $d1 ( @a2 ) {
foreach my $d2 ( @{$d1}) {
print $d2 . " ";
}
print "\n";
}
print "\n";
exit();
Hash of hash of arrays
#!/usr/bin/perl
use warnings;
use strict;
#Hash of hash of arrays
#Construct
my %GO;
#SET values
$GO{"1"}{"child"}[0] = 1;
$GO{"1"}{"child"}[1] = 2;
$GO{"1"}{"child"}[2] = 3;
$GO{"1"}{"parent"}[0] = 4;
$GO{"1"}{"parent"}[1] = 5;
#GET values: assignment
my @t = @{$GO{"1"}{"child"}};
foreach my $node ( @t ) {
print $node . " ";
}
print "\n";
#GET values: foreach - evaluate in array context!
foreach my $node ( @{$GO{"1"}{"parent"}} ) {
print $node . " ";
}
print "\n";
exit();
Further reading and resources