Preserve aliasing with Map

Discussion in 'Perl Misc' started by ctcgag@hotmail.com, May 27, 2004.

  1. Guest

    I had a script whose inner workings (greatly simplified) worked like this:

    foreach my $r (@record) {
    my @codes=something($r);
    foreach my $entry ( grep foo($_), @entry_hash{@codes} ) {
    next unless defined $entry;
    ## do some stuff here
    undef $entry;
    };
    }

    Since a hash slice gives L-values, and since both grep and foreach do
    aliasing, the last line of the inner loop actually changed each value (in
    %entry_hash) on which it did work, so that later I could figure out if any
    thing in %entry_hash were "orphans" that didn't get operated on.

    Later, I needed to change it so that each slot in %entry_hash held a list
    of entries, rather than one entry. But this code:

    foreach my $r (@record) {
    my @codes=something($r);
    foreach my $entry ( grep foo($_), map {$_?@$_:()} @entry_hash{@codes} ) {
    next unless defined $entry;
    ## do some stuff here
    undef $entry;
    };
    }

    Did not work, because the map broke the aliasing chain and so the change to
    $entry would not be reflected in the original data structure. I ended up
    adding an extra foreach loop between the inner and outer ones, but I wonder
    of there was a more elegant way to preserve the aliasing chain.

    Thanks,

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 27, 2004
    #1
    1. Advertisements

  2. writes:

    > I had a script whose inner workings (greatly simplified) worked like this:


    > foreach my $entry ( grep foo($_), @entry_hash{@codes} ) {


    > Later, I needed to change it so that each slot in %entry_hash held a list
    > of entries, rather than one entry. But this code:


    > foreach my $entry ( grep foo($_), map {$_?@$_:()} @entry_hash{@codes} ) {


    > Did not work, because the map broke the aliasing chain and so the change to
    > $entry would not be reflected in the original data structure.


    This is a problem I've encountered a number of times.

    > I ended up adding an extra foreach loop


    That's what I usually do.

    > but I wonder of there was a more elegant way to preserve the
    > aliasing chain.


    You could switch to using references.

    foreach my $entry ( grep foo($$_), map {$_ ? \(@$_) : () } @entry_hash{@codes} ) {

    Now a change to $$entry will be reflected in the original data structure.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, May 27, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    865
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    9,201
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    526
  4. Patrick Guio
    Replies:
    6
    Views:
    3,551
    chris
    Oct 20, 2004
  5. Erik Arner
    Replies:
    0
    Views:
    1,463
    Erik Arner
    Nov 2, 2004
  6. ma740988

    map within map

    ma740988, Nov 7, 2004, in forum: C++
    Replies:
    0
    Views:
    3,026
    ma740988
    Nov 7, 2004
  7. Noixe

    Help, Map of map

    Noixe, Jan 1, 2005, in forum: C++
    Replies:
    6
    Views:
    567
    =?iso-8859-1?B?Sm9hcXXtbiBNIEzzcGV6IE118W96?=
    Jan 1, 2005
  8. Grzegorz Chrupala
    Replies:
    2
    Views:
    373
    Grzegorz Chrupala
    Jun 30, 2003
Loading...