Here is a full suite of file based function. The redundant code could be cut away and it could be made into a module if there is general use for this sort of thing.

#!/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 = <F>; 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( <IN> ) { 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( <BW> ) { 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 $_ = <IN>; 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 $t +mp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( <IN> ) { 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(<IN>), "\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 $_ = <IN>; 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 $t +mp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( <IN> ) { 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 +"; $_ = <IN>; 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( <BW> ) { 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 $_ = <IN>; 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 $t +mp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( <IN> ) { 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(<IN>), "\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 $_ = <IN>; 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 $t +mp$_ $!\n"; print {$fhs[$_]} $data[$_], "\t"; } while( <IN> ) { 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 +"; $_ = <IN>; chomp; print OUT join "\t", reverse(split ' '),"\n"; close IN; unlink "$tmp$_.txt" } close OUT; }

cheers

tachyon


In reply to Re: Binary file handling by tachyon
in thread Binary file handling by Hena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.