($k, $v) <== pop %hash;
or
($k, $v) <== %hash.pop;
make sense to anyone except me?
Since we now have an explicit concept of pairs, one could consider a
hash to be nothing but an unordered (but well indexed) list of pairs.
So, C<< pop %hash >> would be a lot like C<< each >>, except, of course,
that it deletes the pair at the same time.
If we do that, I'd also want to be able to
push %x, %y;
which would mean something like:
%x{%y.keys} <== %y{%y.keys};
but be much easier to read.
-- Rod Adams.
(And now I'm really off to bed.)
Makes sense to me. Although I would be more inclined to think of pop as
returning a pair - but does a pair in list context turn into a list of
key, value? If so then the above makes lots of sense.
> Since we now have an explicit concept of pairs, one could consider a
> hash to be nothing but an unordered (but well indexed) list of pairs.
> So, C<< pop %hash >> would be a lot like C<< each >>, except, of course,
> that it deletes the pair at the same time.
The only thing people might not be too pleased about is that the order
is entirely at the whim of the internal implementation of hashes. I
suppose you could say %hash.sort.pop(), but that would probably re-sort
every time and that's clearly not desirable. Working on a sorted copy is
also not particularly pleasant as memory could be a considerable problem.
> If we do that, I'd also want to be able to
>
> push %x, %y;
>
> which would mean something like:
>
> %x{%y.keys} <== %y{%y.keys};
>
> but be much easier to read.
Yes, I'd like that. I find myself wanting to do things like that quite a
lot in Perl 5.
> Rod Adams wrote:
>> Does
>> ($k, $v) <== pop %hash;
>> or
>> ($k, $v) <== %hash.pop;
>> make sense to anyone except me?
>
> Makes sense to me. Although I would be more inclined to think of pop
> as returning a pair - but does a pair in list context turn into a list
> of key, value? If so then the above makes lots of sense.
Seeing the above, I first thought up a "pair context":
($k => $v) = %hash.pop;
However ... IIRC, in list assignment, lvalue pairs are treated as
name/value pairs, so there would be a list assignment of just one
element here. And this is still list assignment ...
Better to be explicit, I suppose:
($k, $v) = %hash.pop.kv;
>> If we do that, I'd also want to be able to
>> push %x, %y;
>> which would mean something like:
>> %x{%y.keys} <== %y{%y.keys};
>> but be much easier to read.
>
> Yes, I'd like that. I find myself wanting to do things like that quite
> a lot in Perl 5.
%x.push(%y);
If no one else writes it, I will. ;-)
Eirik
--
A free society is one where it is safe to be unpopular.
-- Adlai Stevenson
It's clear to me.
The only thing is that, right off the top of my head, I can't see
where it would be used. Since the order in which the pairs were
returned would be effectively random, you can't use this to remove and
process a specific element. So, really, the only time it's useful is
if you want to process all the elements in the hash--in which case,
why not just use C< each >?
--Dks
--
dst...@dstorrs.com
That's true for the PerlHash implementation, but what about
OrderedHash? Or LookupQueue? Still seems like a useful trick.
Ashley
Good point. I'm glad you pointed that out, because it felt like this
should be useful...glad to see it was just my imagination that was at
fault. :>
--Dks