#!/usr/bin/perl -w use strict; our $JOIN = "\t"; =head1 TESTS test( 'flip_horizontal' ); test( 'flip_vertical' ); test( 'rotate_minus90' ); test( 'rotate_plus90' ); test( 'rotate180' ); test( 'flip_diag_backslash' ); test( 'flip_diag_forwardslash' ); sub test { my $func = shift; no strict 'refs'; print "\n\n$func()\n", ('-'x (length($func)+2)), "\n"; &$func( "c:/data.txt", "c:/data-transpose.txt" ); print readfile("c:/data.txt"); print "\n\n"; print readfile("c:/data-transpose.txt"); } sub readfile { my $file = shift; local $/; open F, $file or die "Can' read $file\n"; my $data = ; close F; return $data; } =head2 RESULTS flip_horizontal() ----------------- 11 12 13 21 22 23 31 32 33 13 12 11 23 22 21 33 32 31 flip_vertical() --------------- 11 12 13 21 22 23 31 32 33 31 32 33 21 22 23 11 12 13 rotate_minus90() ---------------- 11 12 13 21 22 23 31 32 33 13 23 33 12 22 32 11 21 31 rotate_plus90() --------------- 11 12 13 21 22 23 31 32 33 31 21 11 32 22 12 33 23 13 rotate180() ----------- 11 12 13 21 22 23 31 32 33 33 32 31 23 22 21 13 12 11 flip_diag_backslash() --------------------- 11 12 13 21 22 23 31 32 33 11 21 31 12 22 32 13 23 33 flip_diag_forwardslash() ------------------------ 11 12 13 21 22 23 31 32 33 33 23 13 32 22 12 31 21 11 =cut sub flip_horizontal { my ( $infile, $outfile ) = @_; open IN, $infile or die "Can't read $infile $!\n"; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; while( ) { chomp; print OUT join $JOIN, reverse(split ' '),"\n"; } close IN; close OUT; } sub flip_vertical { my ( $infile, $outfile ) = @_; require File::ReadBackwards; tie *BW, 'File::ReadBackwards', $infile or die "Can't read $infile $!\n"; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; while( ) { chomp; print OUT join "\t", (split ' '),"\n"; } close BW; close OUT; } sub rotate_minus90 { my ( $infile, $outfile, $tmp ) = @_; $tmp ||= 'c:/tmp/temp'; open IN, $infile or die "Can't read $infile $!\n"; # find number of columns and open a temp file for each local $_ = ; chomp; my @data = split ' '; my $num_cols = $#data; my @fhs; for( 0..$num_cols ) { open $fhs[$_], ">$tmp$_.txt" or die "Can't create temp file $tmp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( ) { chomp; @data = split ' '; print {$fhs[$_]} $data[$_], "\t" for 0..$num_cols; } close IN; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; for ( reverse 0.. $num_cols ) { close $fhs[$_]; # close the temp file open IN, "$tmp$_.txt" or die "Can't read temp file $tmp$_ $!\n"; print OUT scalar(), "\n"; close IN; unlink "$tmp$_.txt" } close OUT; } sub rotate_plus90 { my ( $infile, $outfile, $tmp ) = @_; $tmp ||= 'c:/tmp/temp'; open IN, $infile or die "Can't read $infile $!\n"; # find number of columns and open a temp file for each local $_ = ; chomp; my @data = split ' '; my $num_cols = $#data; my @fhs; for( 0..$num_cols ) { open $fhs[$_], ">$tmp$_.txt" or die "Can't create temp file $tmp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( ) { chomp; @data = split ' '; print {$fhs[$_]} $data[$_], "\t" for 0..$num_cols; } close IN; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; for ( 0.. $num_cols ) { close $fhs[$_]; # close the temp file open IN, "$tmp$_.txt" or die "Can't read temp file $tmp$_ $!\n"; $_ = ; chomp; print OUT join "\t", reverse(split ' '),"\n"; close IN; unlink "$tmp$_.txt" } close OUT; } sub rotate180 { my ( $infile, $outfile ) = @_; require File::ReadBackwards; tie *BW, 'File::ReadBackwards', $infile or die "Can't read $infile $!\n"; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; while( ) { chomp; print OUT join "\t", reverse(split ' '),"\n"; } close BW; close OUT; } sub flip_diag_backslash { my ( $infile, $outfile, $tmp ) = @_; $tmp ||= 'c:/tmp/temp'; open IN, $infile or die "Can't read $infile $!\n"; # find number of columns and open a temp file for each local $_ = ; chomp; my @data = split ' '; my $num_cols = $#data; my @fhs; for( 0..$num_cols ) { open $fhs[$_], ">$tmp$_.txt" or die "Can't create temp file $tmp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( ) { chomp; @data = split ' '; print {$fhs[$_]} $data[$_], "\t" for 0..$num_cols; } close IN; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; for ( 0.. $num_cols ) { close $fhs[$_]; # close the temp file open IN, "$tmp$_.txt" or die "Can't read temp file $tmp$_ $!\n"; print OUT scalar(), "\n"; close IN; unlink "$tmp$_.txt" } close OUT; } sub flip_diag_forwardslash { my ( $infile, $outfile, $tmp ) = @_; $tmp ||= 'c:/tmp/temp'; open IN, $infile or die "Can't read $infile $!\n"; # find number of columns and open a temp file for each local $_ = ; chomp; my @data = split ' '; my $num_cols = $#data; my @fhs; for( 0..$num_cols ) { open $fhs[$_], ">$tmp$_.txt" or die "Can't create temp file $tmp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( ) { chomp; @data = split ' '; print {$fhs[$_]} $data[$_], "\t" for 0..$num_cols; } close IN; open OUT, ">$outfile" or die "Can't write $outfile $!\n"; for ( reverse 0.. $num_cols ) { close $fhs[$_]; # close the temp file open IN, "$tmp$_.txt" or die "Can't read temp file $tmp$_ $!\n"; $_ = ; chomp; print OUT join "\t", reverse(split ' '),"\n"; close IN; unlink "$tmp$_.txt" } close OUT; }