back

Morse.pl

This one converts a string into morse code.

usage: perl morse.pl string
(string needs to be quoted so that it's treated as a single argument)
#!/usr/bin/perl --

#	convert a string into morse code

$istring = $ARGV[0];
$istring_len = ( length ($istring) ) - 1;

$istring = lc $istring;

for $offset (0 .. $istring_len) {
	$morsevalue = get_morse ( substr ( $istring, $offset, 1) );
	print "$morsevalue ";
	}
	
print "\n";
exit(0);

sub get_morse {
	$letter = $_[0];
	if ($letter eq "a") { return ".-" };
	if ($letter eq "b") { return"-..." };
	if ($letter eq "c") { return "-.-." };
	if ($letter eq "d") { return "-.." };
	if ($letter eq "e") { return "." };
	if ($letter eq "f") { return "..-." };
	if ($letter eq "g") { return "--." };
	if ($letter eq "h") { return "...." };
	if ($letter eq "i") { return ".." };
	if ($letter eq "j") { return ".---" };
	if ($letter eq "k") { return "-.-" };
	if ($letter eq "l") { return ".-.." };
	if ($letter eq "m") { return "--" };
	if ($letter eq "n") { return "-." };
	if ($letter eq "o") { return "---" };
	if ($letter eq "p") { return ".--." };
	if ($letter eq "q") { return "--.-" };
	if ($letter eq "r") { return ".-." };
	if ($letter eq "s") { return "..." };
	if ($letter eq "t") { return "-" };
	if ($letter eq "u") { return "..-" };
	if ($letter eq "v") { return "...-" };
	if ($letter eq "w") { return ".--" };
	if ($letter eq "x") { return "-..-" };
	if ($letter eq "y") { return "-.--" };
	if ($letter eq "z") { return "--.." };
	if ($letter eq ".") { return ".-.-.-" };
	if ($letter eq ",") { return "--..--" };
	if ($letter eq "?") { return "..--.." };
	if ($letter eq "/") { return "-..-." };
	if ($letter eq "@") { return ".--.-." };
	if ($letter eq "1") { return ".----" };
	if ($letter eq "2") { return "..---" };
	if ($letter eq "3") { return "...--" };
	if ($letter eq "4") { return "....-" };
	if ($letter eq "5") { return "....." };
	if ($letter eq "6") { return "-...." };
	if ($letter eq "7") { return "--..." };
	if ($letter eq "8") { return "---.." };
	if ($letter eq "9") { return "----." };
	if ($letter eq "0") { return "-----" };
	if ($letter eq " ") { return "" };
	return " ";
}

All this code is © 2006 - 2015, except for the noted pieces which are © to their authors as noted. Feel free to use anything you find here, I'm not that precious about it. Take it, improve it, make lots of cash with it, it's a gift.

back