[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ord function in perl

Posted on 2002-06-28
8
Medium Priority
?
1,612 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

826 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