Sunday, 9 December 2012

A Look at Cartesian Products

Copyright 2012 by Shawn H Corey. Some rights reserved.
Licence under CC BY-SA 3.0

Problem: You have a number of sets and you want all the combinations when choosing one element from each set.

In mathematics, these combinations are called the Cartesian product. They are also known as cross-products. In the database world, they are sometimes called cross-joins.

This article is to show how to create them using Perl's glob function and how to create them in a subroutine.

Tuesday, 27 November 2012

A Look at Recursion

Copyright 2012 by Shawn H Corey. Some rights reserved.
Licence under CC BY-SA 3.0

At first, recursion seems to be magic. Break the problem into smaller tasks, call the same subroutine to process those sub-tasks, and voilĂ , problem solved. In truth, it's not quite so simple but all recursion has four steps. Master those steps and you can do recursion with ease.

Saturday, 24 November 2012

A Look at Arrays

Welcome to my blog on Perl. I'm starting it by reposting an article I wrote from blogs.perl.org: A Look at Arrays.

Perl's built-in datatype, array, is a multi-purpose tool which can be used for many things. It's primary purpose is to preserve the order of data. But it comes with a powerful set of tools to make manipulating it easy. This article is to show how to use these tools by implementing a linked list.

Linked lists were originally created for languages like C which only had simple datatypes. They are a technique for using the system's memory allocation to create a data structure that allows unlimited expansion, that is, until it runs out of memory. They use a simple datatype called a pointer that records a position in memory. Without pointers, linked list would be impossible.

Perl has a datatype similar to pointers, the reference. Like pointers, it records a memory location and it can be used link them. But using references to implement a linked list the same way one would do in C, needlessly complicates the code. Linked lists can be implemented using just arrays. Here's how.