[Linux-disciples] Converting an array to a hash
Stephen R Laniel
steve at laniels.org
Wed Oct 6 13:24:51 EDT 2004
On Wed, Oct 06, 2004 at 01:20:10PM -0400, Chung-chieh Shan wrote:
> 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 <>;
You guys have convinced me that it doesn't much matter. I
actually think the code ends up being more literate in any
case if I use an array *and* a hash.
My needs are actually a bit more complicated than I thought.
I ended up deciding to turn the word list into a frequency
distribution of words, so that the meat looks like
%wordsHash = ();
File::find( some stuff, wanted => \&wanted );
and the meat of wanted() is
@words = m/([A-Za-z]+)/g;
foreach my $word (@words) {
%wordsHash{$word}++;
}
I doubt there's a quicker way to do this. And in any case,
@word only ever contains the words from a single file
(whereas %wordsHash contains all the words from all the
files that the wanted() function hit), so @words will never
be all that large. Not enough to worry about.
Thanks for all your input.
--
``I am all about the delivery of *justice* to
parking, people. That is all.''
-Jon Sung, 27 September 2004
-------------- 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/93ac390e/attachment.pgp
More information about the Linux-disciples
mailing list