LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2013, 08:28 AM   #1
slufoot80
Member
 
Registered: Nov 2011
Posts: 69

Rep: Reputation: Disabled
Smile 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";
                                                                                                                                                                                                                                                           }
 
Old 05-11-2013, 09:07 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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.
 
1 members found this post helpful.
Old 05-11-2013, 11:59 AM   #3
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.
 
Old 05-11-2013, 12:27 PM   #4
slufoot80
Member
 
Registered: Nov 2011
Posts: 69

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-11-2013, 02:59 PM   #5
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
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.
 
Old 05-11-2013, 04:31 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
please provide a before-and-after picture.
 
  


Reply

Tags
awk, perl, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to search a pattern in a diretory and output number of find counts vivignesh Linux - Newbie 1 08-07-2012 01:14 AM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
shell script :: how to print string that appeared maximum number of times in file ?? mayankmehta83 Linux - Newbie 3 12-07-2009 07:44 AM
Ping for a specific number of times grob115 Programming 4 08-15-2009 09:52 PM
how to find given number is integer or not in shell script kvmreddy Linux - Newbie 2 08-15-2009 08:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration