[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] Sideways Perl



Here's a quick rot-45, or -90 or whatever, based on a very simple-
minded approach. Maybe it should be called rot-in-hell...

Give skew() any list of lists, and any integer args. Minutes of
fun for the whole family...


my @matrix = map [split ' '], split /\n/, <<_EOM_;
A B C D
E F G H
I J K L
M N O P
_EOM_

print skew(\@matrix, 1, 1, 1, -1);

sub skew {
  my ($matrix, $xi, $xj, $yi, $yj) = @_;
  my $p = Pic->new;
  my ($i, $j) = (0,0);
  for (@$matrix) {
    my ($ii, $jj) = ($i, $j);
    for (@$_) {
      $p->insert($ii, $jj, $_);
      $ii += $xi;
      $jj += $xj;
    }
    $i += $yi;
    $j += $yj;
  }
  $p->as_string;
}



### a limited ASCII "picture" class

package Pic;

sub new {
  bless [[], [0,0]], $_[0];
}

sub insert {
  my $self = shift;
  my ($ox, $oy) = @{$self->[1]};
  my ($x, $y, $c) = @_;

  if ($x < $ox) {
    unshift @{$self->[0]}, map [], 1..($ox - $x);
    $ox = $x;
  }
  if ($y < $oy) {
    for (@{$self->[0]}) {
      unshift @$_, map " ", 1..($oy - $y);
    }
    $oy = $y;
  }
  @{$self->[1]} = ($ox, $oy);
  $self->[0][$x-$ox][$y-$oy] = $c;
  $self;
}

sub as_string {
  my $self = shift;
  my $ret = '';
  for (@{$self->[0]}) {
    $ret .= join "", map {$_ || " "} @{$_ || []}, "\n";
  }
  $ret;
}


__END__
Tushar Samant
~
~
:wq


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe