Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Counting rows in a list 7

Posted on 2013-11-27
5
Medium Priority
?
398 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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