Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by jwkrahn (Monsignor)
on Apr 24, 2013 at 06:45 UTC ( #1030289=note: print w/ replies, xml ) Need Help??


in reply to Re: 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)?

# 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/,/;


Comment on Re^2: How can one get all possible combinations of elements of different arrays using File::Glob(bsd_glob)?
Select or Download Code
Re^3: 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 08:15 UTC

    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

      Hi hdb,

      Thanks for the code snippets and update. Honestly speaking, I do not understand exactly where I have to make the changes in my script with the codes given by you in update for getting correct results from the script. I think I have to read more about the use of File::glob perl module and look for examples online to understand the stuff. Using the code given by you I have found that the cmd shows the result {C1G1,C1G2,C2G1}. I find it difficult how to use this in my script to solve the problem.

      With kind regards,

      Here goes the output of cmd:

      Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\x>cd desktop C:\Users\x\Desktop>t9.pl {C1G1,C1G2,C2G1} C:\Users\x\Desktop>t9.pl
        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

        The key is in the glob function. If you feed it a string like "{1,2}{a,b,c}" it creates all combinations of elements within the braces. Here is a complete solution for your problem. I hope you can adapt it to your needs. There is nothing really new in there.

        use strict; use warnings; use File::Glob qw{bsd_glob}; my $string; my $entry; 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): "; my $filename = <STDIN>; chomp $filename; open my $fh, "<", $filename or die "Cannot open $filename.\n"; my $DNA; { local $/=undef; $DNA = <$fh>; } close $fh; chomp $DNA; $DNA=~s/\n/,/g; $string .= "{$DNA}"; } # Curly brace for entry1 ends: } until ($entry==2); for (bsd_glob $string) { print "~$_~\n"; }

        So for every file you input, it creates this substring "{...,...,...}" with all elements from the file and adds it to the previous substrings of the same kind. When you are done, the overall string is fed into glob to create all combinations. I hope this makes it clear.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-12-26 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (171 votes), past polls