Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm pretty new at perl. Today, I decided I would challenge myself and make a script bigger than just 4 or 5 lines. I came up with the idea to make a plane geometry calculator, in which the args will indicate which figure and operations to do as well as any prerequisite numbers to do them.

Readmore tags don't seem to be working, so I'm going to post the readmores inside these little dash things. Treat it like a readmore.

-----------------------------IN READMORE--------------------------------------------------
In the script, the possible figures are tri, cir, sqr, and rec. The possible operations are area, peri(perimeter), or hypo(hypotenuse). Based on the combination of these two args, the script will assign a value to $reqargs, which indicates the number of required arguments following the operation. And now, based on the combination of the three pieces of data you have so far, it generates a number in the scalar $refsum. The script then has an enourmous array of if/elsif/else cases built around the possible amount for $refsum, and based on this scalar it will determine the operation, execute it, and store the results in $answer, which is printed at the end. And here's readmore inside a readmore about how refsum is determined.
-----------------------------IN READMORE--------------------------------------------------
REFSUM is built off place values. It is first declared as zero before anything is done to it. Then, if you specified a triangle, circle, square, or rectangle, it will add 0.1, 0.2, 0.3, or 0.4 to $refsum, respectively. The same goes for all other input. I think that's a pretty good explanation...

-----------------------------OUT READMORE-------------------------------------------------
-----------------------------OUT READMORE-------------------------------------------------

Everything in the script seems to be running fine, except for the fact that it always tells me I haven't specified any arguments. I will put my code below. If you don't want to read the code you might be able to find the problem in the readmores; I'm pretty sure it has something to do with the way i use $ARGV.

#!usr/bin/perl -w use warnings; print "Welcome to Will's Plane Geometry Calculator.\nThe first argumen +t should be tri, cir, rec, or sqr.\nThe second argument should be area, peri, or hypo.\nIf you give hypo as the second argumen +t, the next two arguments should be the known sides of the traingle.\nthe peri arg will also find circumference if cir is giv +en for the figure. Specify the diameter to find this.\nPi is approximated at 3.141592654(simply change my pi if a diff +erent approximation is needed)\nTo find area of a circle specify the radius.\nI think you can figure out the rest.\n", " +-" x76, "\n"; my $pi=3.141592654; if (defined $ARGV[0] && defined $ARGV[1] && defined $ARGV[2] && define +d $ARGV[3]) { #MasterIf; keep all commands inside #################################### #Define our variables according to arguments #First off is ARG 0 (figure) if ($ARGV[0]=="tri") { my $figure="triangle"; } elsif ($ARGV[0]=="cir") { my $figure="circle"; } elsif ($ARGV[0]=="rec") { my $figure="rectangle"; } elsif ($ARGV[0]=="sqr") { my $figure="square"; } # #Next is ARG 1 (operation) # if ($ARGV[1]=="area") { my $op="area"; if ($figure=="rectangle") { my $reqargs=2; } elsif ($figure=="square") { my $reqargs=1;} elsif ($figure=="triangle") { my $reqargs=2; } elsif ($figure=="circle") { my $reqargs=1; } } elsif ($ARGV[1]=="peri") { my $op="perimeter"; if ($figure=="rectangle") { my $reqargs=2; } elsif ($figure=="square") { my $reqargs=1; } elsif ($figure=="triangle") { my $reqargs=3; } elsif ($figure=="circle") { my $reqargs=1; } } elsif ($ARGV[1]=="hypo") { my $op="hypotenuse"; my $reqargs=2; if ($figure!="triangle") { die "Hypotenuse calculations only work +with argument tri.\n"; } } # #Define REFSUM for easier case management later #The following cases are to convert defined scalars into numbers. B +ased on the number, the script will decide what to do. # my $refsum=0; if ($figure=="triangle") { $refsum=$refsum+0.1 } elsif ($figure=="circle") { $refsum=$refsum+0.2 } elsif ($figure=="rectangle") { $refsum=$refsum+0.4 } elsif ($figure=="square") { $refsum=$refsum+0.3 } if ($op=="area") { $refsum=$refsum+1 } elsif ($op=="peri") { $refsum=$refsum+2 } elsif ($op=="hypo") { $refsum=$refsum+3 } if ($reqargs==1) { $refsum=$refsum+10 } elsif ($reqargs==2) { $refsum=$refsum+20 } elsif ($reqargs==3) { $refsum=$refsum+30 } # #Next is ARG 2 (first num) #This is where it starts to get complicated # print "\n INFO PRINTOUT FROM SCALARS: \n FIGURE: $figure \n OPERATIO +N: $op \n REQUIRED ARGS: $reqargs\n\n"; if ($refsum<10) { die "[err] REFSUM is less than 10; invalid number."; } if ($refsum==21.1) { if (defined $ARGV[4]) { my $base=$ARGV[3]; my $height=$ARGV[4]; my $answer=$base*$height; } else { die "Did not specify height of triangle\n"; } } elsif ($refsum==32.1) { if (defined $ARGV[5] && defined $ARGV[4]) { my $sidesone=$ARGV[3]; my $sidestwo=$ARGV[4]; my $sidesthree=$ARGV[5]; my $answer=$sidesone+$sidestwo+$sidesthree; } else { die "Did not specify all three sides\n"; } } elsif ($refsum==23.1) { if (defined $ARGV[4]) { my $firstrefraw=$ARGV[3]; my $firstrefuse=$firstrefraw*$firstrefraw; my $secondrefraw=$ARGV[4]; my $secondrefuse=$secondrefraw*$secondrefraw; my $answer=$firstrefuse+$secondrefuse; } else { die "Did not specify both sides\n"; } } elsif ($refsum==12.2) { my $diameter=$ARGV[3]; my $answer=$diameter*$pi; } elsif ($refsum==11.2) { my $radraw=$ARGV[3]; my $raduse=$radraw*$radraw; my $answer=$raduse*$pi; } elsif ($refsum==13.2) { print "[err] a 3 in the ones place value is reserved for triangles +.\n"; die "[err] invalid value for REFSUM ( $refsum )\n"; } elsif ($refsum==11.3) { my $asidesa=$ARGV[3]; my $answer=$asidesa*$asidesa; } elsif ($refsum==12.3) { my $asidesp=$ARGV[3]; my $answer=$asidesp*4; } elsif ($refsum==13.3) { print "[err] a 3 in the ones place value is reserved for triangles +.\n"; die "[err] invalid value for REFSUM ( $refsum )\n"; } elsif ($refsum==21.4) { if (defined $ARGV[4]) { my $length=$ARGV[3]; my $width=$ARGV[4]; my $answer=$length*$width; } else { die "Length and Width of rectangle not specified.\n"; } elsif ($refsum==22.4) { if (defined $ARGV[4]) { my $lengthraw=$ARGV[3]; my $lengthuse=$lengthraw*2; my $widthraw=$ARGV[4]; my $widthuse=$widthraw*2; my $answer=$lengthuse*$widthuse; } else { die "Length and Width of rectangle not specified.\n"; } elsif ($refsum==23.4) { die "Invalid number for REFSUM; reserved for triangles.\n"; } # #Calculations part is done, this part is just telling you the answer # if (defined $answer) { print "\n\n","-" x76,"\n","Calculations Completed Successfully!\n" +; print "Your answer is: $answer"; } else { print "\n\n","-" x76,"\n","Calculations Completed.\n Operation Uns +uccessful.\n Cause Unkown.\n"; } print "Due to stupidity of PERL: pi is $pi"; } else { die "Please define at least 4 arguments\n"; } + #Die if less than 4 args given
Thanks, ~OxYgEn

In reply to Can't grab $ARGV[n] by OxYgEn

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 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?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-19 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found