-----Original Message----- From: Gordon Low [mailto:[EMAIL PROTECTED] Sent: 09 March 2004 08:29 To: [EMAIL PROTECTED] Subject: Logical problem with small script??
Wondered if anyone can throw some light on why this script won't act properly. Wrote it to convert a string using a ceaser cipher for a course I am doing. Basically get the string and replace each character with the character offset by 3 or 4 up to 26. Have to try up to 26 times so it was crying out for a script, trouble is it doesn't work the way I intended and I cannot see how. No real attempts at error catching have been done as it was only supposed to be used by myself. Supposed to do this Enter >cipher.pl phhw dw plgqljkw ...and get all 26 different combinations to crack the message. Trouble is I get this..... qiix ex qmhrmklx -------> offset = 1 <------- qiix ex qmhrmklx -------> offset = 1 <------- rjjy fy rnisnlmy -------> offset = 2 <------- qiix ex qmhrmklx -------> offset = 1 <------- rjjy fy rnisnlmy -------> offset = 2 <------- skkz gz sojtomnz -------> offset = 3 <------- qiix ex qmhrmklx -------> offset = 1 <------- rjjy fy rnisnlmy -------> offset = 2 <------- skkz gz sojtomnz -------> offset = 3 <------- tlla ha tpkupnoa -------> offset = 4 <------- Instead of just going through once it is repeating ie loops to 1 then loops to 2 then loops to 3 up till loops to 25. Cracks the message ok at offset 23 but I don't want it looping all the time. Code follows, I am on Linux 8.0 with Perl 5.8, there is probably better ways to do this but it is driving me mad trying to see the logical problem, hoping someone can help. ******************************************************************* #!/usr/bin/perl -w use strict; my @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"); my %alphat; my $count; my $ceascount; my $op = ""; foreach (@alpha){ # set up a lookup table for the array offset $alphat{$alpha[$count]} = $count; $count++; } for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++,print $op){ # try all ceaser cipher offsets foreach (@ARGV){ # take in each command line arg for ($count=0; $count < length($_); $count++){ # go through each arg and convert with cipher $op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) % ($#alpha+1)]; # conversion } $op .= " "; # space between args } $op .= " -------> offset = $ceascount <-------\n"; # final result } # use another ceaser cipher offset ******************************************************************* Hi Gordon, The problem here not really the logic of the script. Each time you loop through on the outer for loop $op is being appended to, not over written. Each time though the loop you are processing a different offset but printing that and all previous ones. To fix this simply move the line my $op = ""; to be the first line inside the outer for loop and move the print to the end of that loop. for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++) { my $op = ""; # This means $op is empty each time through foreach (@ARGV) { for ($count=0; $count < length($_); $count++) { $op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %($#alpha+1)]; } $op .= " "; } $op .= " -------> offset = $ceascount <-------\n"; # final result print $op,"\n"; } Alternatively move the print to outside the loops all together. ie only print once. for ($ceascount = 1; $ceascount < ($#alpha +1);$ceascount++) { foreach (@ARGV) { for ($count=0; $count < length($_); $count++) { $op .= $alpha[($alphat{substr($_,$count,1)} + $ceascount) %($#alpha+1)]; } $op .= " "; } $op .= " -------> offset = $ceascount <-------\n"; } print $op,"\n"; Hope this helps, Jim -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>