Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?

by supriyoch_2008 (Scribe)
on Apr 24, 2013 at 04:28 UTC ( #1030274=perlquestion: print w/ replies, xml ) Need Help??
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~

Comment on How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
Select or Download Code
Re: How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
by hdb (Prior) on Apr 24, 2013 at 05:36 UTC

    Just a few hints as code snippets:

    # declare string for glob my $string = ""; # slurp in file into a string $/=''; $DNA = <FILE>; # remove \n at end chomp($DNA); # replace line endings with commata $DNA =~ s/\n/,/; # surround with braces $DNA = "{$DNA}"; # $DNA should now be good for glob $string .= $DNA; # repeat for each file, don't forget to open and close
      # slurp in file into a string $/=''; $DNA = <FILE>; # remove \n at end chomp($DNA);

      You have set $/ to paragraph mode so it will not "slurp in file", just the first paragraph of the file.    Also the value of $/ will effect what chomp removes (hint: paragraph mode removes more than just a single \n character).



      # replace line endings with commata $DNA =~ s/\n/,/;

      That replaces a single newline.    If you want to replace ALL newlines then:

      # replace newlines with commas $DNA =~ s/\n/,/g;

      Or perhaps:

      # replace newlines with commas $DNA =~ tr/\n/,/;

        All correct. I should have stated that I was only giving some hints rather than a functioning script.

        UPDATE: The code below works hopefully and has no side effects hopefully?

        use strict; use warnings; my $string = ""; my $DNA; { local $/=undef; $DNA = <DATA>; } chomp($DNA); $DNA =~ s/\n/,/g; $DNA = "{$DNA}"; $string .= $DNA; print $string; __DATA__ C1G1 C1G2 C2G1
Re: How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
by kcott (Abbot) on Apr 24, 2013 at 08:37 UTC

    G'day supriyoch_2008,

    The builtin glob function handles this fine.

    #!/usr/bin/env perl use strict; use warnings; use Inline::Files; my @file_data = map { [ do { local $/; <$_> } =~ /(\w+)/gm ] } *K1_TXT, *K2_TXT, + *K3_TXT; my $glob_string = join '' => map { '{' . join(',' => @$_) . '}' } @fil +e_data; print "$_\n" for glob $glob_string; __K1_TXT__ A1T1 A2T3 __K2_TXT__ C1G1 C1G2 C2G1 __K3_TXT__ A1C1

    Output:

    A1T1C1G1A1C1 A1T1C1G2A1C1 A1T1C2G1A1C1 A2T3C1G1A1C1 A2T3C1G2A1C1 A2T3C2G1A1C1

    However, if you want to use File::Glob, this code produces the same output.

    #!/usr/bin/env perl use strict; use warnings; use Inline::Files; use File::Glob qw{bsd_glob}; my @file_data = map { [ do { local $/; <$_> } =~ /(\w+)/gm ] } *K1_TXT, *K2_TXT, + *K3_TXT; my $glob_string = join '' => map { '{' . join(',' => @$_) . '}' } @fil +e_data; print "$_\n" for bsd_glob $glob_string; __K1_TXT__ A1T1 A2T3 __K2_TXT__ C1G1 C1G2 C2G1 __K3_TXT__ A1C1

    I would recommend you put 'use strict;' at the top of your code and fix all the problems it highlights; including bareword in 'use File::Glob(bsd_glob);' and a plethora of undeclared variables (@array, $combi, $number, and others). I also found your code hard to read; the main problem was indentation — take a look at perlstyle.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030274]
Approved by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-12-27 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls