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.

In reply to Measure distance between balusters by perlofwisdom

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.