Created
April 29, 2017 22:46
-
-
Save glinesbdev/9f85f9a8d9fdef529a0b59a15f7b55bb to your computer and use it in GitHub Desktop.
Unique words in lyrics
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; | |
| 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