thorvid_461 has asked for the wisdom of the Perl Monks concerning the following question:

How in perl do you advance a letters using a for statement?
For example, with numbers i would use the following:

my $start_number = 1;
my $end_number = 10;
for (my $i=$start_number; $i<=$end_number; $i++)
{print "\$i = $i\n";}

I would like to do this same thing only with letters.

my $start_letter = "A";
my $end_letter = "D";
for (my $i=$start_letter; $i<=$end_letter; $i++)
{print "\$i = $i\n";}

I'm just not sure how it would be done.

Replies are listed 'Best First'.
Re: For Statement & Letters
by toolic (Bishop) on Jan 27, 2010 at 20:42 UTC
Re: For Statement & Letters
by keszler (Priest) on Jan 27, 2010 at 20:58 UTC
    Perl has a 'magic' incrementor. The code you posted for letters will work if you change the comparison operator <= to its string equivalent le:
    use strict; my $start_letter = 'A'; my $end_letter = 'D'; for (my $i=$start_letter; $i le $end_letter; $i++) { print "\$i = $i\n"; }
    As others have stated there are better ways to do this in Perl...
      This is what I was looking for and works perfectly.
      Thanks for the response, I really appreciate it.
Re: For Statement & Letters
by kennethk (Abbot) on Jan 27, 2010 at 20:51 UTC
    Rather than using using C-Style for loops, you should probably use Foreach Loops both described in perlsyn. For the numbers case you have given and using Range Operators as described in perlop to generate the list, you could replace:

    my $start_number = 1; my $end_number = 10; for (my $i=$start_number; $i<=$end_number; $i++) { print "\$i = $i\n"; }

    with the much briefer, clearer (IMHO) and more robust (less prone to bugs):

    foreach my $i (1 .. 10) { print "\$i = $i\n"; }

    One of the nice features of range operator is that it operates on letters as well as numbers:

    foreach my $i ('A' .. 'D') { print "\$i = $i\n"; }

    Perl documentation seems to be down right now, so hopefully the above links function properly, but if not, you should be able to use the above information to navigate your local copies of the documentation. As a side note, see Writeup Formatting Tips, in particular please wrap posted code in <code> tags to aid in readability.

      Just one minor note to add: In Perl, foreach is just an alternate spelling of for. This can also be written as (and I would write it as) for my $i ('A' .. 'D')
Re: For Statement & Letters
by ikegami (Patriarch) on Jan 27, 2010 at 20:55 UTC
    Change <= to lt and your code will work.
Re: For Statement & Letters
by ambrus (Abbot) on Jan 27, 2010 at 20:58 UTC

    Change < to le and it magically works:

    my $start_letter = "A"; my $end_letter = "D"; for (my $i = $start_letter; $i le $end_letter; $i++) { print "\$i = $i\n"; }
Re: For Statement & Letters
by umasuresh (Hermit) on Jan 27, 2010 at 20:51 UTC
    Are you trying to do something like this?
    use strict; use warnings; my @letters = ("A".."D"); for (my $i=0; $i<@letters; $i++) { print "$i = $letters[$i]\n"; }
    prints:
    0 = A 1 = B 2 = C 3 = D
Re: For Statement & Letters
by hominid (Priest) on Jan 27, 2010 at 20:45 UTC
    my $start_letter = ord('A'); my $end_letter = ord('D'); for (my $i=$start_letter; $i<=$end_letter; $i++) { print chr($i), "\n"; }