Thread letter swapping (13 answers)
Opened by havi at 2003-09-19 21:18

Ronnie
 2003-09-20 01:16
#18540 #18540
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
Ich mach mit:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl

use strict;
use warnings;

while(<>) {
s/\b(\w+)\b/&wortspiel($1)/ge;
print;
}

sub wortspiel {
if ($_[0] =~ m/\b(\w)(\w+)(.)\b/) {
my @letters = split(//, $2);
shuffle (\@letters);
return $1.join ('', @letters).$3;
}
else {
return $_[0];
}
}

sub shuffle {

# vgl. O'Reilly, Perl-Kochbuch dt., 1. Auflage, 4.17, fisher-yates-shuffle

my $array = shift;
my $i;

for ($i = @$array; --$i; ) {
my $j = int rand ( $i+1 );
next if $i == $j;
@$array[$i, $j] = @$array[$j, $i];
}
}


Gruss,
Ronnie

View full thread letter swapping