Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Fabric calculator - a better way to do it?

by ailie (Friar)
on Dec 06, 2001 at 04:04 UTC ( [id://129813]=perlquestion: print w/replies, xml ) Need Help??

ailie has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Fabric calculator - a better way to do it?
by jynx (Priest) on Dec 06, 2001 at 09:36 UTC

    A few suggestions,

    Most of these are rather cosmetic, but they should help readability.

    Considering how many times you read from STDIN you may want to farm that part out to a subroutine. It would remove clutter from the rest of the code and should be a bit more maintainable later.

    For structures like %width an array seems to make more sense than a hash. It works either way, but it's probably a good idea to try and use more appropriate tools when possible.

    Also you can reduce the number of variables you have to keep track of by using a hash for multiple things. For example, your $number_of_* variables in the last section could probably be written as (using a subroutine for getting STDIN):

    my %number; $number{blocks} = get_input("How many blocks are you making?"); $number{strips} = int( $total_pieces / $number_per_fabric_width_strip) +; etc...
    jynx
(crazyinsomniac:) Re: Fabric calculator - a better way to do it?
by crazyinsomniac (Prior) on Dec 06, 2001 at 11:42 UTC
    This is me just being a monkey and testing the script out:
    F:\dev>perl fabric.pl How many fabrics are you using? 2 How wide is fabric 1? 1 To be safe, we'll use a width of -1 for fabric 1. How many different shapes of fabric 1 is a block made of? 1 What is the width of shape 1? 1 What is the height of shape 1? 2 Shape dimensions: The dimensions of shape 1 are 1x2 You can get 0 piece(s) of shape 1 per 1" strip of fabric 1. You can get 36 1" strips per yard. You can get 0 of shape 1 per yard of -1" of fabric 1. How many of shape 1 do you need per block? 23 You need 23 per block. How many blocks are you making? 2 You are making 2 blocks. You need 46 of shape 1. Illegal division by zero at fabric.pl line 88, <STDIN> line 7. F:\dev>
    I'll update this node after I've messed with the code.

    My monkeying around immediately proves insightful into how you can improve this (at least it appears so to me).

    A good strategy might be to turn this into a module, and then, write "interfaces" for it, like commandline, CGI, GUI. All I have to say at this point is, not bad quiltmaker (some of your math may be funny, but useful to real people and immensly perlmonkish -- no prog/perl bg, good practices at the beginning). I see sanity checks, units of measurement conversions, price estimation.... ;D

    update:

    #!/usr/bin/perl -w use strict; # How many fabrics are you using? my $number_of_fabrics; print "How many fabrics are you using? \n"; chomp($number_of_fabrics = <STDIN>); $number_of_fabrics =~ s/\D//; # only allow number input ## $number_of_fabrics ||= 3; # assume default quietly unless($number_of_fabrics and $number_of_fabrics >=3 ) { warn "Ahem!!! You must enter a NUMBER >= 3, but that's ok, default + is 3"; $number_of_fabrics = 3; } #... more to come ... ;D

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      Wow. It never even occurred to me to put in widths like that, since quilting fabric is almost always 45" wide, sometimes 60" wide - certainly not 1" wide. Of course, now I'm going "D'oh!"

      The math is inelegant, but that's a basic fact of calculating fabric yardages for quilts. Simple math, but annoying.

      I like the module idea. Of course, I have no idea how to go about such a thing, but learning is really the whole point.

      Price estimation is simple:
      print "No, you may not buy more fabric. \n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://129813]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found