DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 7th February 2023
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default Calculating equal offsets of hole for irrigation pipe or wood: gap.pl

During one of the corona lockdowns in 2020 I wrote a small Perl script to calculate evenly divided offsets for screws or nails. Later I also used it for irrigation pipe.
Code:
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

# 
my $length = 0;
my $divisions = 0;
my $start = 0;
my $end = 0;

GetOptions(
   'length:i' => \$length,
   'divisions:i' => \$divisions,
   'start:i' => \$start,
   'end:i' => \$end
   ) or die usage();

sub usage{
print <<END;
Calculate equally spaced offset of holes in irrigation pipe
or of screws in piece of wood

   \$ \./gap.pl -length 100 -divisions=12 -start=2 -end=4

This will divide (100 - 2 - 4) in 12 equal parts and print the offsets 
END
exit(1);
}

&usage if ($length == 0) or ($divisions == 0);

my $increment;
my $position; 
my $counter = 0;

$position = $start;
$increment = ($length - $start - $end) / $divisions;


#---
print "\nLength\t\t: $length";
print "\nStart\t\t: $start";
print "\nEnd\t\t: $end";
print "\nDivisions\t: $divisions\n";
print "\nStart\t\t: $start";
print "\nIncrement\t: $increment";
print "\nLast\t\t: \($length - $end)\n";

while ($position < ($length - $end +1)) {
    $counter++;
    print "\n$counter\t$position";
    $position = $position + $increment;
}


print "\n";
Some examples of usage:
Code:
$ ./gap.pl

Calculate equally spaced offset of holes in irrigation pipe
or of screws in piece of wood

   $ ./gap.pl -length 100 -divisions=12 -start=2 -end=4

This will divide (100 - 2 - 4) in 12 equal parts and print the offsets

$ ./gap.pl -length 120 -s 6 -end 10 -div 10 

Length          : 120
Start           : 6
End             : 10
Divisions       : 10

Start           : 6
Increment       : 10.4
Last            : (120 - 10)

1       6
2       16.4
3       26.8
4       37.2
5       47.6
6       58
7       68.4
8       78.8
9       89.2
10      99.6
11      110

$ ./gap.pl -length 120 -s 6 -end 10 -div 15 

Length          : 120
Start           : 6
End             : 10
Divisions       : 15

Start           : 6
Increment       : 6.93333333333333
Last            : (120 - 10)

1       6
2       12.9333333333333
3       19.8666666666667
4       26.8
5       33.7333333333333
6       40.6666666666667
7       47.6
8       54.5333333333333
9       61.4666666666667
10      68.4
11      75.3333333333334
12      82.2666666666667
13      89.2
14      96.1333333333334
15      103.066666666667
16      110
$
Notice that you can shorten the command line options and that the '=' is optional.
I also don't round the offsets, that is left for the user to decide.

As a metric system user I pity the poor souls that have to figure these things out in inches and feet
Attached Files
File Type: pl gap.pl (1.1 KB, 7 views)
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #2   (View Single Post)  
Old 7th February 2023
frcc frcc is offline
Don't Worry Be Happy!
 
Join Date: Jul 2011
Location: hot,dry,dusty,rainy,windy,straight winds, tornado,puts the fear of God in you-Texas
Posts: 335
Default

Nice!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The spiped secure pipe daemon J65nko General software and network 0 5th August 2022 04:10 AM
Dual ISP Equal Cost Multi-path routing perfopt OpenBSD General 4 23rd March 2022 02:36 AM
How do I pipe a man page into emacs for searches? OS_Stuntman FreeBSD General 9 20th November 2011 06:37 PM
Adobe: hole closed, hole open J65nko News 0 5th November 2010 06:50 PM
OpenBSD: equal-cost multipath routing Lexus45 OpenBSD General 0 31st August 2010 08:13 AM


All times are GMT. The time now is 11:21 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick