Solved

Counting values in a list 6

Posted on 2013-11-19
5
225 Views
Last Modified: 2013-11-19
The code below will count the number of rows, with x in the second column where 105 is the value in the first column,
and list the number of rows with each value for x in the second column.

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

 /^105,(\d+)/ &&  $c{$1}++ while <M>;

close M;
open C,">count.txt" or die "count.txt $!";
print C "$c{$_}\n" for sort{$c{$b}<=>$c{$a}}keys %c;
close C;
***********************

 /^101,(\d+)/ &&  $c{$1}++ while <M>;  is the main operating line

Could this be changed do to the same, but for x in the third column ?

thankyou ?
0
Comment
Question by:MichaelGlancy
[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 84

Expert Comment

by:ozo
ID: 39659743
change  /^105,(\d+)/ to /^105,[^,]*,(\d+)/
(assuming that the third column is an unsigned decimal integer)
0
 

Author Comment

by:MichaelGlancy
ID: 39659776
/^105,[^,]*,(\d+)/  


is giving a strange set of results but I have to go to a meeting now so can look closer at it later
0
 

Author Comment

by:MichaelGlancy
ID: 39661014
the following results were given from a test set

13 78540
14 78540
15 77792
12 77700
16 76384
11 75924
17 74400
10 73112
18 71920
9 69160
19 69020
20 65772
8 63960
21 62244
22 58500
7 57400
23 54600
24 50600
6 49364
25 46552
26 42504
5 39732
27 38500
28 34580
29 30780
4 28380
30 27132
31 23664
32 20400
33 17360
3 15180
34 14560
35 12012
36 9724
37 7700
38 5940
39 4440
40 3192
41 2184
42 1400
43 820
44 420
45 172
46 44


The results seem to be correct BUT they are written to file in a strange manner.

they should be written this way

3 15180
4 28380
5 39732
6 49364.... etc

with the first number being the value of the data in the third column, and the second larger number being the number of rows with that value as the third column.

Now the code works as required.
Should I accept this for this question, and we can sort the order out in another question ?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39661030
print C "$_ $c{$_}\n" for sort{$a<=>$b}keys %c;
0
 

Author Closing Comment

by:MichaelGlancy
ID: 39661078
works great thankyou very much

please watch for next question :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Zip Folders Using Chilkat Routines 1 74
Not seen Link button 5 72
Link failure 16 64
Create .bat File 16 72
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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