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.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re: GP problem with tree structure using hash by BrowserUk
in thread GP problem with tree structure using hash by thealienz1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.