in reply to How can one get the correct results of all combinations with bigger input files?

davido's advice leads to the following exercise: write a script that takes a list of file names, counts the lines in each file and calculates the number of possible combinations (without creating the combinations, of course). (Nothing against pencil and slide rule, but this is a Perl community...) Once you have the result, we can continue the discussion.

  • Comment on Re: How can one get the correct results of all combinations with bigger input files?

Replies are listed 'Best First'.
Re^2: How can one get the correct results of all combinations with bigger input files?
by supriyoch_2008 (Monk) on Apr 29, 2013 at 05:22 UTC

    Hi hdb,

    As per davido's advice and your suggestions, I have written a script c.pl that takes a list of file names and calculates number of lines in each text file and finally the total number of combinations. I have observed that the number of combinations will be very large i.e. 644972544 with files s1.txt, s2.txt and s3.txt (each with 864 lines). But I have written a line to remove white space and empty lines,if any, at the end of each text file i.e $fh=~s/\s+$//g;. I am not sure whether it works or not.

    Here goes the c.pl;

    #!/usr/bin/perl use strict; use warnings; my $entry; my @a; do { print"\n Press 1 to enter a File or 2 to count Total Combinations: + "; $entry=<STDIN>; chomp ($entry); if ($entry==1) { print"\n\n Enter File Name to count number of LINES (.txt): "; + my $filename = <STDIN>; chomp $filename; open my $fh, "<", $filename or die "Cannot open $filename.\n"; $fh=~s/\s+$//g; # To remove white space & empty # lines after end of each text file my $count = 0; while ( <$fh> ) {$count++;} print"\n Lines in File $filename: $count\n\n"; push @a, $count; } } until ($entry==2); my $product=1; $product *= $_ foreach @a; print"\n\n Total Combinations: $product\n"; exit;

    I have got the following results:

    C:\Users\x\Desktop>c.pl Press 1 to enter a File or 2 to count Total Combinations: 1 Enter File Name to count number of LINES (.txt): s1.txt Lines in File s1.txt: 864 Press 1 to enter a File or 2 to count Total Combinations: 1 Enter File Name to count number of LINES (.txt): s2.txt Lines in File s2.txt: 864 Press 1 to enter a File or 2 to count Total Combinations: 1 Enter File Name to count number of LINES (.txt): s3.txt Lines in File s3.txt: 864 Press 1 to enter a File or 2 to count Total Combinations: 2 Total Combinations: 644972544 C:\Users\x\Desktop>