CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need Script to display previous ASCII character, when a ASCII character entered.

 



abilash1000
New User

Apr 22, 2015, 4:32 AM

Post #1 of 9 (4714 views)
Need Script to display previous ASCII character, when a ASCII character entered. Can't Post

HI I am new to scripting, could any one help me to write a script to display previous ASCII character, from the ASCII Table when a ASCII character entered.

Please feel free to get back to me.

Thanks.


BillKSmith
Veteran

Apr 22, 2015, 8:33 AM

Post #2 of 9 (4692 views)
Re: [abilash1000] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

What have you tried? Do you need help with the I/O or with the computation of the previous character? I recommend that you explicitly limit the scope of your problem to printable (not control) standard (not extended) ASCII characters. Display an error message if either input or output is out of scope.

Use the functions ord and chr. Refer: perldoc perlfunc.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 22, 2015, 8:39 AM

Post #3 of 9 (4692 views)
Re: [abilash1000] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

I do not understand exactly what you want. You want the script to print the ASCII code (e.g. 88) when someone enters a letter at the keyboard? If this is what you want, take a look at the ord built_in.

At the command line prompt, type this:


Code
$perldoc -f ord


and this should princit something like this on your screen:

Code
         ord EXPR 
ord Returns the numeric (the native 8-bit encoding, like
ASCII or EBCDIC, or Unicode) value of the first
character of EXPR. If EXPR is omitted, uses $_.

For the reverse, see "chr". See perlunicode for
more about Unicode.

Page 1 (printed 4/22/15)



abilash1000
New User

Apr 22, 2015, 9:25 AM

Post #4 of 9 (4681 views)
Re: [Laurent_R] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

Hi All,

Thanks for your reply.

I trying to write a script where, if I enter "test123" I should get "sdrs012";

It is tricky here, If I enter a ASCII character, including symbols; I would like to get its previous ASCII character in the ASCII Table.

So, when we check on the ACII Table "http://www.asciitable.com" the previous character for the word test123 is as follows;
t=s,e=d,s=r,t=s,1=0,2=1,3=2.

Hope, this clarifies what I am trying to do.

Thanks for your help.

Regards,
Abi


Laurent_R
Veteran / Moderator

Apr 22, 2015, 9:55 AM

Post #5 of 9 (4674 views)
Re: [abilash1000] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

Hi,

you would have to split the string into an array of individual characters, find the ASCII value just below and join the array into the new string. Something like this (but this is untested, I do not have Perl installed on my mobile device):

Code
my @chars = split //, "test123" ; 
$_ = chr (ord ($_) -1) for @chars;
my $new_string = join '', @chars;
print $new_string;


Update: I've now tested it and it duly prints "sdrs012".

Note however that you'll have to figure out what to do when you subtracting 1 for the ASCII value leads you out of range (or give you a nion printable character).


(This post was edited by Laurent_R on Apr 22, 2015, 10:15 AM)


BillKSmith
Veteran

Apr 22, 2015, 1:52 PM

Post #6 of 9 (4662 views)
Re: [Laurent_R] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

OK, you want to translate a whole string, not just a single character. This is exactly what the translation operator is for.

Ignoring invalid input which Laurent and I both mentioned in previous posts:

Code
use strict; 
use warnings;
my $string = 'test123';
$string =~ tr/!-~/ -}/;
print $string, "\n";

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 23, 2015, 12:14 AM

Post #7 of 9 (4654 views)
Re: [BillKSmith] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

Hi Bill,
I thought about the tr/// operator and concluded that it was not really possible, or would end up to be too complicated.

I must say that I don't quite understand the way you are suggesting to use it, and I am curious: are you just defining a wide ASCII range covering all the printable characters?


BillKSmith
Veteran

Apr 23, 2015, 5:50 AM

Post #8 of 9 (4619 views)
Re: [Laurent_R] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

Laurent,

Your guess about a wide range is correct. The book "Unix in a Nutshell" specifies the ASCII character set in two tables ("Nonprinting Characters" and "Printing Characters"). The second specifies the graphic for characters \x020 (space) through \x07e (~). I decided that the translation only made sense if the input and output were both "Printing Characters". This required translating \x021 (!) through \x07e (~) to \x020 (space) through \x07d (}). I doubted that the tr operator would accept the escape notation so I used the actual characters.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 23, 2015, 10:17 AM

Post #9 of 9 (4573 views)
Re: [BillKSmith] Need Script to display previous ASCII character, when a ASCII character entered. [In reply to] Can't Post

Thank you, Bill, for your answer. I could have checked by myself, but did not have an ASCII table on my mobile device when I asked my question.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives