develooper Front page | perl.perl5.porters | Postings from June 2013

Re: key/value hash and index/valuse array slices syntax

Thread Previous | Thread Next
From:
Eric Brine
Date:
June 10, 2013 21:55
Subject:
Re: key/value hash and index/valuse array slices syntax
Message ID:
CALJW-qGczF0HoqQGA0c2xAVEv-8LRXt3a52e_hkeMGjmxdXQxw@mail.gmail.com
oops, I didn't notice you switched from m// to qr//.

But qr// does something useful too, contrary to what you said. I've seen
hashes keyed by regex patterns.

$hash{$re} = [ /$re/ ];



On Mon, Jun 10, 2013 at 5:53 PM, Eric Brine <ikegami@adaelis.com> wrote:

> On Mon, Jun 10, 2013 at 4:17 PM, Lukas Mai <plokinom@gmail.com> wrote:
>
>> On 10.06.2013 13:33, Dave Mitchell wrote:
>>
>>> On Sun, Jun 09, 2013 at 06:51:23PM -0500, David Nicol wrote:
>>>
>>>> one of the perl6 RFCs suggested allowing a regex as a hash index
>>>>
>>>>     @hash{/REGEX/}
>>>>
>>>> to mean
>>>>
>>>>    @hash{ grep /REGEX/, keys %hash }
>>>>
>>>
>>> but that syntax already has a meaning:
>>>
>>>      %h = qw(aa 1 bb 2 cc 3);
>>>      $_ = 'aa bb';
>>>      print @h{/(\w+)/};  # prints 1
>>>      print @h{/(\w+)/g}; # prints 12
>>>
>>>
>> Right, but @hash{qr/REGEX/} could work. Currently it stringifies the
>> regex object and uses that as a hash key, but that's not very useful IMHO.
>>
>
> No, it doesn't. Please reread the post to which you replied. There's no
> stringification of the pattern, It's accessing the keys whose name is the
> value of $1, $2, etc. after performing a match operation (against $_).
>
>
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About