Solved

Counting rows in a list 7

Posted on 2013-11-27
5
390 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adoquery sql  left join does not work 25 91
Problem to page 4 85
iSeries email authority 6 57
parse a file and get data out 11 46
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 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