What I want to be able to do is step through either an array or a single unspaced string. I want to skip the leading "-", and starting from the first letter, extract three letters; the first "-" before the first letter, the first letter, and the letter following the first letter.
Then I want to move to the second letter and extract three more letters; the letter before the second letter, the second letter, and the letter following the second letter.
I want to continue in this manner until the final character, the "-" comes up. I don't want to extract the letter before, and the nothing after.
I want the final output to look like:
-FD
FDR
.}
.} intermediate trigraphs
.}
VZR
ZR-
In this particular case, the text has 80 characters and I add the before and after dashes for my convenience. However, I want this program to be able to work with longer length text.
I have tried but I am unable to solve this problem. I feel it should be routine, and that's what frustrates me. How can this be re-coded?
I am a novice using Machten, (BSD 4.4 lite with Perl 5.004) on an old Mac. I don’t know how to upgrade to a later Perl version, so I can’t use modules and other good stuff. This version of perl is good enough for me to learn on and it ought to do what I want it to do which is basically text manipulation.
I have removed the line numbers so you can copy the program into your editor with the least amount of bother. I know you can add the numbers then.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%
Why does it only print out 41 lines instead of 81???
When I comment out the shift on line 37, 82 lines print.
I would also put a exit on the loop so the final "-" doesn't get looped on.
#!/usr/bin/perl -w
# trigraph - extracts the first three characters, shifts to the second
# character and extracts three more characters. continues to
# the next to the last character.
#
# USAGE: perl -F trigraph (call it whatever you want)
use strict;
undef $/; # enable "slurp" mode
my $text = $_;
while(<DATA>) {
s/ //g; # strip spaces from file
s/\n//g; # strip newlines from file
$text = $_;
chomp $text; # $text contains one long word
}
print "tp1\n";
print $text, "\n";
my @text = split(//, $text); # @text contains 80 single spaced letters
unshift(@text, "-"); # add a "-" to front of data file
push(@text, "-"); # add a "-" to end of data file
print "tp2\n";
print "@text\n"; # @text contains 82 single spaced characters
print "tp3\n";
print "There are " . ($#text -1) . " characters", "\n";
print "tp4\n";
print "The 39th character is $text[38]\n"; # this shows that the chars are there
print "The 51st character is $text[50]\n"; # to be deleted later
print "The 82nd character is $text[81]\n"; #
my $i = 0;
for($i=0; $i <= $#text; $i++) {
my @tri = @text[0,1,2];
print @tri, "\n";
shift(@text);
push(@text);
}
__DATA__
FDRJN UHVXX URDMD SKVSO PJRKZ DYFZJ
XGSRR VTQYR WDARW DFVRK VDRKV TDFSZ
ZDYFR DNNVO VTSXS AWVZR