Avatar of Europa MacDonald
Europa MacDonald
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Counting rows in a list 7

I have a list, with data on each row separated by a comma. I have several columns of data.

1,2,3,4,5,6,avg1,3.50,60000,1,1,1,1,1,avg2,1.0

x,x,x,x,x,x,text,1.1,60000,x,x,x,x,x,text,x
x,x,x,x,x,x,text,1.1,60000,x,x,x,x,x,text,x
x,x,x,x,x,x,text,1.1,60000,x,x,x,x,x,text,x
x,x,x,x,x,x,text,1.1,03030,x,x,x,x,x,text,x
x,x,x,x,x,x,text,1.1,03030,x,x,x,x,x,text,x
x,x,x,x,x,x,text,1.1,02220,x,x,x,x,x,text,x

It is the data in the 10th column that I have to count. With the result for the above sample list being

60000 3
03030 2
02220 1

The thing is, it is not a number, I have to look at it as text, and count the variations as I find them.

I tried using this code

#!/usr/bin/perl
use strict;
use warnings;
open M,"<master_sorted_002_reduced.vim" or die "master_sorted_002_reduced.vim $!";
my $c=0;


$c+= /[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(\d+)/ while <M>;


close M;
open C,">count4.txt" or die "count4.txt $!";
print C "$_ $c\n";
close C;

But it only seems to count the number of lines ?

Could you please help me with code which will count the rows, and report the variable and the number of each variable that it finds ? without treating it like a number ?
PerlProgrammingProgramming Languages-Other

Avatar of undefined
Last Comment
Europa MacDonald

8/22/2022 - Mon
FishMonger

Using a regex to parse out a csv file works, but is the wrong approach.  You should use the split function and store the results in a hash.  You should also use a lexical var for the filehandle and the 3 arg form of open.  I normally also recommend using Text::CSV when parsing csv file, but left it out this time.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $file = "master_sorted_002_reduced.vim";
open my $fh, '<', $file or die "Failed to open '$file' <$!>";

my %count;
while (my $line = <$fh>) {
    my $field = (split /,/, $line)[9];
    $count{"$field"}++;
}
close $fh;

print Dumper \%count;

Open in new window

Europa MacDonald

ASKER
thanks, Ive tried it, and I get a temporary window open and close.
I dont know a lot about PERL or programming. How do I get the results to print to a text file ?
FishMonger

Run the script from the command line.

Given your  sample data, the column you say you need is the 9th, not the 10th.
Here's the results I get after adjusting the column index on the array slice.
c:\test>Michael.pl
$VAR1 = {
          '60000' => 3,
          '03030' => 2,
          '02220' => 1
        };

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
FishMonger

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Europa MacDonald

ASKER
thankyou very much works great :)