Advertisement
Guest User

Untitled

a guest
Feb 1st, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3.  
  4. @MORSE_VALUES = (".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
  5. "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.");
  6.  
  7. #$morse_message = "-.--....--.-.------.----....-...----------.-.-.--.--....----....--..-.-.-.-.";
  8. #$morse_message = ".-.-.-.-..--....----....--.--.-.-.----------...-....----.------.-.--....--.-";
  9. #$morse_message = "-.-.-.-.--..----....----..-..-.-.-..........---.----....-......-.-..----..-.";
  10. $morse_message = ".-..----..-.-......-....----.---..........-.-.-..-..----....----..--.-.-.-.-";
  11.  
  12.  
  13. open(WORDS, "/usr/share/dict/words");
  14. @words = <WORDS>;
  15. close(WORDS);
  16.  
  17. chomp(@words);
  18.  
  19. @words = (@words);
  20.  
  21. foreach $w (@words) {
  22. $w = uc($w);
  23.  
  24.  
  25. $mw = convertToMorse($w);
  26.  
  27.  
  28. if (index($morse_message, $mw) != -1) {
  29. print "$mw "."$w\n";
  30. }
  31. }
  32.  
  33.  
  34.  
  35. sub convertToMorse {
  36. local($str) = @_;
  37.  
  38. $morse = "";
  39.  
  40. for($i=0;$i<length($str);$i++) {
  41. $ascii = ord(substr($str, $i, 1));
  42.  
  43. if ($ascii > 64 && $ascii < 91) {
  44. $morse .= $MORSE_VALUES[$ascii - 65];
  45. }
  46. elsif ($ascii > 47 && $ascii < 58) {
  47. $morse .= $MORSE_VALUES[$ascii - 48 + 26];
  48. }
  49. }
  50.  
  51. return $morse;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement