Tag Archives: Hash Trees

Converting Freemind Mind-maps Directly to Perl Hash Trees

freemind

I use Freemind quite a bit for brainstorming and as an outliner.  One of it’s better uses for me is to hammer out an idea for a perl hash tree very quickly.  The problem is that once I have the hash tree exactly the way I want it in Freemind, I have to manually re-create the hash tree in perl source, with all the required formatting.

This is no longer the case, as I’ve written a quick and dirty “freemind2perl” script (below) which takes a Freemind mind-map file, and converts it into a perl hash tree automagically.  I’m not sure if it will work with all versions of Freemind, but mind-map files (.mm files) are XML based, and the format really hasn’t changed across versions.

Just save the script below as ‘freemind2perl.pl’ and run it with ‘perl freemind2perl.pl yourmap.mm’.  It requires the “XML::Simple” perl module to be installed.

Here’s the script (click here to download):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl -w
 
use strict;
 
use XML::Simple;
use Data::Dumper;
 
my $xml = new XML::Simple;
my $mm_file = shift;
my $data = $xml->XMLin("$mm_file");
my $clean;
 
sub prep_clean
{
    my $data = shift;
    my $clean;
 
    foreach my $key ( keys %{ $data } )
    {
        if ( $key eq "TEXT" )
        {
            $clean->{$data->{$key}} = 1;
        }
 
        if ( $key eq "node" )
        {
            if ( ref( $data->{$key} ) eq "HASH" )
            {
                $clean->{$data->{'TEXT'}} = prep_clean(\%{$data->{$key}});
            }
 
            if ( ref( $data->{$key} ) eq "ARRAY" )
            {
                my $sub_hashes = {};
                for ( my $i = 0; $i <= $#{$data->{$key}}; $i++)
                {
                    foreach my $sub_hash ( \%{ $data->{$key}[$i] } )
                    {
                        my $subout = prep_clean( $sub_hash );
                        $sub_hashes = { %$sub_hashes, %$subout };
                    }
                }
                $clean->{$data->{'TEXT'}} = $sub_hashes;
            }
        }
    }
    return $clean;
}
 
$clean = prep_clean( \%{ $data->{'node'} } );
 
print Dumper(\$clean);
 
exit;