Tuesday, April 25, 2006

Perl : map function

See what the following code does.

Code:

@numarray = (97 .. 122);
@charray = map(chr, @numarray);
print @charray;

Output:

abcdefghijklmnopqrstuvwxyz

Explanation:
  • .. is a range operator. @numarray contains numbers from 97 to 122
  • map function applies function char on each element of @numarray. The output is stored in @charray.
  • chr function returns the character represented by that NUMBER in the character set.  For example, "chr(65)" is "A".
  • For more information on built-in perl functions, do 'man perlfunc'.

No comments: