Solved

calculating number combinations in perl

Posted on 2009-07-07
5
198 Views
Last Modified: 2012-05-07
I have a small problem. I am working on a probability solution.

I have quasi-randomly generated data sets, from the value of 1 to 999. These values are the end result of another mathematical process.

The data comes out in groups of seven, eight and ten.

We have a mid range of 501-550.

If I could get help on the seven group problem, Im sure I could find my way with the other two groups when I can see the code structure.

I need to build a database (which could be a text file if it is big enough) of all possible value combinations, so out of the 50 numbers, from 501 to 550.

So the first possible combination would be
501,502,503,504,505,506,507

Then the next possible combination would be
501,502,503,504,505,506,508

And the combinations would tick up one at a time until
544,545,546,547,548,549,550, which would be the final one.

I expect about 10 million results.

I need each individual result output to a text file / word pad one line at a time, delineated by a comma.

Could you tell me perl code which would do this ?

Thankyou very much
0
Comment
Question by:MichaelGlancy
  • 3
5 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24799045

use Math::Combinatorics;
 
 

my $combin = Math::Combinatorics->new(count => 7, data => [501..550]);
 

while(my @combo = $combin->next_combination){

	print join(',', @combo)."\n";

}

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24799053
thankyou Adam

would this print out to a text file ?
0
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 500 total points
ID: 24799133
If you don't have Math::Combinatorics installed and can't install it for some reason, here's a code version.

For either my solution or Adam's, just redirect the output to a text file using the greater than symbol:
scriptname.pl > filename.txt
my $count = 3;

my $low = 501;

my $high = 550;
 

sub cmb

{

  my ($rem, $used, $text, $min) = @_;

  $rem--;
 

  for my $n ($min..($high - $rem))

  {

    next if $used->{$n};

    if ($rem)

    {

      $used->{$n} = 1;

      cmb($rem, $used, "$text$n,", $n + 1);

      $used->{$n} = 0;

    } else

    {

      print "$text$n\n"

    };

  }

}
 

my %h;

cmb($count, \%h, "", $low);

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24799168
thankyou

where would I put

scriptname.pl > filename.txt

?

0
 

Author Closing Comment

by:MichaelGlancy
ID: 31600854
worked out the file thing myself
thankyou very much
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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

20 Experts available now in Live!

Get 1:1 Help Now