Solved

Counting multiple conditions

Posted on 2013-11-20
21
236 Views
Last Modified: 2013-11-20
The code below counts the number of rows, the value of the third column (in my data set), and will list the value of the third column along with the number of rows that it appears in.

so for this sample data set:
101,102,143,145,146,149
101,102,143,145,147,148
101,102,143,145,247,149
102,120,143,147,248,149
102,134,144,245,346,447
102,125,144,145,446,548
102,125,144,145,446,549

when 101 is the first value it will list data for the third column as:

143 4
144 3

Could the code be adjusted to count all incidences for 101 AND 102 ?
0
Comment
Question by:MichaelGlancy
  • 8
  • 7
  • 6
21 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 39663163
Which code?
0
 

Author Comment

by:MichaelGlancy
ID: 39663166
apologies .... :)


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

 /^101,[^,]*,(\d+)/ &&  $c{$1}++ while <M>;

close M;

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

Expert Comment

by:ozo
ID: 39663185
/^10[12],[^,]*,(\d+)/ &&  $c{$1}++ while <M>;
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39663187
You mean

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

 /^10[12],[^,]*,(\d+)/ &&  $c{$1}++ while <>;

close M;

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

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 39663262
that alteration just hangs for a long time :(
0
 
LVL 84

Expert Comment

by:ozo
ID: 39663271
If you altered <M> to <> it will be waiting for you to enter the data in STDIN
0
 

Author Comment

by:MichaelGlancy
ID: 39663317
I corrected <M> but it still hangs on my main data and doesnt show any return on count.txt in my sample data short list
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39663406
Sorry, I had made changes to see what you were getting.

This should run for you.


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

 /^10[12],[^,]*,(\d+)/ &&  $c{$1}++ while <M>;

close M;

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

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 39663481
its not working for me at all.

this is the sample data

101,102,103,104,105,106
101,102,103,104,105,106
101,102,103,104,105,106
101,103,104,105,106,107
101,103,104,105,106,107
101,103,104,105,106,107
102,103,106,107,108,109
102,103,106,107,108,109

the program should show

101 103 3
101 104 3
102 106 2

Is this possible ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39663584
changing
/^10[12],[^,]*,(\d+)/ &&  $c{$1}++ while <M>;
to
/^(10[12]),[^,]*,(\d+)/ &&  $c{"$1 $2"}++ while <M>;
should change the output from
103 3
104 3
106 2
to
101 103 3
101 104 3
102 106 2

But then you may also want to change
sort{$a<=>$b}keys %c
to
sort keys %c
to get rid of the Argument  isn't numeric warning message
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 31

Expert Comment

by:farzanj
ID: 39663625
Or you can try this:

#!/usr/bin/perl
#
my %count;

while(<>)
{
        my ($n1, $n2) = /^(\d+),[^,]+,(\d+)/;
        $count{$n1}{$n2}++;
}

foreach my $v1 (sort keys %count)
{
        foreach my $v2 (sort keys %{$count{$v1}})
        {
                print $v1, " ", $v2, " ", $count{$v1}{$v2}, "\n";
        }
}

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 39663680
using my sample data which I just ran from 1-60, count.txt contains

12 43 600
12 29 18240
12 42 1015
12 44 310
12 17 19840
12 26 23023
12 35 8008
12 40 2268
12 33 11200
12 46 33
12 39 3120
12 20 25578
12 18 22475
12 25 24288
12 15 11968
12 24 25300
12 30 16473
12 31 14688
12 28 19950
12 38 4125
12 19 24360
12 23 26000
12 32 12920
12 27 21560
12 37 5280
12 36 6578
12 34 9555
12 22 26325
12 14 6545
12 21 26208
12 45 128
12 41 1568
12 16 16368

when I use

/^(1[2]),[^,]*,(\d+)/ &&  $c{"$1 $2"}++ while <M>;

Maybe I have asked the question wrong ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39663698
I thought you wanted
/^(10[12]),[^,]*,(\d+)/
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39663706
Try my solution above by running like

./scriptname count.txt
0
 

Author Comment

by:MichaelGlancy
ID: 39663735
farzanj, thanks but Im a non-programmer working on a stats project. Dont know how to do that yet.

ozo, what is /^(10[12]),[^,]*,(\d+)/ ?

my data starts 101 mostly, then 102 etc.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39663755
What I mean was that you need to put that in a file.  You can name it anything but I named it

scriptname

And the file you want it to read it count.txt, right?
And you need to make it executable

chmod +x scriptname

Then on command line you need to say
./scriptname count.txt
0
 
LVL 84

Expert Comment

by:ozo
ID: 39663836
/(10[12])/ is equivalent to /(101|102)/
0
 

Author Comment

by:MichaelGlancy
ID: 39663883
sorry, farzanj, youve lost me, Im just hoping for a simple solution :)


thanks ozo

is it possible to change the script to analysis two sets at the same time ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39663942
The script should already analyse two sets at the same time
unless you mean something else by at the same time than I am understanding.
0
 

Author Comment

by:MichaelGlancy
ID: 39664323
I havent explained myself properly

just now it analyses the first column (for101) and the third column and it counts

Could it analyse the first column for 101, 102, 103 etc and still count the rows and values of third column ?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39664339
/^(10[123]),[^,]*,(\d+)/ &&  $c{"$1 $2"}++ while <M>;  # analyze the first column for 101, 102, 103

/^([^,*]),[^,]*,(\d+)/ &&  $c{"$1 $2"}++ while <M>;  # analyze for anything in the first column
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 52
topping1 challenge 7 53
Path of Workbook 3 45
Help to convert powershell script into a gui 9 77
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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

11 Experts available now in Live!

Get 1:1 Help Now