Last active
May 1, 2017 04:58
-
-
Save glinesbdev/8529abb83b8bc4f65c61880e5d160076 to your computer and use it in GitHub Desktop.
Create hash from file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| sub read_line { | |
| our %hash; | |
| my @list = split(" ", shift); | |
| foreach my $word (@list) { | |
| $hash{$word}++; | |
| } | |
| } | |
| sub read_file { | |
| my $file = shift; | |
| open (FILE, $file) or die "Cannot open file: '$file' $!\n"; | |
| while (my $line = <FILE>) { | |
| read_line $line; | |
| } | |
| } | |
| sub print_hash { | |
| our %hash; | |
| my @list = %hash; | |
| print "@list\n"; | |
| } | |
| sub print_keys { | |
| our %hash; | |
| my @list = keys %hash; | |
| print "@list\n"; | |
| } | |
| sub print_values { | |
| our %hash; | |
| my @list = values %hash; | |
| print "@list\n"; | |
| } | |
| sub count_keys { | |
| our %hash; | |
| my @list = keys %hash; | |
| print scalar @list . "\n"; | |
| } | |
| sub count_values { | |
| our %hash; | |
| my @list = values %hash; | |
| print scalar @list . "\n"; | |
| } | |
| read_file "disturbed_prayer_lyrics.txt"; | |
| print_hash; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment