We are remodeling our house, and one of the projects is to replace the wooden balusters on our stair case with wrought iron. Due to new building codes, we have to go from 2 balusters/step to 3/step. Since all the stair treads are slightly different sizes, it's important that the distance between all the balusters be as uniform as possible. Unfortunately, it's not as easy as it sounds. So, I came up with this relatively simple calculator to tell me exactly where to drill each hole on each step.

while (1) { my $first = GetMeasurement('1st'); my $next_step = GetMeasurement('2nd'); my $distance = abs($next_step - $first); my $gap = $distance / 3; my $gap_frac = Fractionalize($gap); print "\nDistance between balusters: $gap_frac\" ($gap)\n"; my $second = $first + $gap; # Locate second hole my $third = $second + $gap; # Locate third hole my $first_frac = Fractionalize($first); # Get fraction value my $second_frac = Fractionalize($second); # Get fraction value my $third_frac = Fractionalize($third); # Get fraction value print "\nDrill 1st baluster hole at: $first_frac\" ($first)\n"; print "Drill 2nd baluster hole at: $second_frac\" ($second)\n"; print "Drill 3rd baluster hole at: $third_frac\" ($third)\n\n"; } exit; ##################################################### # Get measurement to first hole on stair tread. ##################################################### sub GetMeasurement { $position = shift; print "Enter $position measurement.....: "; my $measurement = <STDIN>; exit if $measurement =~ /q/i; # Terminate if 'q' chomp $measurement; if ( $measurement =~ /(\d*)\s(\d*)\/(\d*)/ ) { $measurement = ${1} + ${2} / ${3}; } return $measurement; } ##################################################### # Convert a number with decimals to a fraction-scale: 32's of an inch ##################################################### sub Fractionalize { my $number = shift; # Get the number my @numbers = split(/\./, $number); # Get int&decimal my $modulus = '.' . $numbers[1]; # Put decimal back my $x32 = ($modulus * 32); # Multiply x 1/32" my $fraction = Reduce($x32,32); # Reduce fraction return "$numbers[0] $fraction"; # Return fraction } ##################################################### # Reduce a fraction as far as possible (6/32 -> 3/16, 16/32 -> 1/2) ##################################################### sub Reduce { my $divisor = shift; # Get top my $denominator = shift; # Get bottom $divisor = int $divisor; # Trim insignificat digits # Continue until either number is no longer divisible by 2 while (1) { unless (($divisor %2) or ($denominator %2)) { $divisor = $divisor / 2; $denominator= $denominator / 2; } else { last; } } return "$divisor/$denominator"; }

Here's how it looks when executed...
Enter 1st measurement.....: 2 5/8 Enter 2nd measurement.....: 12 1/8 Distance between balusters: 3 5/32" (3.16666666666667) Drill 1st baluster hole at: 2 5/8" (2.625) Drill 2nd baluster hole at: 5 25/32" (5.79166666666667) Drill 3rd baluster hole at: 8 15/16" (8.95833333333333) Enter 1st measurement.....: q
"P" is for practical.

Replies are listed 'Best First'.
Re: Measure distance between balusters
by MonkE (Hermit) on Oct 04, 2007 at 00:18 UTC

    That's great ... using Perl for carpentry work. Good work! And I hope you didn't get any sawdust in your keyboard.

    This reminds me of a similar DIY project of my own where I had to calculate the number of balusters needed for a (horizontal) handrail I was building. Of course I wanted the spacing between the balusters to be the same throughout, and I worked it through with algebra. It was harder than I thought it would be though.