[Linux-disciples] Converting an array to a hash
Adam Rosi-Kessel
adam at rosi-kessel.org
Wed Oct 6 13:31:03 EDT 2004
You could, of course, also run the command line:
find . -type f -exec cat '{}' ';' | tr " " "\n" | sort | uniq -c | sort -nr
to get the same result.
(slight modifications needed to clean up the output)
On Wed, Oct 06, 2004 at 01:24:51PM -0400, Stephen R Laniel wrote:
> 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
>
> _______________________________________________
> Linux-disciples mailing list
> Linux-disciples at lists.bostoncoop.net
> http://lists.bostoncoop.net/mailman/listinfo/linux-disciples
--
Adam Rosi-Kessel
http://adam.rosi-kessel.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/5bed93b4/attachment.pgp
More information about the Linux-disciples
mailing list