At 6:26 PM -0700 7/8/11, Robert wrote:
I have currently wrote a simple script to attempt to create a list of
every letter combination this is a rough outline of what I want my
program to do . . .

A
B
C
...
AA
AB
AC

I used perl a bit for a high school project several years ago so I'm
not that good at it =(  Never the less I have extensively googled what
I am trying to accomplish and came across the join command.  I tried
to plug this into my code and get the desired output.

Here is what I have:


!/usr/bin/perl
use strict;

my @array = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', );

print join("\n", @array , );

#
#
#So far so good I get the desired output for the first letter collum
#
#

!/usr/bin/perl
use strict;

my @array = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', );

my @first = join("\n", @array , );

print join(@first , @array );

#
#
#Here is where it gets messed up, the output that I get is as follows
# A1B1C1D1E1F1G1H1I1J1K1L1M1N1O1P1Q1R1S1T1U1V1W1X1Y1Z
#

join is not what you want. join takes the elements of an array and concatenates them into a scalar value and adds a delimiter between each pair.

You can generate pairs of letters this way:

my @letters = (A..Z);
for my $l1 ( @letters ) {
  for my $l2 ( @letters ) {
    print "$l1$l2\n";
  }
}

I have seen more clever ways, but the above will do the trick and is easy to understand.

If you want more than 2, you will want to investigate "combinations". For example the module Math::Combinatorics has functions for generating combinations and permutations. However, what you seem to want is "combinations with replacement", so you might have to search for that term.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to