Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > I can't loop properly

I can't loop properly
Thread Tools
Forum Regular
Join Date: Oct 2003
Status: Offline
Reply With Quote
Dec 20, 2005, 09:50 PM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2