Dear Monks, I am currently trying to transpose a file. The input file I have is input.txt . And I want to create a transposed file and name that input2.txt My input file looks like this
923.066 923.813 432.895 433.381 M1 M2
I want my input2.txt file to look like
923.066 432.895 M1 923.813 433.381 M2
This is the code I have written.
#!/usr/bin/perl use strict; use File::Path qw(rmtree); my @rows = (); my @transposed = (); open F1,"input.txt"; while(<F1>) { chomp; push @rows, [ split / / ]; } #print @rows; my $filein2 = "input2.txt"; unlink $filein2; open my $fileHandlein2, ">>", "input2.txt" or die "Can't open 'input2. +txt'\n"; for my $row (@rows) { for my $column (0 .. $#{$row}) { push(@{$transposed[$column]}, $row->[$column]); } } for my $new_row (@transposed) { for my $new_col (@{$new_row}) { print $fileHandlein2 $new_col, " "; } print "\n"; }

The output file generated looks like

500.000 1333.000 M1 600.000 1130.000 M3

Could you please let me know what mistake I am doing and how to modify my code so that I can get the transposed file?

thanks,

In reply to Transpose a file by Ganesh Bharadwaj1

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.