user974896
user974896

Reputation: 1813

Adding character litterals perl (very simple)

If anyone wants some quick rep here you go :).

How can I add character litterals like I can do in C. For example

 print 'A' + 1

The above should print 'B' since ASCII 'A' + 1 gives ASCII 'B'

Upvotes: 1

Views: 75

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

If you store strings in variables then you can increment them.

$ perl -E'$a = "A"; say ++$a'
B

$ perl -E'$a = "abacaa"; say ++$a'
abacab

$ perl -E'$a = "Z"; say ++$a'
AA

Upvotes: 2

hobbs
hobbs

Reputation: 240473

chr(ord('A') + 1). Perl doesn't have a character type, it has a string type. And a string doesn't behave numerically as an ASCII value. You want ord to convert it to a numeric codepoint and chr to convert it back.

Upvotes: 5

Related Questions