in reply to keeping track of the current element in a multi-dimensional hash

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.

Replies are listed 'Best First'.
Re: Re: keeping track of the current element in a multi-dimensional hash
by GhodMode (Pilgrim) on Sep 09, 2002 at 14:24 UTC

        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