[Linux-disciples] Converting an array to a hash

Chung-chieh Shan ccshan at post.harvard.edu
Wed Oct 6 13:20:10 EDT 2004


On 2004-10-06T13:11:36-0400, Adam Rosi-Kessel wrote:
> I doubt you'd be able to measure a performance difference between any of
> the various ways of accomplishing this.

Yeah.

> Still, if you really want to
> skip the array, this should work:
> 
> my %wordsHash;
> while (<>) {
>   foreach my $word (m/([A-Za-z]+)/g) {
>     $wordsHash{$word} = defined;
>   }
> }

You can assign multiple entries into a hash at the same time:

    @hash{1,2,3} = (4,5,6);

If the right-hand-side list is shorter than the left-hand-side, then
undef is used.  Hence you can make a hash whose keys are words and
values are undef:

    my %wordsHash;
    @wordsHash{m/([A-Za-z]+)/g} = () while <>;

(Untested code.)

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
Making it easier to be good.  The Green parties.  http://www.greens.org/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20041006/b0c44494/attachment.pgp


More information about the Linux-disciples mailing list