Hi Monks

I have read how to sort columns on this node:

http://www.perlmonks.org/?node_id=674374

I am trying to sort my columns in a similar manner. I have tried implementing the subroutine but it doesn't work. My idea was to first sort the columns and then to shift and pop each sorted column to get the largest and smallest value in each column. For each column I want to print the largest and smallest value, just as I have printed the sum of the entries in each column.

#!/usr/bin/perl #use strict; #use warnings; use integer; use Math::Matrix; ##you will print results but first remove any previous files my $fcgs = "final_cg_size.txt"; if (unlink($fcgs) == 1) { print "Existing \"final_cg_size.txt\" file was removed\n"; } #now make a file for the final core genome sizes output my $output_fcgs = "final_cg_size.txt"; if (! open(FCGS, ">>$output_fcgs") ) { print "Cannot open file \"$output_fcgs\" to writ +e to!!\n\n"; exit; } open IN, 'cg_size.txt' or die$!; my @colSum; while(<IN>){ chomp($_); our @array =(); our @colArray = split(/\s/,$_); for(my $i = 0; $i <= $#colArray; $i++){ $colSum[$i] += int($colArray[$i]); } for(our $i = 0; $i <= $#colArray; $i++){ $error_bars[$i] = $colArray[$i]; } my $sorted = sortSub(@colArray, 0, 1, 3); # print "$error_bars[16] \n"; # prints elements of col +umn 16 # print "@error_bars \n"; # not what I want print "***************************\n"; } print "\n"; foreach my $ j(0 .. $#colSum){ print "The sum of column",$j+1," is: ",$colSum[$j],"\n"; } sub sortSub { my @array = @{ +shift }; my @sorted = sort { for my $ix ( @_ ) { my $cmp = $a->[$ix] <=> $b->[$ix]; return $cmp if $cmp; } return 0; } @array; return \@sorted; } _FILE_ 2124 1827 1702 1710 1658 1637 1676 1630 1602 1607 1579 1558 1575 1550 +1548 1540 1537 2058 1771 1776 1701 1645 1672 1615 1599 1609 1583 1570 1587 1578 1563 +1555 1541 1537 2237 1864 1684 1673 1633 1667 1610 1624 1591 1589 1593 1576 1565 1570 +1559 1539 1537 2056 1845 1748 1693 1679 1670 1663 1638 1590 1564 1564 1573 1571 1545 +1542 1543 1537 2056 1808 1755 1689 1684 1663 1627 1581 1594 1580 1580 1616 1559 1568 +1541 1539 1537 1957 1889 1769 1667 1697 1669 1637 1631 1584 1621 1576 1570 1569 1552 +1547 1542 1537 2181 1991 1706 1715 1654 1686 1605 1622 1594 1597 1576 1564 1567 1560 +1567 1540 1537 2056 1873 1803 1711 1715 1669 1630 1619 1578 1604 1584 1568 1557 1556 +1544 1549 1537 1996 1915 1746 1690 1685 1630 1625 1589 1609 1593 1569 1562 1574 1555 +1548 1541 1537 2181 1875 1810 1728 1638 1625 1628 1621 1614 1587 1576 1558 1552 1557 +1543 1541 1537

In reply to sorting columns and getting the largest ansd smallest values in each column by $new_guy

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.