Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

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.

#!/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 fabri +c $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_st +rip; print "You can get $number_per_fabric_width_strip piece(s) of sh +ape $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"; } } } }

Thanks for any advice.


In reply to Fabric calculator - a better way to do it? by ailie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2022-10-06 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (26 votes). Check out past polls.

    Notices?