Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Created April 29, 2017 22:46
Show Gist options
  • Select an option

  • Save glinesbdev/9f85f9a8d9fdef529a0b59a15f7b55bb to your computer and use it in GitHub Desktop.

Select an option

Save glinesbdev/9f85f9a8d9fdef529a0b59a15f7b55bb to your computer and use it in GitHub Desktop.
Unique words in lyrics
#!/usr/bin/perl
use strict;
use warnings;
my $lyric_file = $ARGV[0];
my @unique_words = [];
my $total_words = 0;
open(my $lyrics, '<:encoding(UTF-8)', $lyric_file)
or die "Could not open file '$lyric_file' $!";
open(my $unique_file, '>:encoding(UTF-8)', 'unique_file.txt')
or die "Cannot open file 'unique_file.txt' $!\n";
while (my $row = <$lyrics>) {
my @mod_row = split(/ /, $row);
foreach my $i (0 .. $#mod_row) {
$total_words++;
unless (grep(/^$mod_row[$i]$/, @unique_words)) {
my $word = $mod_row[$i];
$word =~ s/,//g;
push @unique_words, $word;
}
}
}
print $unique_file join(" ", @unique_words);
print $unique_file "There are " . scalar @unique_words . "/$total_words unique words in the song provided.\n";
close($unique_file);
close($lyrics);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment