Last week I needed a way of converting a two-dimensional array, in perl, into a tree-like datastructure.
While seemingly trivial, the convoluted use of references when passing around arrays and hashes between perl function calls -- recursive calls, in this case -- made this a mind-boggling exercise to me. (the fact that I hadn't seriously used perl in the last eight years might have had part in that, too)
Below the jump is my implementation, debugging info left in and all. If it's useful for someone, that's great; if not, ... well, except for my mental sanity no harm was done in writing this, either :)
use strict;
use warnings;
# Copyright (c) 2008, Filip Van Raemdonck -- http://www.sysfs.be/
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Sysfs nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Sysfs ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Sysfs BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Sample testing use: perl -e 'use MHash; test_tpush();'
sub tpush (@; $);
my $LOGLEVEL = 2;
sub DBUG {
my $verbosity = shift;
print @_ if ($verbosity >= $LOGLEVEL);
}
sub tpush (@; $) {
my @row = @{(shift)}; # list/array to process
my $parent = shift; # hash reference for parent node
my $lev = shift (@row);
my %thisnode;
my $yesnomsg = '';
if (exists ($parent->{$lev})) {
%thisnode = %{$parent->{$lev}};
} else {
$yesnomsg = ' not yet';
}
DBUG (1, "Currently at level $lev, which does$yesnomsg exist in parent hash (which has ".keys (%{$parent})." elements)\n");
if ($#row >= 0) {
tpush (@row, %thisnode);
DBUG (0, " have node with ".keys (%thisnode)." key(s)\n");
}
DBUG (1, "Pushing $lev onto our parent hash\n");
$parent->{$lev} = %thisnode;
}
sub dump_tree {
my %root = %{(shift)};
my $lev = shift;
DBUG (0, "Tree root has ".keys (%root)." elements\n");
foreach my $k (keys (%root)) {
DBUG (2, (" " x $lev)."$k:\n");# -> $root{$k} = [%{$root{$k}}]\n");
foreach my $v ($root{$k}) {
++$lev;
if ($v =~ m/^HASH\(/) {
dump_tree ($v, $lev);
} else {
DBUG (2, (" (nohash)" x $lev)."$v\n");
}
--$lev;
}
}
}
sub test_tpush {
my %root;
my @testlist = ('n0d:n1e:n2f', 'n0a:n1e:n2b:n3d', 'n0b:n1a', 'n0a:n1b:n2c:n3d:n4e', 'n0a:n1b:n2d');
my @testgrid;
for my $item (@testlist) {
my @templist = split (/:/, $item);
DBUG (1, @templist);
push (@testgrid, @templist);
}
for my $iref (@testgrid) {
tpush (@$iref, %root);
}
dump_tree (%root, 0);
}
1;