Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Splice of ref to AoH not removing element

by chromatic (Archbishop)
on May 29, 2004 at 21:34 UTC ( [id://357517]=note: print w/replies, xml ) Need Help??


in reply to Splice of ref to AoH not removing element

Adding to or removing from an array you're iterating over is a bad idea. If you create a loop over two elements (indices 0 and 1) and, on the first iteration, remove one element, what will happen on the second iteration?

An alternate approach would be to build up a third data structure to hold the difference of the two sets. I think perlfaq4 has the right idea. The code might be (warning, untested!):

my @AoH_all = ( { name => "Bill", id => 1, }, { name => "Mike", id => 3 }); my @AoH_one = ( { name => "Bill", id => 1, } ); my %keep_ids = map { $_->{id} => 1 } @AoH_all; delete @keep_ids{ map { $_->{id} } @AoH_one }; my @difference = grep { exists $keep_ids{ $_->{id} } } @AoH_all;

Update: two typos fixed.

Replies are listed 'Best First'.
Re: Re: Splice of ref to AoH not removing element
by liz (Monsignor) on May 30, 2004 at 12:59 UTC
    Adding to or removing from an array you're iterating over is a bad idea. If you create a loop over two elements (indices 0 and 1) and, on the first iteration, remove one element, what will happen on the second iteration?

    However, if you would iterate in reverse order (1,0) then there would be no problem at all.

    Liz

Re: Re: Splice of ref to AoH not removing element
by bradcathey (Prior) on May 30, 2004 at 21:29 UTC

    Thanks chromatic, it looks like an elegant solution. But I'm still rather new at things like map and grep. I've been trying hard to make your code work, but am reaching the head-banging stage.

    I did add the final right curly bracket to the "delete" line:

    delete @keep_ids{ map { $_->{id} } @AoH_one }; print Dumper (%keep_ids);
    which prints:
    $VAR1 = '3'; $VAR2 = 1;

    So, I have the id of the hash I want to keep, but I'm not sure what is happening on the grep line. Also, I'm not quite sure of how to build the AoH I need for my tmpl_loop from just knowing that resulting id.

    I'm going to continue to work on it, but in the meantime, if another monk comes along that can fill in some of the details, I'm very teachable right now ;^). Thanks!


    —Brad
    "A little yeast leavens the whole dough."

      map and grep are really pretty easy, once you know the secret: read them right to left.

      map iterates over a list, executing the code block for each element in turn, returning a list of whatever the code block returns. Use it to transform one list into another.

      grep iterates over a list, executing the code block for each element in turn, returning a list of only those elements for which the code block returns true. Use it to filter out list elements.

        Thanks again chromatic. I do understand the basics you explained. I guess there is something idomatic that I'm missing. I was able to get it working fine by replacing the grep line with:
        my @difference; for my $i ( 0 .. $#AoH_all) { for my $key (keys(%keep_ids)) { if ($AoH_all[$i]{'id'} == $key ) { $difference[$i] = $AoH_all[$i]; } } }
        I'm sure that your grep line is doing just that. I just can't get it to work.
        my %difference = grep { exists $keep_ids{ $_->{id} } } @AoH_all; print Dumper (%difference);
        prints:
        $VAR1 = 'HASH(0x808bd2c)'; $VAR2 = undef;
        The deadline looms, so I must 'loop' back for another look at grepping and that last line of yours. Thanks for getting me this far!

        Update: With those edits to chromatic's grep line, it worked as intended beautifully. PM rocks.

        —Brad
        "A little yeast leavens the whole dough."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://357517]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found