# Is there a more efficient way of setting up rules from large lists? [closed]

I wrote a code for converting a string into a Morse code.

convert[text_String] :=
StringJoin[
Map[
Replace[
{"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" -> "--.. ", " " -> "/ "}],
Characters[text]]]


Example

convert["a ladybug loves sleeping"]


gives

.- / .-.. .- -.. -.-- -... ..- --. / .-.. --- ...- . ... / ... .-.. . . .--. .. -. --.

I'm getting the correct output. However, when I coded my list of rules, I typed in every rule ("a"->".- ", "b"->"-... ", etc.) individually. I am wondering, how could I produce a set of rules in a more efficient way. Say I had a thousand of rules to create from two existing lists, making a list of rules from the two list by hand would be too tedious.

## closed as unclear what you're asking by m_goldberg, garej, yohbs, MarcoB, Mr.Wizard♦Jun 7 '17 at 13:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Thread[list1->list2]? – Kuba Jun 6 '17 at 18:40
• I believe your question is something like: "If I had a really large alphabet how could I automate this list creating process?" Well, if I understood you correctly, the morse code is a convention, so no easy way for that. But if you accept using binary instead of morse things get interesting. – ivbc Jun 6 '17 at 19:58
• Incidentally this should be written using StringReplace as it will be much faster, i.e. convert[text_String] := StringReplace[text, { (* rules *) }] – Mr.Wizard Jun 8 '17 at 7:00

I think this is what you are looking for:
Given that you already have your lists

morseAlphabet = {"·−","−···","−·−·","−··","·","··−·","−−·","····","··","·−−−","−·−","·−··","−−","−·","−−−","·−−·","−−·−","·−·","···","−","··−","···−","·−−","−··−","−·−−","−−··"}


and

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"};


You can use

Thread[alphabet -> morseAlphabet]


to create your replacement rules.

StringJoin[Characters["A LADYBUG LOVES SLEEPING"] /.Thread[alphabet -> morseAlphabet]]


Would then be equivalent to your function convert[text_String]

• Yes, that was the function I was looking for! Thanks! – Nigi Tanka Jun 7 '17 at 21:40