Guest User

Untitled

a guest
May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use feature qw(switch say);
  6. use Data::Dumper;
  7.  
  8. my $file = $ARGV[0] or die("Please specify a file to read\n");
  9.  
  10. if (!-e $file) {
  11. die("File '".$file."' doesn't exist\n");
  12. }
  13.  
  14. my %words;
  15.  
  16. open(my $file_h, $file);
  17. my $content = join(' ', <$file_h>);
  18.  
  19. for (split(' ', $content)) {
  20. $words{length($_)} = $_;
  21. }
  22.  
  23. my @keys = sort(keys(%words));
  24.  
  25. say "The longest word was $words{$keys[-1]} with $keys[-1] characters"
Add Comment
Please, Sign In to add comment