in reply to GP problem with tree structure using hash
If you add -w or use warnings to the top of your code you will get a whole heap of warning messages. Fixing these will get you a lot closer to seeing what is wrong with your code.
A few hints:
In your dec2bin sub, you are stripping leading zeros, but then relying upon having exactly 6 '1's or '0's when you later split the return value.
Instead, us substr to return just the part of the string you want:
sub dec2bin { my $str = unpack("B32", pack("N", shift)); # $str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros return substr( $str, -6 ); }
That will rid you of a couple of hundred runtime warnings.
There are a couple of places where you are trying to test a value for being undef using:
if( $value eq undef ) { ...
That is much better done as
if( defined $value ) { ...
You are building your %range hash in a sub, but the hash is defined outside. Effectively a global which generally a frowned upon practice.
The logic of your truth table is that values 8 .. 15, and 49, 51, 53, 55, 57, 59, 61, 63 are set to 1 and the rest 0. You may or may not see a way to use this information to do the initialisation in a better way?
When iterating over a simple range of integers in Perl, it's generally considered more readable and easier to code that as:
for my $i ( 0 .. 63 ) { ... }
but it's ultimately a personal preference thing.
To select a random node you could use:
$tree->{ ('left', 'right')[ rand 2 ] } { ('left', 'right')[ rand 2 ] } { ('left', 'right')[ rand 2 ] } = 'AND';;
Again, you may see a better way to code that.