supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perl Monks
I have three arrays in different text files i.e. k1.txt, k2.txt and k3.txt. My interest is to get all possible combinations of the array elements. I have written the script t.pl but it is showing wrong results. I am at my wit's end to correct the mistake. I request perl monks to look into my script and provide necessary suggestions for getting correct result.
I have given the text files below:
Text file k1.txt:
A1T1 A2T3
Text file k2.txt:
C1G1 C1G2 C2G1
Text file k2.txt:
A1C1
Here goes the script t.pl:
#!/usr/bin/perl use warnings; use File::Glob(bsd_glob); do { @array=@array1; print"\n\n Press 1 to Enter New File or 2 to Combine: "; $entry=<STDIN>; chomp $entry; ############################ # Use of if Conditional: ############################ if ($entry==1) { print"\n\n Enter New File Name (.txt): "; $filename = <STDIN>; chomp $filename; ################################ # open the file, or exit: ### ################################ unless ( open(FILE, $filename) ) { print "Cannot open file \"$filename\"\n\n"; exit;} @DNA= <FILE>; close FILE; $DNA=join('',@DNA); push @array, $DNA; @array1=@array;} # Curly brace for entry1 ends: elsif ($entry==2) { @array1=@array; # Curly brace for entry2 starts $number=@array1; print"\n\n No. of Elements in Joined Array: $number\n"; print"\n Joined Array:\n"; print @array1; # Use of foreach LOOP to view each element of joined array: $num=0; foreach my $element (@array1) { $num++; print"\n Array No.$num of the Joined Array:\n"; print $element; print"\n"; # Code to surround each element of joined array # followd by comma i.e. [ ], @element=split('',$element); $str1=sprintf '[%s],'x @element,@element; print"\n str1: $str1\n"; push @ARRAY1,$str1; } # Curly brace for foreach ends: print"\n ARRAY:\n"; print @ARRAY1; print"\n"; # To produce all possible combinations of different elements: $combi=join('',map {'{'.join (',',@$_).'}'} @ARRAY1); @list=bsd_glob($combi); print"\n Results:\n"; print"\n @list\n"; } # Curly brace for Entry 2 ends: } until ($entry==2); # Square bracket for do-until: exit;
I have got wrong results in cmd as follows:
Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\x>cd desktop C:\Users\x\Desktop>t.pl Press 1 to Enter New File or 2 to Combine: 1 Enter New File Name (.txt): k1.txt Press 1 to Enter New File or 2 to Combine: 1 Enter New File Name (.txt): k2.txt Press 1 to Enter New File or 2 to Combine: 1 Enter New File Name (.txt): k3.txt Press 1 to Enter New File or 2 to Combine: 2 No. of Elements in Joined Array: 3 Joined Array:A1T1 A2T3 C1G1 C1G2 C2G1 A1C1 Array No.1 of the Joined Array: A1T1 A2T3 str1: [A],[1],[T],[1],[ ],[A],[2],[T],[3],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ], Array No.2 of the Joined Array: C1G1 C1G2 C2G1 str1: [C],[1],[G],[1],[ ],[C],[1],[G],[2],[ ],[C],[2],[G],[1],[ ],[ ],[ ],[ ],[ ],[ ],[ ], Array No.3 of the Joined Array: A1C1 str1: [A],[1],[C],[1],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ], ARRAY: [A],[1],[T],[1],[ ],[A],[2],[T],[3],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[C],[1],[G],[1],[ ],[C],[1],[G],[2],[ ],[C],[2],[G],[1],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[A],[1],[C],[1],[ ],[ ],[ ],[ ],[ ],[ ],[ ],[ ], Results: {} C:\Users\x\Desktop>
The correct results at the end should look like:
~A1T1C1G1A1C1~ ~A1T1G1G2A1C1~ ~A1T1C2G1A1C1~ ~A2T3C1G1A1C1~ ~A2T3CAG2A1C1~ ~A2T3C2G1A1C1~
|
|---|