Alphabet

Austin Schutz tex at off.org
Sat Feb 9 02:20:32 CST 2002


On Fri, Feb 08, 2002 at 08:04:43PM -0800, E J wrote:
> 
> > =====
> > "Ovid" on http://www.perlmonks.org/
> > Someone asked me how to count to 10 in Perl:
> > push at A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
> > shift at a;shift at a if $a[$[]eq$[;$_=join q||, at a};print $_,$/for reverse @A
> > 
> 
> so how would you say (print..?) the alphabet. I wrote a script to 
> display (in my browser) a list of the doc directories in my 
> /usr/share/doc dir by the letter of the alphabet.  Apache's index 
> listing didn't do it for me, because it displayed *all* of them, and 
> took quite a while.
> 

	Something like:

	print join(' ', 'a' .. 'z' ) . "\n";

	Which will output:

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

	The important part of which is the .. operator, which returns an
array representing the range between 'a' and 'z'.
	Range in this case refers to the ascii characters between 'a' and
'z'.


> This is the pertitant portion of the script;
> ------------
> my $alphabet = "abcdefghijklmnopqrstuvwxyz";
> for $item (split //, $alphabet) {
>     print $q->a({-href=>"docs.cgi?letter=$item"},$item),"\ \n";
> }
> 

	The equivalent would look something like:

foreach $item ('a' .. 'z') {
  print $q->a({-href=>"docs.cgi?letter=$item"},$item),"\ \n";
}

	If you want to get fancy, you can do it with the map function,
which would look something like:

print map (
  $q->a({-href=>"docs.cgi?letter=$_"},$_)."\ \n",
  'a' .. 'z'
);

	where map works somewhat like a for ('a' .. 'z') { ... } loop.

	Austin
TIMTOWTDI



More information about the Pdx-pm-list mailing list