Solved

ord function in perl

Posted on 2002-06-28
8
1,592 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
crawling gofundme 4 126
Exchange 2010 Transport Rule Regex 28 107
Regular Expression question to filter with negation. 6 95
Is it true to say that Python is successor of Perl? 7 173
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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