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

What, dear monks, is the most efficient way of getting these arrays:
my @x_values_for_points_above_the_trend_line; my @y_values_for_points_above_the_trend_line; my @distances_above_the_trend_line; my @x_values_for_points_below_the_trend_line; my @y_values_for_points_below_the_trend_line; my @distances_below_the_trend_line; my @x_values_for_points_on_the_trend_line; my @y_values_for_points_on_the_trend_line; # The contents of these arrays have corresponding values in the same a +rray positions.
... into a lookup hash so that I can perform operations like the following:
my $count_A = -1; my @regions_A_array; foreach (@x_values_for_points_above_the_trend_line) { $count_A++; my $x_value_SM = $_; my $y_value_SM = $y_values_for_points_above_the_trend_line[$count_A] +; my $Select_area_above_trendline = " SELECT DISTINCT A.Region FROM Temp_table_A A, Temp_table_B B WHER +E A.Region = B.Region AND A.SM = ".$x_value_SM." AND B.SM = ".$y_valu +e_SM." "; my $sth_atl = $dbh->prepare($Select_area_above_trendline) or die "Co +uldn't prepare query: ".$dbh->errstr; $sth_atl->execute() or die "Couldn't execute query: ".$sth_atl->errs +tr; my @regions_A; while (@regions_A = $sth_atl->fetchrow_array) { push (@regions_A_array, @regions_A); } my @regions_A_array = r(@regions_A_array); # This is to remove du +plicates from the array } # The subroutine below removes duplicates sub r{ keys%{{map{$_,1}@_}} }
...... with the added functionality of printing out each region in the array @regions_A_array with the $distances_below_the_trend_line[$pos] value.


By the way, I accept that it looks like this process has been badly designed. It is an evolved piece of code.

Replies are listed 'Best First'.
Re: Making do with a badly designed process
by amarquis (Curate) on Jan 18, 2008 at 15:16 UTC

    Backing up a step, you will probably find that there is a statistics module on CPAN (hell, maybe even core) that both does the regression for you and returns it in an easy to use form.

    Just food for thought. It'll probably take you the same amount of time to find/grab/implement a module solution as it would to implement your solution, but with a CPAN module you'll have tighter, easy to build upon code.

Re: Making do with a badly designed process
by Anonymous Monk on Jan 18, 2008 at 13:42 UTC
    First find a way to do it, then we'll tell you if its efficient