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. Advertising

  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. Advertising

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. Tim Tyler

    LCD anti-aliasing in Java

    Tim Tyler, Sep 4, 2003, in forum: Java
    Replies:
    2
    Views:
    1,283
    Tim Tyler
    Sep 5, 2003
  2. Kevin Bertman

    Anti-aliasing GIF Images

    Kevin Bertman, Nov 26, 2004, in forum: Java
    Replies:
    4
    Views:
    745
    marcus
    Nov 29, 2004
  3. Wesley T Perkins

    Aliasing a class name?

    Wesley T Perkins, Jun 28, 2005, in forum: Java
    Replies:
    8
    Views:
    1,954
    John Currier
    Jul 1, 2005
  4. Roedy Green

    More anti-aliasing puzzles

    Roedy Green, Aug 10, 2005, in forum: Java
    Replies:
    25
    Views:
    2,187
    Roedy Green
    Aug 16, 2005
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    203
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page