LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script: find how many times the first number is paired with the second number (https://www.linuxquestions.org/questions/linux-newbie-8/script-find-how-many-times-the-first-number-is-paired-with-the-second-number-4175461537/)

slufoot80 05-11-2013 08:28 AM

Script: find how many times the first number is paired with the second number
 
I am trying to find a script that will tell me how many times the first number is paired with the second number

I already have a script that will tell me the total number of times a number is found but now I want to know how many times the number 127 is paired with 128. this is to find the most popular pairs

code for total number of times

Code:

#!/usr/bin/perl -w
## count.pl
use strict;

my @names = qw(

);

  my %count;

  foreach (@names) {
        if (exists $count{$_}) {
        $count{$_}++;
  } else {
  $count{$_} = 1;
  }
  }
                                                                                                  foreach (keys %count) {
                                                                                                                                                                                                                      print "$_ \toccurs $count{$_} time(s)\n";
                                                                                                                                                                                                                                                          }


grail 05-11-2013 09:07 AM

Well I am not really seeing how the above does much of anything as you are looping through an empty array?

As for your query, where are you stuck and what have you done?
One would think if you are happy to store the data in arrays then you simply look at the first and second element and see if the second is greater than the first by 1.

Beryllos 05-11-2013 11:59 AM

If all you want to know is how many times the values 127 and 128 appear together in the same record, that is pretty easy. Here is one possible solution:
Code:

(pseudocode... sorry about that)
count=0 ; I noticed you set the default to 1... Why?
loop over records
    exists127=false
    exists128=false
    read the record
    if exist 127 then exists127=true
    if exist 128 then exists128=true
    if exists127==true and exists128==true then count++
end loop

If you want to find the frequency of pairings of numbers which may vary, we need to know some more details. To begin with:
Does every record have exactly two numbers?
Does it matter which number comes first?
What is the range of numbers?

One approach might be to:
Set up a 2D array count[range][range] initialized to zero.
Loop over records.
Read value1 and value2.
If the order doesn't matter, force value1<=value2 by swapping the values if value1>value2.
Do count[value1][value2]++;
After reading and processing all records, find the maximum array element (or several highest elements, depending on what you are trying to accomplish) and report the count together with its indices.

After you clearly specify your requirements, you could adapt this or choose a more efficient algorithm.

slufoot80 05-11-2013 12:27 PM

ok the range of numbers is not important lets say 1 to 100 what I want to do is


in one line you have 10 numbers 1..80 and whatever comes in between I want to know how many times
03 & 29 come out together I have the code to find how many times each number comes out.

i.e. or find which numbers are displayed together more often

07 13 18 19 21 23 29 32 37 38 41 45 48 52 56 59 61 72 75 77
03 08 09 13 27 29 30 32 37 42 43 48 57 58 66 70 73 75 78 80
02 06 08 10 13 14 18 19 22 31 35 38 48 56 59 61 67 75 78 80
01 02 03 07 14 19 20 34 39 48 51 52 54 59 65 68 69 71 75 76

Beryllos 05-11-2013 02:59 PM

Please state the problem clearly. The original post has values of 127 and 128. In your latest post, you say the range is "not important" (okay), but could be "1 to 100," or "1..80." You say each line has 10 numbers, but in your example, each line has 20 numbers. Okay, fine, we can leave some parameters open for now, but the larger problem is that I still don't know what the basic requirement is. Do you mean any pair which occurs more/most often? For example, 48 and 75 appear in all 4 lines; that looks like a winner. Or does it have to be 03 and 29 like you said?

If you want to count all pairwise occurrences, I suggest that you set up a list, or an array, to count all possible pairs: 1&2, 1&3, 1&4, ..., 2&3, 2&4, 2&5, ..., 79&80. For 1..N, that is N*(N-1)/2 possible pairs. I suggest that you count with 2 nested loops. Outer loop steps through each line, item by item, not including the last item. Inner loop steps through all items following the item of the outer loop.
Code:

outer loop: for(i=1;i<10;i++)
inner loop: for(j=i+1;j<=10;j++)

Finally, you mentioned that you have code to count the total number of times a number is found. That code will be of little or no use for finding and counting pairwise occurrences. To count pairs is a completely different problem. Frequency of single occurrence is not necessarily related to frequency of occurrence in pairs.

schneidz 05-11-2013 04:31 PM

please provide a before-and-after picture.


All times are GMT -5. The time now is 12:45 PM.