Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Script: find how many times the first number is paired with the second number
 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

 05-11-2013, 09:28 AM #1 slufoot80 Member   Registered: Nov 2011 Posts: 69 Rep: 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"; }```
 05-11-2013, 10:07 AM #2 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,550 Rep: 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.
 05-11-2013, 12:59 PM #3 Beryllos Member   Registered: Apr 2013 Location: Massachusetts Distribution: Debian Posts: 360 Rep: 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.
 05-11-2013, 01:27 PM #4 slufoot80 Member   Registered: Nov 2011 Posts: 69 Original Poster Rep: 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
 05-11-2013, 03:59 PM #5 Beryllos Member   Registered: Apr 2013 Location: Massachusetts Distribution: Debian Posts: 360 Rep: 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.
 05-11-2013, 05:31 PM #6 schneidz LQ Guru   Registered: May 2005 Location: boston, usa Distribution: fc-15/ fc-20-live-usb/ aix Posts: 5,149 Rep: please provide a before-and-after picture.

 Tags awk, perl, sed

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post vivignesh Linux - Newbie 1 08-07-2012 02:14 AM turbomen Linux - Newbie 1 11-16-2010 03:48 AM mayankmehta83 Linux - Newbie 3 12-07-2009 08:44 AM grob115 Programming 4 08-15-2009 10:52 PM kvmreddy Linux - Newbie 2 08-15-2009 09:05 AM

LinuxQuestions.org

All times are GMT -5. The time now is 10:22 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -