laziness, impatience, and hubris PerlMonks

### Hexamer combination from 3mers.

by oxydeepu (Novice)
 on Oct 10, 2012 at 15:38 UTC Need Help??
oxydeepu has asked for the wisdom of the Perl Monks concerning the following question:

Hi all monks,
I need a help with a script. I have a list of 3mers like

ABC
EFG
KLM
XYZ

So what I want is make all possible hexamers from the above list

e.g.,
ABCABC
ABCEFG
ABCKLM
ABCXYZ
EFGABC
EFGEFG
EFGKLM
EFGXYZ
......
......
XYZABC
XYZEFG
XYZKLM
XYZXYZ

I think I have explained my problem. So can anyone help with this.
Best,
Deepak

Replies are listed 'Best First'.
Re: Hexamer combination from 3mers.
by marto (Archbishop) on Oct 10, 2012 at 15:44 UTC

Which part are you having problems with? How would you solve the problem using a pen and paper? I suspect you've been given this as a learning exercise, asking others to solve the problem word defeat the purpose of such an exercise

Hi,
Actually this not an excercise. I just wanted to explain the problem as simple as possible thats why. The list Bigger than this. I just wanted the logic.

Best,
Deepak

Re: Hexamer combination from 3mers.
by daxim (Chaplain) on Oct 10, 2012 at 15:53 UTC
```use 5.010;
use Algorithm::Combinatorics qw(variations_with_repetition);
say join q(), @{ \$_ }
for variations_with_repetition [qw(ABC EFG KLM XYZ)], 2;
__END__
ABCABC
ABCEFG
ABCKLM
ABCXYZ
EFGABC
EFGEFG
EFGKLM
EFGXYZ
KLMABC
KLMEFG
KLMKLM
KLMXYZ
XYZABC
XYZEFG
XYZKLM
XYZXYZ
Re: Hexamer combination from 3mers.
by choroba (Bishop) on Oct 10, 2012 at 15:47 UTC
```#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';

my @trimers = <>;
chomp @trimers;
say for glob +('{' . join(',', @trimers) . '}' ) x 2;
Update: Using glob is safe only if the data do not contain any special characters like ? * [] {} , or whitespace.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Hi,
Thanks alot. That works.
Best,
Deepak

Re: Hexamer combination from 3mers.
by 2teez (Vicar) on Oct 10, 2012 at 16:34 UTC

Or you could simply use a foreach loop like so:

```use warnings;
use strict;

my @three_mers = qw( ABC EFG KLM XYZ);

foreach my \$mers_first (@three_mers) {
foreach my \$mers_sec (@three_mers) {
print \$mers_first, \$mers_sec, \$/;
}
}

OUTPUT
```ABCABC
ABCEFG
ABCKLM
ABCXYZ
EFGABC
EFGEFG
EFGKLM
EFGXYZ
KLMABC
KLMEFG
KLMKLM
KLMXYZ
XYZABC
XYZEFG
XYZKLM
XYZXYZ

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Create A New User
Node Status?
node history
Node Type: perlquestion [id://998240]
Approved by GrandFather
help
Chatterbox?
 [liz]: ^10 is just a short way of saying 0..^10, which is a short way of saying 0..9 [liz]: ^N is a very common idiom in Perl 6 to indicate "N times" [liz]: The ^ indicates "exclude endpoint from range"

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2018-07-16 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (344 votes). Check out past polls.

Notices?