http://qs1969.pair.com?node_id=101489


in reply to Help w/ Law of Sines script

A few suggestions: 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 ) );
instead could be written as:

if (grep $_ <= 0, @{$angles->[0]}{('A' .. 'C')}) { my $zero_angle = 'A'; if ($angles->[0]{C} <= 0) { $zero_angle = 'C' } elsif ($angles->[0]{B} <= 0) { $zero_angle = 'B' } my @sides = ('A' .. 'C'); @sides = grep $_ ne $zero_angle, @sides; $sum = 0; $sum += $_ for @{$angles->[0]}{@sides}; $other = 180 - $sum; $angles{$var} = $other; $angles->[0]{$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->[0][1]. Simple enough.

Keep with it. It's a neat idea!

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

Replies are listed 'Best First'.
Re: Re: Help w/ Law of Sines script
by sifukurt (Hermit) on Aug 03, 2001 at 00:36 UTC
    Very nice. That's exactly the sort of thing I was looking for. Originally I had the repeated blocks of code as a subroutine, but for reasons I don't know, it didn't want to work correctly. It was calculating the correct values, but wasn't returning them correctly. Of course, I was working on this in the evening after a day of programming, so by that time I was probably suffering from that weird sort of programming blindness where the answer to the problem is hiding in plain sight.

    I'm going to incorporate your suggestions (as well as the suggestions of others) and will post the modified version as soon as it is ready. Thanks much.
    ___________________
    Kurt