The key is to extract the column number from the column data then use that to place it in the table. Extracting stuff is what regexes do. Formatting the table is nicely done with printf. Consider:

use warnings; use strict; use constant kMaxCol => 7; # print header printf "%5s ", $_ for 1 .. kMaxCol; print "\n"; # Process data while (<DATA>) { chomp; next unless length; my @cols = split (/\s+/, $_); my @sorted; @cols = map {[(m/(\d+)p/i ? $1 - 1 : kMaxCol), $_]} @cols; # Gener +ate indexes $sorted[$_->[0]] = $_->[1] for grep {$_->[0] >= 0 && $_->[0] < kM +axCol} @cols; $sorted[$_] ||= '' for 0 .. kMaxCol - 1; printf " %7s", $_ for @sorted; print "\n"; } __DATA__ c1p110 c2p452 9p235 8p226 5p100 6p218 c1p278 2p397 c6p280 7p273 3p409

Prints:

1 2 3 4 5 6 7 c1p110 c2p452 5p100 6p218 c1p278 2p397 3p409 c6p280 7p273

Perl reduces RSI - it saves typing

In reply to Re: rearange the columns by GrandFather
in thread rearange the columns by sanku

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.