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

O Wisest of the Wise, I respectfully request Your counsel in contemplating my dilemma...

I have a multi-demensional hash: %payment

I want to walk through the elements at one level of this hash:
for ( keys %{$payment}{$check_number} ) { ...

Then, I want to record in a scalar variable the number of elements at a lower level of this hash:
my $numpayments = scalar (keys %{$payment{$check_number}{$account}});

I want to walk through the values of this level of the hash:
for ( keys %{$payment{$check_number}{$account} ) { ...
(there is one more level of indexes for keys to make a list from)

While walking through the elements, I want to increment a scalar variable and have it set the value of another variable when I'm on the last element:

my $last_payment = 0; if ( ++$payment_count == $numpayments ) { $last_payment = 1 };

Then, while I'm in the loop somewhere, I want to take a different action if it's the last record:

if ( $last_payment == 1 ) { print "last record\n"; print FH "\n"; }

    The problem is, it's not always setting the variable if I'm on the last element of the hash. I know this because it goes to the next element at the higher level of the hash, does stuff there, then goes through another element at the lower level of the hash.

Invulnerable. Unlimited XP. Unlimited Votes. I must be...
        GhodMode

Replies are listed 'Best First'.
Re: keeping track of the current element in a multi-dimensional hash
by Ovid (Cardinal) on Sep 06, 2002 at 22:34 UTC

    If you can post your code in a block (or a subset to demonstrate the problem), this might make things clearer. By putting all of your code together, I get the following:

    for ( keys %{$payment{$check_number}} ) { my $numpayment = scalar ( keys %{$payment{$check_number}{$account}} +); for ( keys %{$payment{$check_number}{$account} ) { my $last_payment = 0; if ( ++$payment_count == $numpayments ) { $last_payment = 1 }; if ( $last_payment == 1 ) { print "last record\n"; print FH "\n"; }

    I don't think that can be correct as both of your for loops are assinging to $_, thus having your second one trounce on the value of the first. Also, this line is problematic:

    if ( ++$payment_count == $numpayments )

    As a general rule, you don't want to autoincrement variables within another expression. You are not guaranteed that it will behave the way you expect. If possible, one way to correct that:

    $payment_count++; if ( $payment_count == $numpayments )

    It may not effect the program, but it's easier to read. If you can post your code a bit more clearly, we may be able to see the issue (or maybe someone else sees it now, I don't). Since the code above doesn't actually use the $_ variable that you're stepping on, I can only assume that there's more here than meets the monitor.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

          With the help of Snafu, who is a co-worker of mine, I figured out this one.
          I was resetting my $payment_count variable to 0 in the wrong place.
          He should've posted the answer here, I would've voted him up :)
          I pulled the autoincrement out of the conditional statement. I have had consistent results using it that way, but I agree that it is easier to read when separated.
      many thanks...

      Invulnerable. Unlimited XP. Unlimited Votes. I must be...
              GhodMode
Re: keeping track of the current element in a multi-dimensional hash
by BrowserUk (Patriarch) on Sep 06, 2002 at 22:41 UTC

    Without seeing more of your code, we can only speculate why you are not always catching the last payment. My best guess would be that there is a path through the code that bypasses your increment.

    You might be able to simplify your code slightly as

    my $numpayments = scalar keys ..... ... if (! --$numpayments) {lastpayments =1} ... if ( $lastpayments ){ print "last record\n"; };

    Or even simpler

    my $numpayments = scalar keys %..... .... --$numpayments; ... if(! $numpayments ) { print "Last record\n"; }

    If you need to remember the $numpayments value for other uses, just make a copy and decrement that.

    Using a single var and --, will eliminate a couple of possible modes of failure. It won't necessarially fix your problem, but i t might help highlight where it is.

    If not, come back and post a bit more code.


    Well It's better than the Abottoire, but Yorkshire!