Home: Perl Programming Help: Regular Expressions:
Use of ord in s/$var/\ord/gie


Jul 19, 2000, 11:44 AM

Views: 6676
Use of ord in s/$var/\ord/gie

Hello everyone,

Could someone be so kind as to help me make this work?

I have the following code that works as intended:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$var = "Test";
$lne = length($var);
for ($letter = 0; $letter < $lne; $letter++) {
$trn_msg = substr($var, $letter, $lne);
$x .= "&#" . ord($trn_msg) . ";";
$var = $x;
print "\$var = $var";

I want to take a string and translate every character of it to it's HTML code equivalent, so as a result I get

$var = &# 84 ; &# 101 ; &# 115 ; &# 116 ;

(I had to put extra spaces to prevent the browser from converting them)

I'll like to use a regexp to do the same, so I tried:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$var =~ s/[\w\-\_\.\@]/\ord/gie;
print "\$var = $var ";

but as a result I get:

$var = SCALAR(0x876f0bc)SCALAR(0x8764fe4)SCALAR(0x8765044)SCALAR(0x87650a4)

and if I use the -w switch it gives the message:

use of uninitialized value in ord

I also don't know how to go about including "&#" and ";" at the beginning and end of the output for the regexp just as I did for the code that works.

Any help and suggestions will be greatly appreciated. Thank you in advance for your cooperation, and Be Well.

[This message has been edited by LeoF (edited 07-19-2000).]

Enthusiast / Moderator

Jul 19, 2000, 12:24 PM

Views: 6676
Re: Use of ord in s/$var/\ord/gie

I don't know what inspired you to put a \ in front of ord. What your method does is, for each character matched, you replace it with a reference to the value returned by ord. ord with no arguments is ord($_), but since $_ is undefined, it's like ord(undef). That's why you're getting that 'use of uninitialized value' warning. ord(undef) is 0, so you're getting a reference to the value 0 (which is something like SCALAR(0xADDR)).

If you want to use a regex, here's one method:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$str =~ s/(.)/'&#' . ord($1) . ';'/egs;
# the /e means evaluate the RHS as Perl code
# the /g means do this globally
# the /s means that . matches EVERY character

"RHS" is short-hand for "right hand side". I need the /s modifier here because /./ normally doesn't match a newline. If you don't want to change EVERY character then change what's inside the parens on the LHS:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$str =~ s/([\w.-])/'&#' . ord($1) . ';'/eg;
# no need for the /s anymore

There are other ways to do this, too:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$str =
join '',
# join with an empty string

map '&#' . ord . ';',
# map gets each character as $_, so ord is ok

split //, $str;
# split $str into a list of its characters

Or use the HTML::Entities module (you might need to download it from http://search.cpan.org/):

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use HTML::Entities;
$str = encode_entities($str, "\000-\377");
# we want EVERY character encoded, so we pass
# "\000-\377" to be used as a character class

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher

[This message has been edited by japhy (edited 07-19-2000).]


Jul 19, 2000, 2:36 PM

Views: 6676
Re: Use of ord in s/$var/\ord/gie

Hi Jeff

As to what inspired me to put a \ in front of ord, I wish I could remember, I've tried getting this one right for so long that I can no longer keep track of the many ways ($_, $_[0], $1, etc) that I used. I guess before putting it in I wasn't getting any transformation on the string.

I tried

$str =~ s/(.)/'&#' . ord($1) . ';'/egs;

and it works great, that's just what I needed.

Thanks again for you time and attention, you're very kind. I appreciate you showing different ways of doing things, I hadn't programmed in over 10 years and I'm just starting with Perl, this language seems so powerful that I just wish I'd more time available to dedicate to learning it fully, and your examples are sure a big help towards this end. I've saved them for future reference.

Be Well