A couple of comments:
1) the open || die statement should actually be
open IN, $infile or die "Can't open input file: $!";
For reasons of precedence.
2) Here are some faster algorithms. One way is to use a
hash to record which characters you've already placed
into your substitution string. Here's the main loop:
my %set;
my $substit = "";
for (1..26) {
my $randchar;
do { $randchar = chr((int rand 26) + 65) }
while $set{$randchar}++;
$substit .= $randchar;
}
This way you don't have to do a search through the
string each time.
And here's an even better way of doing it: build a
random permutation of the alphabet string. To do this,
we'll actually need an array:
my @alpha = split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Now we set another array equal to this one, then call
the random shuffle algorithm on it:
my @crypt = @alpha;
fisher_yates_shuffle(\@crypt);
which shuffles the array in place. Now all you need to do
is get back the substitution string:
my $substit = join '', @crypt;
Here's the definition of the fisher_yates_shuffle sub:
sub fisher_yates_shuffle {
my $array = shift;
for (my $i = @$array; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i, $j] = @$array[$j, $i];
}
return join '', @$array;
}
(Taken directly from perlfaq4.)
I did some benchmarking on these, and here's what I got:
Benchmark: timing 5000 iterations of orig, f_yates, hash...
orig: 33 secs (23.60 usr 0.00 sys = 23.60 cpu)
f_yates: 5 secs ( 2.88 usr 0.00 sys = 2.88 cpu)
hash: 13 secs ( 5.68 usr 0.00 sys = 5.68 cpu)
where "orig" is the one you posted, "f_yates" is the one
using fisher_yates_shuffle, and "hash" is the one using a
hash to record seen characters.
If I've messed anything up, let me know.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.