in reply to why use a hash instead of an array
Hi DarrenSol
Wonderful answers has been given, but maybe this little example / question could also "say" something.
If I give the following:
And I ask that for these:teacher students professor students school class teacher desk table chalk borad class school teacher
1. print out all the words, without repeating any, 2. print out all the repeated words only, 3. print out the number of times each words is seen and probably an illustration this with an histogram.
How would you do these.
Let me answer QnS 1. and 2., allow you to figure question 3 and try your hands on other solution WITHOUT USING HASH
Solution:use strict; use warnings; my %hash; while(<DATA>){ chomp; $hash{$_}++ for split; } print join $/ => sort keys %hash; print $/,$/, join $/ => grep {$_ if $hash{$_} > 1} sort keys %hash; __DATA__ teacher students professor students school class teacher desk table chalk borad class school teacher
-
borad chalk class desk professor school students table teacher
-
class school students teacher
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: why use a hash instead of an array
by 5mi11er (Deacon) on Jun 11, 2013 at 21:18 UTC | |
Re^2: why use a hash instead of an array
by Lotus1 (Vicar) on Jun 12, 2013 at 19:04 UTC | |
by LanX (Saint) on Jun 12, 2013 at 21:53 UTC | |
by Lotus1 (Vicar) on Jun 12, 2013 at 22:54 UTC | |
by LanX (Saint) on Jun 12, 2013 at 23:09 UTC |
In Section
Seekers of Perl Wisdom