in reply to Re^2: Accidentally creating hash elements with grep { defined } on a hash slice
in thread Accidentally creating hash elements with grep { defined } on a hash slice
I was just asking myself exactly that question. Turns out grep requires lvalues, but sort doesn't.
>perl -MO=Concise -e"@b = grep f, @h{@a}" ... a <@> hslice lKM ->b ... >perl -MO=Concise -e"@b = sort @h{@a}" ... a <@> hslice lK ->b ...
Notice the "M" for "Modifiable" in the former.
So that begs the question: Why does grep require lvalues? The difference is:
>perl -e"@a = grep { $_='!'; 0 } @h{qw(i j)};" >perl -e"@a = sort { $a='!'; 0 } @h{qw(i j)};" Modification of a read-only value attempted at -e line 1.
But as far as I can tell, there is no good reason.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Accidentally creating hash elements with grep { defined } on a hash slice
by wol (Hermit) on Nov 04, 2008 at 13:51 UTC | |
by ikegami (Patriarch) on Nov 04, 2008 at 15:21 UTC | |
|
Re^4: Accidentally creating hash elements with grep { defined } on a hash slice
by ferment (Novice) on Nov 04, 2008 at 14:28 UTC |