perlquestion
ailie
<p>This is a script I wrote to calculate how much fabric I need when making various quilt blocks. At the moment it's run from the command line, but I'd like to eventually get it so it can be run from a web page. That's where I really started to get frustrated. Alternately, a GUI might be sort of neat.</p>
<p>Anyway, I would be interested in receiving feedback on the script as it stands now. I have a vague feeling that perhaps there are better, cleaner ways to do much of what I've got, and I'd like to learn them.</p>
<code>
#!/usr/bin/perl -w
use strict;
# How many fabrics are you using?
my $j;
my $number_of_fabrics;
print "How many fabrics are you using? \n";
chomp($number_of_fabrics = <STDIN>);
for ($j = 1; $j <= $number_of_fabrics; $j++) {
# How wide is the fabric you are using?
my %fabric_width;
my $fab_width;
print "How wide is fabric $j? \n";
chomp($fab_width = <STDIN>);
$fabric_width{$j} = $fab_width - 2;
print "To be safe, we'll use a width of $fabric_width{$j} for fabric $j. \n";
# How many different shapes is a block made of?
my $number_of_shapes;
print "How many different shapes of fabric $j is a block made of? \n";
chomp($number_of_shapes = <STDIN>);
# What are the dimensions of the shapes?
my $i;
my %width;
my %height;
my %shape;
my $piece;
for ($i = 1; $i <= $number_of_shapes; $i++) {
my $shape_width;
print "What is the width of shape $i? \n";
chomp($shape_width = <STDIN>);
$width{$i} = $shape_width;
my $shape_height;
print "What is the height of shape $i? \n";
chomp($shape_height = <STDIN>);
$height{$i} = $shape_height;
$shape{$i} = $shape_width . "x" . $shape_height;
}
print "Shape dimensions: \n";
foreach $i (keys (%shape)) {
print "The dimensions of shape $i are $shape{$i}\n";
}
# For each shape, how many can be made per yard of fabric?
my $number_per_fabric_width_strip;
my $strips_per_yard;
my $number_per_yard;
my $number_of_pieces;
my %pieces;
my $number_of_strips;
my $strip_yardage;
my $remainder;
my $fabric_yardage;
my $number_of_blocks;
my $total_pieces;
foreach $i (keys (%height)) {
$number_per_fabric_width_strip = $fabric_width{$j} / ($height{$i});
$number_per_fabric_width_strip = int $number_per_fabric_width_strip;
print "You can get $number_per_fabric_width_strip piece(s) of shape $i per
$width{$i}\" strip of fabric $j.\n";
$strips_per_yard = 36 / ($width{$i});
$strips_per_yard = int $strips_per_yard;
print "You can get $strips_per_yard $width{$i}\" strips per yard. \n";
$number_per_yard = $number_per_fabric_width_strip * $strips_per_yard;
print "You can get $number_per_yard of shape $i per yard of
$fabric_width{$j}\" of fabric $j. \n";
print "How many of shape $i do you need per block? \n";
chomp($number_of_pieces = <STDIN>);
$pieces{$i} = "$number_of_pieces";
print "You need $number_of_pieces per block. \n";
print "How many blocks are you making? \n";
chomp($number_of_blocks = <STDIN>);
print "You are making $number_of_blocks blocks. \n";
$total_pieces = $number_of_pieces * $number_of_blocks;
print "You need $total_pieces of shape $i. \n";
if ($total_pieces <= $number_per_fabric_width_strip) {
print "You will need one $width{$i}\" strip of fabric $j. \n";
}
else {
$number_of_strips = $total_pieces / $number_per_fabric_width_strip;
$number_of_strips = int $number_of_strips;
$remainder = $total_pieces % $number_per_fabric_width_strip;
$strip_yardage= $number_of_strips * $width{$i};
if ($remainder == 0) {
print "You will need exactly $strip_yardage\". \n";
}
else {
$fabric_yardage = $strip_yardage + $width{$i};
print "You will need $fabric_yardage\" of fabric $j. The last $width{$i}\" strip will have only $remainder pieces cut from it. \n";
}
}
}
}
</code>
<p>Thanks for any advice.</p>