 Clear questions and runnable codeget the best and fastest answer PerlMonks

### comment on

 Need Help??
A few suggestions:
• Break the Law_Of_Sines() function up a bit more. For example, you have a (rather hefty) section of code cut'n'pasted three times. Make it a subroutine.
• Instead of running \$A, \$a, \$B, \$b, \$C, and \$c, why not do a list of hashes called angles? So, you could do something like \$angles->{B} to reference the second angle in the first triangle. The real benefit of that is to allow you to do for loops, reducing the number of lines while improving readability.
An example of the second suggestion would be:

```    unless ( \$A > 0 && \$B > 0 && \$C > 0 ) {
if ( \$A > 0 && \$B > 0 ) { \$var = "C" }
if ( \$A > 0 && \$C > 0 ) { \$var = "B" }
if ( \$B > 0 && \$C > 0 ) { \$var = "A" }
if ( \$var eq "C" ) {
\$sum = \$angles{B} + \$angles{A};
\$other = 180 - \$sum;
\$angles{\$var} = \$other;
\$C = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$oth
+er ) );
} elsif ( \$var eq "B" ) {
\$sum = \$angles{A} + \$angles{C};
\$other = 180 - \$sum;
\$angles{\$var} = \$other;
\$B = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$oth
+er ) );
} elsif ( \$var eq "A" ) {
\$sum = \$angles{B} + \$angles{C};
print ("Sum: \$sum\n");
\$other = 180 - \$sum;
\$angles{\$var} = \$other;
\$A = sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$oth
+er ) );

```if (grep \$_ <= 0, @{\$angles->}{('A' .. 'C')}) {
my \$zero_angle = 'A';
if (\$angles->{C} <= 0) { \$zero_angle = 'C' }
elsif (\$angles->{B} <= 0) { \$zero_angle = 'B' }

my @sides = ('A' .. 'C');
@sides = grep \$_ ne \$zero_angle, @sides;

\$sum = 0;
\$sum += \$_ for @{\$angles->}{@sides};
\$other = 180 - \$sum;
\$angles{\$var} = \$other;
\$angles->{\$zero_angle} =
sprintf( "%.10f", Math::NumberCruncher::deg2rad( \$other ) );
}
The primary difference between your version and mine is that I chose a data structure that allows me to have the interpreter associate pieces of data that should be associated together. Your version requires that you continually associate them yourself.

The second difference is that I'm taking advantage of the easy conversions between hashes and arrays through hash-slices. Read up on them. They're SOOOO powerful. :)

Now, you don't have to use my data structure. I chose it because it was the closest to what you were doing. However, it can easily make sense to have the angles in an array and not a hash. So, triangle 1, angle 2 would be \$angles->. Simple enough.

Keep with it. It's a neat idea!

Update: Fixed a few square brackets. Thanx Albannach and ChemBoy!

In reply to Re: Help w/ Law of Sines script by dragonchild
in thread Help w/ Law of Sines script by sifukurt

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.

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 chanting in the Monastery: (3)
As of 2023-12-10 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What's your preferred 'use VERSION' for new CPAN modules in 2023?

Results (39 votes). Check out past polls.

Notices?