Solved

Counting rows in a list 7

Posted on 2013-11-27
5
395 Views
Last Modified: 2013-11-27
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 ?
0
Comment
Question by:Europa MacDonald
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 39680928
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

0
 

Author Comment

by:Europa MacDonald
ID: 39680951
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 ?
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 39680989
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

0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 39681007
To print to a text file you could either redirect the output from the command line, or you can open another filehandle in the script and print to that filehandle.
#!/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)[8];
    $count{"$field"}++;
}
close $fh;

open my $out_fh, '>', 'output.txt' or die "failed to open output.txt";
while (my ($key, $value) = each %count) {
    print $out_fh "$key $value\n";
}
close $out_fh;

Open in new window

0
 

Author Closing Comment

by:Europa MacDonald
ID: 39681095
thankyou very much works great :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Part One of the two-part Q&A series with MalwareTech.
Introduction to Processes
Starting up a Project

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question