Solved

ord function in perl

Posted on 2002-06-28
8
1,586 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

929 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

16 Experts available now in Live!

Get 1:1 Help Now