As practice I've wrote a script to translate text input into Morse Code.
To begin any Perl script you need to include the path Perl is installed on your pc. So since I use Windows, this is accomplished like so:
#C:/Perl/bin/perlThis will of course vary depending on the path its installed. For instance, if you happen to be using linux, it might look like this:
#!/usr/bin/perl
Easy enough, right?
To declare an array you simply enter what you want to name the array with a "@" in front of it.
An example being:
@names = ("bob", "tim", "jake", "pete");
To declare a variable you place a "$" in the front.$x = 1;To output the contents of the array you simply use the "print" command followed by by the name of the array.
print @names; print "@names";Notice the use of quotations. It is generally safe practice to place arrays that hold strings inside them for they produce completely different results. Look at this code
$x = @names; # $x now equals the length of '@names' $x = "@names"; # $x is now the contents of '@names' as a string
Alright so those are the very fundamentals. Now to the script. I'm sure there is a more efficient way to accomplish this, but this is merely my solution.
#!C:/Perl/bin/perl
@morse = (".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-.-", "--..--", "..--..".
".----.", "-.-.--", " ");
@alpha = ("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", ".", ",", "?", "'", "!", "/", " ");
print "Enter File text file path\n\n";
$r = 500;
$file = <STDIN>;
open(FILE, $file) or die "can't open file";
@lines = <FILE>;
close(FILE);
chomp(@lines);
$line = "@lines";
@chars = split(//, $line);
foreach $letter(@chars)
{
$x = 0;
while ($letter ne @alpha[$x])
{
$x++;
last if ($x == $r);
}
$new = $morse[$x];
print "$new ";
}
Explanation of code:
@morse = (".-","-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-.-", "--..--", "..--..".
".----.", "-.-.--", " ");
@alpha = ("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", ".", ",", "?", "'", "!", "/", " ");
The Morse code alphabet and the regular alphabet. Each Morse code letter corresponds with position the of letter. So @morse[1] is equal to @alpha[1]. If you want to make a more functional converter simply add more character to the area.
$file = <STDIN>; # Gets user input $r = 500; # A variable for a simple break function open(FILE, $file) or die "can't open file"; # Opens File, if the file doesn't exist it catches the error
chomp(@lines); # Deletes the invisible new line character from each line $line = "@lines"; # Puts the contents of the text document into a single string @chars = split(//, $line); #Puts each letter into an individual element in @chars. The "//" indicates split by letter.
foreach $letter(@chars) #Cycles through the array. Say that the inputted text was 10 letters long, then this would repeat 10 times.
while ($letter ne @alpha[$x]) #This means : "While the current letter is not equal to the letter
#in the 'x' element of @alpha, then..."
# ne is used for comparing strings
{
$x++; # increment x variable
last if ($x == $r); # Prevents a never ending loop if the character isn't in the
# "library"
This is only a brief look at Perl's functionality. Perl can do much more in regards to strings and arrays.For instance, many times you would want to concatenate strings. To do so you would place a "." between the variables.
$bill = "will"; $formal = "iam"; print "$bill.$formalThis of course results in "william".
Many times you will want to add, or delete elements of an array. To do so you use POP and PUSH. The syntax for both is very simple.
@things = ("ball", "bat", "glove");
push("hat", @things)
Now @things equals ball, bat, glove, hat.You can even push other arrays into an array.
@first = ("bob", "bill", "jill", "will");
@last = ("smith", "doe", "johnson");
push(@last, @first);
THE END
I wish I could line up the comments though...


Sign In
Create Account


Back to top









