Last Updated: July 09, 2016
·
6.271K
· jmcveigh

Perl Sets

Description

This is an article written by Jason McVeigh of Orillia, ON, CA. This article was written in response to the blog entry and rapport with Gabor of Perl Maven Pro.

Sets

In computer programming, a set is a collection of distinct elements. Unique elements may be grouped into a set. Sets are represented in figures as Venn diagrams with each one depicted as a circle containing many elements. Sets are an often used data structure in real-world programming. Sets are readily available in the Perl programming language. This article is an exercise in sets, intuitive set theory and properties of sets.

Set Elements

If an element is a member of a set, then it is said that this element belongs to a set or that this element is in a set. Sets in Perl are used to hold items, data, tags, references or data structures. The order of the elements of a set is of zero consequence. A set may be considered an element as well.

This Article

With this article sets are defined then intuitive set theory to find the union, the intersection and the difference of example sets is completed in the Perl programming language. After, important properties of a set will be fetched using common Perl subroutines. In this article sets containing only strings will be used.

Equality

Two sets A and B are defined to be equal when they have precisely the same elements. If every element of A is an element of B and every element of B is an element of A.

Example Sets

In Perl, using an array to work with sets is well documented. Below, two arrays of unique elements are defined the Perl way. This code defines a set of blondes, identified as set A as well as a set of left-handed people, otherwise known as southpaws, identified as set B.

my @a = qw(Amber Micah Nicole Niomi Sarah Steven Alex Bruno Devin);
my @b = qw(Micah Sarah Steven Joey);

Union

Two sets may be added together. The union of A and B, is the set of all elements that are members of either A or B. The union of A and B, in this example, will be the addition of the set of blondes and the set of left-handed people. The following Perl source code will add set A to set B, fetching the union of A and B.

my (%union,@union);
foreach my $item ((@a,@b)) {
    $union{$item} = 1;
}
@union = keys %union;

Intersection

A new set can also be constructed by determining which members two sets have in common. The intersection of A and B, is the set of all elements that are members of both A and B. The intersection of A and B, in this example, will be the blondes whose dominant hand is the left. The following Perl source code will find the elements that set A and set B have in common, fetching the intersection of A and B.

my @isect;
foreach my $item (@a) {
    push @isect, $item if grep {  $item eq $_ } @b;
}

Difference

Two sets can also be subtracted to find the difference. The difference of A and B, is the set of all elements that are members of A but not members of B. The difference of A and B, in this example, will be the blondes who are either right-handed or have mixed-handedness but are not left-handed. Please note that subtracting members of a set that are not in the set both permitted and of zero consequence.

my @difference;
foreach my $item (@a) {
    push @difference, $item unless grep { $item eq $_ } @b;
}

Subroutines for Sets in Perl

The following are important subroutines used to work with sets. These subroutines expose important properties of a set to Perl programmers.

Create Set from Array

The createfrom subroutine will create a set of unique items from an array. The createfrom procedure is useful as it takes a plain-old array and creates a set of unique elements.

# usage: create_from(@array);
# returns @set
sub create_from {
    my @a = @_;
    my %seen = ();
    my @uniq;
    foreach my $item (@a) {
        push(@uniq, $item) unless $seen{$item}++;
    }
    return @uniq;
}

Is Element in Set?

The subroutine, commonly titled iselementof is used to check whether an element is in a set. This subroutine is written, using the Perl programming language, with the following lines:

# usage: is_element_of($element,@set);
# returns 1 if $element is a member of @set
sub is_element_of {
    my $item = shift;
    return 1 if grep { $item eq $_ }@_;
}

The Empty Set

The empty set is a set with no members at all. The empty set is a constant set. There is only one empty set. It is referred to as the empty set.

The empty set is returned with this subroutine. The empty_set subroutine is built with Perl as follows :

# usage: @e = empty_set();
# returns the empty set
sub empty_set {
    return [];
}

The subroutine, commonly titled is_empty checks whether the set is the empty set. This subroutine is written as follows :

# usage: is_empty(@set);
# returns 1 if @set is the empty set
sub is_empty {
    return 1 if scalar @_ == 0;
}

Cardinality

At last, the number of elements contained within a set is calculated with a subroutine such as this:

# usage: cardinality(@set);
# returns the cardinality of @set
sub cardinality {
    return scalar @_;
}

Summary

In summary, intuitive set logic will accomplish much and is entirely accessible to the Perl programmer. Sets are fundamental data structures in computer programming. In this article, intuitive set logic it was used for deduction. Use this acquired skill to enhance the business logic or functions of a Perl script.

2 Responses
Add your response

I made this!

over 1 year ago ·

I ♥ what I do and I hope you do too!

over 1 year ago ·