Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: GP problem with tree structure using hash

by BrowserUk (Patriarch)
on Oct 12, 2004 at 06:41 UTC ( #398383=note: print w/replies, xml ) Need Help??


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.


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://398383]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2023-02-01 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?