Solved

Counting multiple conditions

Posted on 2013-11-20
21
238 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
combine multiple lines 2 44
Image decoding from Camera 3 72
Re-position the objects 7 98
Log File Creation with Header and Footer 17 63
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

867 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

18 Experts available now in Live!

Get 1:1 Help Now