in reply to Re: ASCII Stereograms (SIRTS)
in thread ASCII Stereograms (SIRTS)
Thanks for the suggestion, however, there is a bug in the above code. While the code correctly extracts non-repeated characters, it can use any of these characters randomly which may result in repeated characters in the line:
Here is the correct version of the program:$r .= $chars[rand @chars] for 1 .. shift;
#!/usr/bin/perl -nw # generate ascii 3-d for an input height field # input contains tabs(ts=8), spaces(h=0) or numbers 0..9 # 0 is the furthest and 9 is the closest. # Copyright 2001 -- Abdulaziz Ghuloum chomp; s/\t/' 'x8/eg; # replace tabs $_|=' 'x72; # normalize width s/ /0/g; # default depth s/(\d)/$1$1/g; # double each letter s/.//; # remove first chop; # and last $p=rs('',16); # pick a random string print $p; $p=reverse$p; s[(\d)(\d)][ # for each step if($2>$1){chop($p) for 1..$2-$1} # remove chars if going up elsif($2<$1){$p=rs($p,$1-$2)}; # add chars if going down $p =~ s/(.*)(.)/$2$1/; # rotate chars $2 # and substitute ]eg; print "$_\n"; sub rs{ my ($p,$n) = @_; return $p unless $n; my @chars = map chr, 33..126; # all printable chars @chars = grep index($p,$_)==-1, @chars if defined $p; $p .= $chars[rand@chars]; rs($p,$n-1); }
|
|---|