Solved

Counting rows in a list 7

Posted on 2013-11-27
5
387 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:MichaelGlancy
  • 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:MichaelGlancy
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:MichaelGlancy
ID: 39681095
thankyou very much works great :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now