I have been using some of this syntax. I was trying to also output a unique count of the numbers in the 3rd & 4th columns. I was trying to push the value's in the column onto an array, if it already is not already in the array. I think this is currently a scalar value but i would need an array/list. This is the error that I receive: Not an ARRAY reference at ./test.pl line 21, <DATA> line 1. I am thinking once I have the values in an array or list I can just print the list in scalar context to get the unique count? I don't know if there is a simpler way to achieve this.
#!/usr/bin/perl use strict; use warnings; my $basecol=shift @ARGV or die "What base col?"; my %h; $basecol--; # zero based inside program. while (<DATA>){ chomp; my @f= split ; next unless @f > 1; splice @f,0,0,$f[0] . "_". $f[1]; my $k = $f[$basecol]; $h{$k}{COUNT}++; for my $col(0..$#f){ next if $col==$basecol; $f[$col]=0 unless $f[$col]=~/^\d+$/; $h{$k}{FIELD_S}[$col]+= $f[$col]; push($h{$k}{FIELD_C}[$col],$f[$col]) if !($f[$col] ~~ $h{$k}{FIELD +_C}[$col]); } } for (sort keys %h){ print "$_\t $h{$_}{COUNT}\t"; for my $f(@{ $h{$_}{FIELD_S} }){ defined $f or $f=''; print "$f\t"; } print "\n"; } __DATA__ U1 ID1 100 280 U1 ID1 137 250 U2 ID2 150 375 U1 ID2 100 100 U3 ID1 100 600 U9 ID3 137 200

In reply to Re^2: Perl Sum & Count column based data by Anonymous Monk
in thread Perl Sum & Count column based data by ler224

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.