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

Hi Monks,

I have a very simple problem which is driving me slightly mad. The question relates to a variable named @unique_slopes which is within a few nested loops. When I print the contents of the variable it contains many numbers (is this just because it is within the loops??). I need all these numbers I store in @unique_slopes to be in the same array (so I can iterate through it and find what i'm after). Is there a better way for me to store these numbers so that I can split the array and iterate through it? I eventually want to use the for loop at the bottom but am just having problems as everything in @unique_slopes is in $unique_slopes[0].

Cheers monks.

foreach my $line (@data) { # lots of irrelevent code for (my $i =1; $i < @array; $i++) { # once again lots of irrelevent code %gradient_hash = map {$matching_temps[$_] => $gradients[$_]} 0 .. + $#matching_temps; while (($key2, $value2) = each (%gradient_hash)) { if (($value2 > 0.1) || ($value2 < -0.1)) { @steep_grads = $key2 . '?' . $value2; @slopes = $value2; } } @unique_slopes = grep {! $seen2{$_} ++} @slopes; print "@unique_slopes<P>"; } }
This is what I eventually want to do with @unique_slopes
for (my $i = 1; $i < @unique_slopes; $i++) { if (($unique_slopes[$i] < -0.1) && ($unique_slopes[$i-1] > 0.1)) + { print "PEAKS: $unique_slopes[$i-1] $unique_slopes[$i]<P>"; } }

update (broquaint): added formatting + removed extraneous whitespace in code

Replies are listed 'Best First'.
Re: going loopy
by broquaint (Abbot) on May 14, 2003 at 11:13 UTC
    The problem is how you create the @slopes and @steep_grads arrays, you're assigning to them, which is not what you want. Instead you want to be using the push function e.g
    while (($key2, $value2) = each (%gradient_hash)) { if (($value2 > 0.1) || ($value2 < -0.1)) { push @steep_grads, $key2 . '?' . $value2; push @slopes, $value2; } }
    This pushes the value onto the array, which looks to be desired behaviour. So hopefully now your code should do the right thing :)
    HTH

    _________
    broquaint

      Thanks for the advice, but I am still having a lot of trouble trying to split the @unique_slopes array! Any suggestions?
        Any suggestions?
        I don't mean to sound harsh but could you read How (Not) To Ask A Question and revise your post above? I can't really help you unless I understand what your problem is, a well stated question with context and relevant data goes a very long way to inspiring any would-be helpers.
        HTH

        _________
        broquaint

Re: going loopy
by perlguy (Deacon) on May 14, 2003 at 11:10 UTC

    Update: Broquaint's response tells me that I should have been looking farther up in the code for a similar problem to the one that I noticed. I thought that you might be doing the iteration over the @unique_slopes array outside of all your loops, and therefore, the problem listed below exists. In my haste, I neglected to see that a similar problem existed with the other two arrays. My appologies.

    It appears to me that you are resetting @unique_slopes each time in the iteration. If my assumptions are correct, change it to (untested):

    push @unique_slopes, (grep !$seen2{$_}++, @slopes);

    And that should solve the problem.