1
$\begingroup$

I have

Thread[Alphabet[] -> morseAlphabet[]]

But I don't get morse code for each letter, instead:

{"a" -> morseAlphabet[], "b" -> morseAlphabet[], 
 "c" -> morseAlphabet[], "d" -> morseAlphabet[], 
 "e" -> morseAlphabet[], "f" -> morseAlphabet[], 
 "g" -> morseAlphabet[], "h" -> morseAlphabet[], 
 "i" -> morseAlphabet[], "j" -> morseAlphabet[], 
 "k" -> morseAlphabet[], "l" -> morseAlphabet[], 
 "m" -> morseAlphabet[], "n" -> morseAlphabet[], 
 "o" -> morseAlphabet[], "p" -> morseAlphabet[], 
 "q" -> morseAlphabet[], "r" -> morseAlphabet[], 
 "s" -> morseAlphabet[], "t" -> morseAlphabet[], 
 "u" -> morseAlphabet[], "v" -> morseAlphabet[], 
 "w" -> morseAlphabet[], "x" -> morseAlphabet[], 
 "y" -> morseAlphabet[], "z" -> morseAlphabet[]}

So yes, I am a learner. I imagine I am not indexing right. Can someone nudge me in the right direction?

$\endgroup$
2

1 Answer 1

4
$\begingroup$

It seems OP has a definition like this:

morseAlphabet[] := {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}

Then here is the result of OP's code :

Thread[Alphabet[] -> morseAlphabet[]]

(* {"a" -> ".-", "b" -> "-...", "c" -> "-.-.", "d" -> "-..", 
 "e" -> ".", "f" -> "..-.", "g" -> "--.", "h" -> "....", "i" -> "..", "j" -> ".---", "k" -> "-.-", "l" -> ".-..", "m" -> "--", "n" -> "-.", "o" -> "---", "p" -> ".--.", "q" -> "--.-", "r" -> ".-.", 
 "s" -> "...", "t" -> "-", "u" -> "..-", "v" -> "...-", "w" -> ".--", "x" -> "-..-", "y" -> "-.--", "z" -> "--.."} *)

Using the WL code from "Create and Decode a Morse Code Signal":

code = <|"a" -> ".-", "b" -> "-...", "c" -> "-.-.", "d" -> "-..", 
  "e" -> ".", "f" -> "..-.", "g" -> "--.", "h" -> "....", "i" -> "..",
   "j" -> ".---", "k" -> "-.-", "l" -> ".-..", "m" -> "--", 
  "n" -> "-.", "o" -> "---", "p" -> ".--.", "q" -> "--.-", 
  "r" -> ".-.", "s" -> "...", "t" -> "-", "u" -> "..-", "v" -> "...-",
   "w" -> ".--", "x" -> "-..-", "y" -> "-.--", "z" -> "--..", 
  "1" -> ".----", "2" -> "..---", "3" -> "...--", "4" -> "....-", 
  "5" -> ".....", "6" -> "-....", "7" -> "--...", "8" -> "---..", 
  "9" -> "----.", "0" -> "-----", "." -> ".-.-.-", "," -> "--..--", 
  "!" -> "-.-.--", "?" -> "..--.."|>

Here we thread into rules:

Thread[Alphabet[] -> Values[KeyTake[code, Alphabet[]]]]

(* {"a" -> ".-", "b" -> "-...", "c" -> "-.-.", "d" -> "-..", "e" -> ".", "f" -> "..-.", "g" -> "--.", "h" -> "....", "i" -> "..", 
 "j" -> ".---", "k" -> "-.-", "l" -> ".-..", "m" -> "--", "n" -> "-.", "o" -> "---", "p" -> ".--.", "q" -> "--.-", "r" -> ".-.", 
 "s" -> "...", "t" -> "-", "u" -> "..-", "v" -> "...-", "w" -> ".--", "x" -> "-..-", "y" -> "-.--", "z" -> "--.."}*)
$\endgroup$
2
  • $\begingroup$ Thank you, and would there be a way to use the morseAlphabet (you were right, I did not define it)? When I do Thread[Alphabet[] -> morseAlphabet[]] I don't get the mapping you get, but {"a" -> {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}[] and so on. I think I am missing something. $\endgroup$ Commented Apr 28, 2023 at 6:10
  • $\begingroup$ It seems you have not defined morseAlphabet correctly. Quit the kernel, then copy, paste, and execute the code in my answer. $\endgroup$ Commented Apr 29, 2023 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.