Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Last active May 1, 2017 04:58
Show Gist options
  • Select an option

  • Save glinesbdev/8529abb83b8bc4f65c61880e5d160076 to your computer and use it in GitHub Desktop.

Select an option

Save glinesbdev/8529abb83b8bc4f65c61880e5d160076 to your computer and use it in GitHub Desktop.
Create hash from file
#!/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