Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by poj (Abbot)
on Apr 24, 2013 at 20:33 UTC ( [id://1030519]=note: print w/replies, xml ) Need Help??


in reply to Re^4: How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
in thread How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?

Here are 11 changes/simplifications to make your script work
#!/usr/bin/perl #1 add use strict use strict; use warnings; #2 add '' use File::Glob('bsd_glob'); #3 declare variables my (@array,@DNA,@list); my ($filename,$combi,$entry,$number,$DNA); do { #4 not needed @array=@array1; print"\n\n Press 1 to Enter New File or 2 to Combine: "; $entry=<STDIN>; chomp $entry; if ($entry==1) { print"\n\n Enter New File Name (.txt): "; $filename = <STDIN>; chomp $filename; unless ( open(FILE, $filename) ) { print "Cannot open file \"$filename\"\n\n"; exit; } @DNA= <FILE>; close FILE; #5 add chomp chomp(@DNA); #6 add join by comma $DNA = join(',',@DNA); push @array, $DNA; #7 not needed @array1=@array; } elsif ($entry==2) { #8 @array1 not needed use @array #@array1=@array; # Curly brace for entry2 starts $number=@array; print"\n\n No. of Elements in Joined Array: $number\n"; print"\n Joined Array:\n"; print "@array\n"; #9 none of this code required because comma added a #6 # 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: $s\n"; #push @ARRAY1,$element; #} # Curly brace for foreach ends: #print"\n ARRAY:\n"; #print @ARRAY1; #print"\n"; # To produce all possible combinations of different elements: #10 use simple $_ on @array #$combi = join('',map {'{'.join (',',@$_).'}'} @ARRAY1); $combi = join('',map {'{'.$_.'}'} @array); print "combi = $combi\n"; @list = bsd_glob($combi); print"\n Results:\n"; #11 print each element on new line print join "\n",@list; } # Curly brace for Entry 2 ends: } until ($entry==2); # Square bracket for do-until: exit;
poj
  • Comment on Re^5: How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
  • Download Code

Replies are listed 'Best First'.
Re^6: How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
by supriyoch_2008 (Monk) on Apr 25, 2013 at 03:03 UTC

    Hi poj,

    Thanks for your suggestions and for showing 11 changes in my script. The script now works but in the result part, many unwanted combinations are found along with the desired 6-letter combinations. The 6-letter combinations are correct and this is only what I need in the result. How can I get only the desired 6-letter combinations in the result?

    Here goes the script again:

    #!/usr/bin/perl #1 add use strict use strict; use warnings; #2 add '' use File::Glob('bsd_glob'); ############################################ ## Output to a TEXT File using file handle: ############################################ my $output="Joint.txt"; open (my $fh,">",$output) or die"Cannot open file '$output'.\n"; #3 declare variables my (@array,@DNA,@list); my ($filename,$combi,$entry,$number,$DNA); do { print"\n\n Press 1 to Enter New File or 2 to Combine: "; $entry=<STDIN>; chomp $entry; if ($entry==1) { print"\n\n Enter New File Name (.txt): "; $filename = <STDIN>; chomp $filename; unless ( open(FILE, $filename) ) { print "Cannot open file \"$filename\"\n\n"; exit; } @DNA= <FILE>; close FILE; #5 add chomp chomp(@DNA); #6 add join by comma $DNA = join(',',@DNA); push @array, $DNA; #7 not needed array1=array; } elsif ($entry==2) { # Curly brace for entry2 starts $number=@array; print"\n\n No. of Elements in Joined Array: $number\n"; print $fh "\n\n No. of Elements in Joined Array: $number\n"; + # To produce all possible combinations of different elements: #10 use simple $_ on @array $combi = join('',map {'{'.$_.'}'} @array); @list = bsd_glob($combi); print"\n Results:\n"; print $fh "\n Results:\n"; #11 print each element on new line print join "\n",@list; print $fh join "\n",@list; } # Curly brace for Entry 2 ends: } until ($entry==2); # Square bracket for do-until: close $output; exit;

    Here goes the result of cmd and of output file Joint.txt:

    No. of Elements in Joined Array: 3 Results: A1T1C1G1A1C1 # This is correct A1T1C1G1 A1T1C1G1 A1T1C1G1 A1T1C1G1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1 C2G1 C2G1 C2G1 A1C1 A1C1 A1C1 A1C1 A1C1 A1C1 C1G1A1C1 C1G1 C1G1 C1G1 C1G1 C1G1 C1G1 C1G1 C1G2A1C1 C1G2 C1G2 C1G2 C1G2 C1G2 C1G2 C1G2 C2G1A1C1 C2G1 C2G1 C2G1 C2G1 C2G1 C2G1 C2G1 A1C1 A1C1 A1C1 A1C1 A1C1 A1C1
      That looks like you have some blank lines in the data files probably at the end. If you print $DNA you should see commas with no values between them. Either edit the files or add a filter like this ;
      $DNA = join(',',grep(/[ACGT]\d[ACGT]\d/,@DNA)); print "$DNA\n";
      poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1030519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found