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~

In reply to How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.