yeah, the while length $_ will do it; see modified code
#!/usr/bin/env perl -l
use warnings;
use strict;
use Benchmark qw/timethese cmpthese/;
use Test::More;
my @az = ('a'..'z', 'A'..'Z', '1'..'9'); # note: if '0' is in your
+alphabet, chop() will fail
my @golden_original;
my $N = 2000;
for( 1 .. $N ) {
push @golden_original, join '', (map { $az[rand @az] } 1..$N-5),
+'0'x5;
}
#print length($str), qq(\t"$str");
#print scalar @golden_original;
#diag explain org => \@gold
+en_original;
my $wcf = [with_chop_fixed(@golden_original)]; #diag explain wcf => $w
+cf;
my $wch = [with_chop (@golden_original)]; #diag explain wch => $w
+ch;
my $wsp = [with_split (@golden_original)]; #diag explain wsp => $w
+sp;
my $wsu = [with_substr (@golden_original)]; #diag explain wsu => $w
+su;
is_deeply( $wch, $wsp, 'your chop vs split');
is_deeply( $wcf, $wsp, 'my chop vs split');
is_deeply( $wsu, $wsp, 'substr vs split');
sub with_split {
my @original = @_;
my @transposed;
for( @original ) {
my $i = 0;
$transposed[$i++] .= $_ for split //;
}
return @transposed;
}
sub with_chop {
my @original = @_;
my @transposed;
for( map $_ = reverse, @original ){
my $i = 0;
$transposed[ $i ++ ] .= chop while $_;
}
return @transposed;
}
sub with_chop_fixed {
my @original = @_;
my @transposed;
for( map $_ = reverse, @original ){
my $i = 0;
$transposed[ $i ++ ] .= chop while length $_;
}
return @transposed;
}
sub with_substr {
my @original = @_;
my $w = scalar @original;
my @transposed = ( ' 'x$w ) x $w;
for my $i ( 0 .. $#original ) {
for my $j ( 0 .. $i ) {
substr($transposed[$i], $j, 1) = substr($original[$j], $i,
+ 1);
substr($transposed[$j], $i, 1) = substr($original[$i], $j,
+ 1);
}
}
return @transposed;
}
#my $r = timethese( -5, {
# with_substr => sub { with_substr( @golden_original ); },
# with_split => sub { with_split ( @golden_original ); },
# with_chop => sub { with_chop ( @golden_original ); },
#});
#cmpthese $r;
done_testing();
| [reply] [d/l] [select] |
Thank you, pryrt, for nice analysis, and thanks for finding a bug with '0'.
| [reply] |