 |
| |
Perl ord Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
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 Perl 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.
Check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
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.
Subscribe now to my FREE newsletter (regarding tips and tricks to manipulate multidimensional arrays and hashes in Perl) and you'll receive a link to download for FREE my Perl chomp Function Tutorial eBook (a $7.50 value), it's a bonus for you and I hope you'll enjoy it. You can see its Table of Contents here (if you subscribe to my free newsletter, you don't have to pay for this eBook or use your paypal account).
Don't forget to check my new How To Tutorial eBooks (PDF format):to see a lot of fully commented examples that help you use the Perl statements and the Perl buit-in functions in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :
Perl How To Tutorial eBooks
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 Array Functions Functions in alphabetical order chomp chop chr crypt defined grep hex index join lc lcfirst length map oct ord pack pop push qw reverse rindex scalar shift sort splice split substr uc undef unshift
return from Perl ord function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!


|
|