display | more...

#!/usr/bin/perl -w

# This is a simple perl script to encode and decode morse
# code.  Files for decoding must use space-delimited crypts
# of dots and dashes

# USAGE: morse (d|e) files

%decodes=(
	'.-'	=>'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',
	'.-.-.-'=>'.',	'--..--'=>',',	'---...'=>':',	'..--..'=>'?',
	'.----.'=>'\'',	'-...-'	=>'-',	'-..-.'	=>'/',	'.-..-.'=>'\"'
);

%encodes=();
while(@pair=each %decodes) {
	$encodes{$pair[1]}=$pair[0];
}

$mode=lc shift @ARGV;
decode() and exit if $mode =~ /d/;
encode() and exit if $mode =~ /e/;

sub decode {
	while(<>) {
		print ($decodes{$1} or '_') while s/^(.*?)\s+(.*)$/$2/;
		print "\n";
	}
	return 1;
}

sub encode {
	while(<>) {
		chop and $_=reverse;
		print ($encodes{uc $char} or '#') and print ' ' while $char=chop;
		print "\n"
	}
	return 1;
}




Thanks to Razhumikin for pointing out my reverse bug.