On Mar 9, 2004, at 2:28 AM, Gordon Low wrote:

Wondered if anyone can throw some light on why this script won't act
properly.

Looks like you've already been helped with the print problem, but I thought I would add two things.


[snip]

#!/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

First, I believe there is a logic bug in the line above. I believe you meant (length($_) - 1). The warnings the program was issuing helped me find it.


        $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

And two, here's a more Perlish example of your script:


#!/usr/bin/perl

use strict;
use warnings;

my @alpha = ('a'..'z');
my %alphat;
foreach (0..$#alpha){
  $alphat{$alpha[$_]} = $_;
}

my $op = '';
for my $ceascount (1..scalar @alpha){
foreach (@ARGV) {
for my $count (0..(length($_) - 1)){
$op .= $alpha[($alphat{substr $_, $count, 1} + $ceascount) % @alpha];
}
$op .= ' '; # space between args
}
$op .= " -------> offset = $ceascount <-------\n";
}
print $op, "\n";


__END__

I'm not trying to say you need to be writing like this. I just thought you might find a shortcut or two in there you like.

Good luck.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to