Creating letter combination generator

Creating letter combination generator

am 09.07.2011 03:26:41 von Robert

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
#


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

Re: Creating letter combination generator

am 09.07.2011 08:27:03 von Jim Gibson

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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Creating letter combination generator

am 09.07.2011 09:27:29 von David Christensen

On 07/08/2011 06:26 PM, 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 . . .

For climbing the Perl learning curve, I recommend the following three
books (in order):

1. "Learning Perl" is the place to start:

http://oreilly.com/catalog/0636920018452/

2. "Perl Cookbook" has useful, idiomatic code snippets with great
explanations. It gives you the building blocks and confidence you need
to start writing useful scripts:

http://oreilly.com/catalog/9780596003135/

3. "Programming Perl" is the language reference manual. It's for when
you need hard core information. I'd recommend waiting for the new
edition, due out in October:

http://oreilly.com/catalog/9780596004927/


Perl works the best and is the easiest to use on Unix or Unix-like
machines. If you're on Windows, get virtual machine software and pick a
Linux distribution (I use Debian). "Learning the Unix Operating System"
is the place to start if you don't already know Unix:

http://oreilly.com/catalog/9780596002619/


HTH,

David

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

Re: Creating letter combination generator

am 09.07.2011 11:10:10 von jwkrahn

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

perl -le'print for "A" .. "ZZ"'


> 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 warnings;

> 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', );

That can be written more simply as:

my @array = 'A' .. 'Z';

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

You are missing a newline after the last @array element:

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

Or:

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

Or:

print map "$_\n", @array;


> #
> #
> #So far so good I get the desired output for the first letter collum
> #
> #
>
> !/usr/bin/perl

use warnings;

> 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 );

The first argument of join() is evaluated in scalar context:

$ perl -le'print prototype "CORE::join"'
$@

And an array in scalar context returns the number of elements in the
array. So your example

print join( @first, @array );

Is the same as:

print join( "1", @array );

Because @first has only one element.

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



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

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

Re: Creating letter combination generator

am 11.07.2011 00:19:29 von Chris Nehren

On Sat, Jul 09, 2011 at 00:27:29 -0700 , David Christensen wrote:
> On 07/08/2011 06:26 PM, 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 . . .
>
> For climbing the Perl learning curve, I recommend the following three
> books (in order):
>
> 1. "Learning Perl" is the place to start:
>
> http://oreilly.com/catalog/0636920018452/

Does the sixth edition still encourage bad practices like calling subs
with &, not using three-arg open with lexical filehandles, and the
like? Best to avoid it for the bad practices newbies will have to
unlearn. Maybe start with Modern Perl if you're okay with the basics.

> 2. "Perl Cookbook" has useful, idiomatic code snippets with great
> explanations. It gives you the building blocks and confidence you
> need to start writing useful scripts:
>
> http://oreilly.com/catalog/9780596003135/

A lot of the cookbook has been outdated by CPAN. I've been programming
Perl professionally for about 5-6 years and have never looked at the
cookbook: CPAN has always* had what I need. As for idiomatic, maybe that
was true at the time the book was published, but things have changed a
lot since then.

> 3. "Programming Perl" is the language reference manual. It's for
> when you need hard core information. I'd recommend waiting for the
> new edition, due out in October:
>
> http://oreilly.com/catalog/9780596004927/
>

Programming Perl is for... what, 5.8? We're on 5.14 now, with both 5.8
and 5.10 EOLd
(http://perldoc.perl.org/perlpolicy.html#BACKWARD-COMPATIBIL ITY-AND-DEPRECATION)
Even when the new version comes out, it'll soon be outdated as well.
Printed references for software are always subpar due to how fast-moving
any non-moribund software project is.

> Perl works the best and is the easiest to use on Unix or Unix-like
> machines. If you're on Windows, get virtual machine software and
> pick a Linux distribution (I use Debian).

This pretty unilaterally discards the efforts Adam Kennedy and others
have put into Strawberry Perl, and completely ignores business
requirements to run on Windows. While it may be nice to stubbornly say
"I refuse to work with Windows" (and I'm spoiled in that I have that
freedom as part of my employment), it's not always the case that others
have such freedom of choice. To be clear, if I had my way, there would
be no more Windows. But PHBs tend to want it for some reason I cannot
discern. So it's better to help those stuck with such decisions.

*: With one notable exception, though somehow I doubt the cookbook would
have examples for dealing with the ebxml standard. :)

--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/

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

Re: Creating letter combination generator

am 11.07.2011 07:55:19 von Octavian Rasnita

From: "Chris Nehren"
....
>> Perl works the best and is the easiest to use on Unix or Unix-like
>> machines. If you're on Windows, get virtual machine software and
>> pick a Linux distribution (I use Debian).
>
> This pretty unilaterally discards the efforts Adam Kennedy and others
> have put into Strawberry Perl, and completely ignores business
> requirements to run on Windows. While it may be nice to stubbornly say
> "I refuse to work with Windows" (and I'm spoiled in that I have that
> freedom as part of my employment), it's not always the case that others
> have such freedom of choice. To be clear, if I had my way, there would
> be no more Windows. But PHBs tend to want it for some reason I cannot
> discern. So it's better to help those stuck with such decisions.
>



:-)

Well, not only PHBs decide to use Windows, but the intelligent bosses also.

Most businesses are not software/hardware companies, but companies with
different fields of activity that have employees which know very little
about computers, employees that probably didn't read a single page of manual
regarding the computers, however they need to be able to use the computer at
office as they can do it at home.

And, there are some fields where Windows is the best and Linux or Mac are
far behind, so some categories of users are forced to use Windows for this
reason.

That's why the portable languages and the languages that can make programs
easy to deploy have such a big advantage.

Octavian


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

Re: Creating letter combination generator

am 11.07.2011 09:29:45 von David Christensen

On 07/10/2011 03:19 PM, Chris Nehren wrote:
> Does the sixth edition still encourage bad practices like calling subs with&, not using three-arg open with lexical filehandles, and the like?

I read Learning Perl 2 e. and it was worth every penny. So, I'm
offering a blind recommendation based on that positive impression from
years ago. If you think the current edition is messed up, please post
examples. provide links, and/or contact the authors.


>Maybe start with Modern Perl ...

Modern Perl struck me as a book of finer points by an expert for
experts. I wouldn't recommend it as a first text.


> Programming Perl ... Printed references for software are always subpar due to how fast-moving any non-moribund software project is.

Perhaps, but I still enjoy having a good printed language reference. I
look forward to buying Camel 4 e.. :-)


> This pretty unilaterally discards the efforts Adam Kennedy and others have put into Strawberry Perl, and completely ignores business requirements to run on Windows.

I'm not discounting Strawberry Perl, ActivePerl, Cygwin, DJGPP, or any
of the others, but Perl is complex and Perl on Microsoft is even more
complex. I think it's best to start with Perl on its native platform,
and deal with the Microsoft complexities later.


David

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