Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Preserve aliasing with Map

Reply
Thread Tools

Preserve aliasing with Map

 
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      05-27-2004
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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-27-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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\\
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM
map that maps to iterators in the same map ? Vlad C++ 0 12-15-2003 08:29 PM
FileUtils - :preserve does not preserve mtime of directories on Windoze Grzegorz Chrupala Ruby 2 06-30-2003 07:38 AM



Advertisments