in reply to Re: Perl Sum & Count column based data
in thread Perl Sum & Count column based data

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