geektron has asked for the wisdom of the Perl Monks concerning the following question:
I have a hashref of hashrefs that looks something like:
when i try to remove items from the set using delete <code> ( simple call, no? <code> delete ( $var->{$key} ) within a loop through the hashref keys, it deleted the value, but not the key, so that later when i'm looping through the same hashref to perform another operation, the key for the hashref it still there ...$var = { '1234-567' => { 'key1' => 'stuff' , 'key2' => 'stuff', } '1234-997' => { 'key1' => 'stuff' , 'key2' => 'stuff', } };
essentially, i'm doing this .....
where $sale_items looks like the example hashref above, and $payments is an arrayref of hashrefs. (almost the same content ... the end goal is double-ledger accounting stuff .... and it's killing me. i'm not a CPA .... )foreach my $paymentRow ( @$payments ) { foreach my $salesKey ( keys %$sale_items ) { ### make sure we're applying the right line items ... next if ( $paymentRow->{creditacct} != $sale_items->{$salesKey}{debitacct} ); ## if payment can be applied, ## apply it and delete the row from payments/credits array warn "DELETING ... {$salesKey} " if ( $paymentRow->{amount} == $sale_items->{$salesKey}{amount} and $paymentRow->{ppid} == $sale_items->{$salesKey}{ppid} ); delete $sale_items->{$salesKey} and undef( $paymentRow ) if ( $paymentRow->{amount} == $sale_items->{$salesKey}{amount} and $paymentRow->{ppid} == $sale_items->{$salesKey}{ppid} ); $sale_items->{$salesKey}{amount} -= $paymentRow->{amount} and undef( $paymentRow ) if ( $paymentRow->{amount} < $sale_items->{$salesKey}{amount} and $paymentRow->{ppid} == $sale_items->{$salesKey}{ppid} ); } }
i think i'm hitting the autovivification issue, *but* when I've tried to add next statments to skip over the places i think the hashref key would be autovivified, nothing happens .....
any advice is appreciated
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: deleting key/value from hashref
by ikegami (Patriarch) on Sep 24, 2004 at 18:23 UTC | |
by geektron (Curate) on Sep 24, 2004 at 19:00 UTC | |
|
Re: deleting key/value from hashref
by Zed_Lopez (Chaplain) on Sep 24, 2004 at 18:28 UTC | |
by geektron (Curate) on Sep 24, 2004 at 18:58 UTC | |
by ikegami (Patriarch) on Sep 24, 2004 at 19:14 UTC | |
|
Re: deleting key/value from hashref
by Eimi Metamorphoumai (Deacon) on Sep 24, 2004 at 18:30 UTC |