use strict; use warnings; use Getopt::Long; use File::Slurp; my $filename = 'text.txt'; my $min_word_length = 3; my $start_word = 0; my $words_to_display = 10; GetOptions( "file=s" => \$filename, "min=i" => \$min_word_length, "start=i" => \$start_word, "words=i" => \$words_to_display ); my $original_text = lc read_file( $filename ); $original_text =~ tr/()//d; my @words = grep /[a-z]{$min_word_length,}/i, split /[,.\s]+/, $original_text; my %word_counter; $word_counter{ $_ } ++ for @words; @words = sort { $word_counter{ $a } <=> $word_counter{ $b } || $a cmp $b } @ words; my @words_to_print = @words[ $start_word .. ( $start_word + $words_to_display - 1 ) ]; for my $word ( @words_to_print ) { print "$word: $word_counter{ $word }\n"; }