C
ctcgag
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
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 {$_?@$_
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