Perl ord Function
This short Perl ord Tutorial will show how to use this function in your scripts. This function is used to convert a character to its ASCII or Unicode numeric value. The reverse of this function is the chr function which is used to convert an ASCII or Unicode value into its equivalent character.Two syntax forms are available for the ord function:
This function:- has as argument an expression
- returns the value of the first character of EXPR
If the Perl ord Function has no argument, the $_ is used instead.
See the following simple example of the
ord function usage:
#!/usr/local/bin/perl
use strict;
use warnings;
print ord('A'), "\n";
# it displays: 65
| Convert a character string into an ASCII code array |
The following example shows you how to use the Perl
ord function in conjunction with the
split and
map functions.
#!/usr/local/bin/perl
use strict;
use warnings;
# initialize a string
my $str = "Hello World!";
# split the string into an array of characters
my @array = split //,$str;
# converts the elements of the array into their
# equivalent ASCII codes
@array = map(ord, @array);
# print the array with spaces between elements
print "@array\n";
# it outputs: 72 101 108 108 111 32 87 111 114 108 100 33
I want to make a few considerations about this code.
- the split function is used with a null string as pattern, so the $str will be split into separate characters
- the map function will run the ord function for each element of the @array; at each iteration step the current element is assigned in turn to $_ and next the ord function will act against the special variable $_; the map function returns the same array but having ASCII values as elements
- the elements of the @array are printed using the space delimiter; if you print an array enclosed in double quotes, its elements will be printed separated by space
The one-line
map function:
@array = map (ord, @array);
can be rewritten with any of the following code lines:
@array = map (ord $_, @array);
@array = map { ord } @array;
@array = map { ord $_ } @array;
By the way, calling the
map function with the expression syntax form is faster than calling it with the block syntax form.
A quicker alternative to do this is by using the unpack function, as in the following example:
#!/usr/local/bin/perl
use strict;
use warnings;
# initialize a string
my $str = "Hello World!";
my @array = unpack("C*", $str);
print "@array\n";
# it displays: 72 101 108 108 111 32 87 111 114 108 100 33
Please note that there is an important difference between the Perl
ord and the
unpack functions. Whereas
ord works on characters, the unpack "C" function works on bytes. If you use the standard ASCII character set, you’ll get the same result. But if you use Unicode, the
unpack function will behave the same meanwhile the
ord function will deal with the full Unicode character, which may be longer than 1 byte.
| Convert a character string into a hexadecimal string |
Please have a look at the following short snippet code:
#!/usr/local/bin/perl
use strict;
use warnings;
# initialize a string
my $str = "Hello World!";
$str =~ s/(.)/sprintf("%x",ord($1))/eg;
print "$str\n";
# it displays: 48656c6c6f20576f726c6421
I want to make a few remarks about how this snippet works:
- the $str scalar variable will be converted to its hexadecimal corresponding value using the s/searchpattern/replacement/modifiers substitution regexp operator and the bind operator =~
- (.) This is the search pattern: the dot means we match any single character and the using of the round parentheses allow us to store the matched character in the special variable named $1 (if you have more parentheses, the expression included in the second parenthesis will be assigned to $2, and so on)
- sprintf("%x",ord($1)) is the replacement argument of the substitution operator and it is pure Perl code; the Perl ord function returns the ASCII numeric value of the character stored in $1; next sprintf will convert this numeric value into its hexadecimal corresponding value (you can use %X if you want the hex values in uppercases)
- e is a modifier and it tells the regex engine to treat the replacement field as Perl code (see above)
- g is a modifier and tells the regex engine to repeatedly apply the substitution for all the characters of the string, starting with the first one
Please click here to download the Perl ord script with all the above examples included.
Table of Contents:
A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
Array Size
Array Length
Perl Hashes
Perl Statements
Perl if
Perl unless
Perl switch
Perl while
Perl do-while
Perl until
Perl do-until
Perl for
Perl foreach
Built-in Perl Functions
Functions by Category
String Functions
Regular Expressions and Pattern Matching
List Functions
Array Functions
Hash Functions
Miscellaneous Functions
Functions in alphabetical order
chomp
chop
chr
crypt
defined
delete
each
exists
grep
hex
index
join
keys
lc
lcfirst
length
map
oct
ord (more)
pack
pop
push
q
qq
qw
reverse
rindex
scalar
shift
sort
splice
split
sprintf
substr
tr
uc
ucfirst
undef
unpack
unshift
values
return from Perl ord function to Perl Basics
Would you like to create your own website like this one?
Hit the Alarm Clock!