> Say we know that
>
> a + b = 60
>
> and
>
> a/b = 0.5
>
> then of course
>
> a = 20, b = 40
>
> which you can see by common sense or by plotting two lines on a graph.
>
> But how to do this programmatically so that it works for any input values
> (without attempting some way of running through possible values and
> testing)?
>
> use strict;
> use warnings;
> my $a_plus_b = 60;
> my  $a_divided_by_b = 0.5;
> my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
> print "a = $a , b = $b\n";
>
> sub resolve_a_b {
>     my $a_plus_b = shift;
>     my  $a_divided_by_b = shift;
>     my ($a,$b);
>     ### what goes here?
>     return ($a, $b);
> }
>

>From the response I got from one list member I have ended up with:

use strict;
use warnings;
my $a_plus_b = 60;
my  $a_divided_by_b = 1.5;
my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
print "a = $a , b = $b\n";

sub resolve_a_b {
    my $a_plus_b = shift;
    my $a_divided_by_b = shift;
    my ($a,$b);
    $b = $a_plus_b/($a_divided_by_b + 1);
    $a = $a_plus_b - $b;
    return ($a, $b);
}

which works for any input values. Whether or not this solves my cropping
problem or not is another matter!

Thanks
Mark



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to