Solved

ord function in perl

Posted on 2002-06-28
8
1,582 Views
Last Modified: 2012-05-04
The ord function returns the ascii value for the character .
$str = "o";
print ord $str; - returns 111

What will be the ord value for non-ascii character ?
$str = "ó";
print ord $str; - returns 198

Actually the ascii decimal equivalent for the above non-ascii character is 243 . Why is the ord value returns 198 ? How should I get the ascii decimal for non-ascii charcters in perl ?

Thanks for the help .
Lakshmi


0
Comment
Question by:lakshmisubram
  • 4
  • 4
8 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7118177
What you see on the screen for "ó", and what it is in real life in the file, depends on a lot of configurations (for example your font in X, keyboard mappings, etc.).

Please use
  od -c file-conatinig-above-script
to see which octal value it is.
If you use
  $str="\306";
you get what you want.
0
 

Author Comment

by:lakshmisubram
ID: 7120186
My query is 'impresión' . Before sending to my search engine , I have to encode this query string manually . Through form submission , the query is automatically encoded and coming as 'impresi%F3n' . But when I encode it manually , I am getting 'impresi%C6n' . The decimal equivalent of %C6 is 198 and %F3 is 243.
The ord function returns the ascii decimal value for the ascii characters properly . But the ord value for non-ascii characters are wrong . Hence , the query sent to my search engine is wrong .
I appreciate your immediate help .
Thanks .
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7120586
my suggestion about the charcter "in real life" does not only apply to your file, but also to any browser. Same dragon there.
In your CGI you must rely on what you get, you have no possibility to identify, just to imagine, what the user, sitting in front of the browser, want to send you. You have to believe it.

Either you tell the users which font, which keyboard mapping, etc. they have to use, or you need to use what you get from them.

Probaly some experts using KOI8 and ISO8859-* fonts are listening here too, they might tell you more details about the font-dragons (I'm just a pure latin charset user;-)
0
 

Author Comment

by:lakshmisubram
ID: 7121255
My question is nothing related to browser or what the user type in . I am taking my query from a file which is 'impresión' . Before sending to my search engine , I want to encode my query as 'impres%F3n' . But I get the encoded value as 'impresi%C6n' using the below function .

sub URLEncode {
    my $theURL = $_[0];
   $theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
   return $theURL;
}

Please correct me if my subroutine for encoding miss anything .
This is urgent . Please reply .
Thanks for your response .
Lakshmi
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 7121271
as I said in my very first comment, the problem then is in your 'impresión'  file, the character is wrong there
0
 

Author Comment

by:lakshmisubram
ID: 7123962
As you said , it's problem with the string in file .
If I give 'impresiµn' in my file , it is escaping correctly as impresi%F3n .

Thank you so much for your immediate answers !!

One more help if possible .
How will I know that 'impresión' to be given as 'impresiµn' ? (There are so many accented charaters in my file .) How will I find out for those ?

Thanks again .

 
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7124052
echo 'impresión' | perl -pe '$_=~tr/[a-zA-Z0-9,._]//d;'
0
 

Author Comment

by:lakshmisubram
ID: 7124340
Executing the above gives the same ó character only know ?
Or I am doing something wrong ?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now