problems in printing each digit of a number in erlang - erlang

I am trying to make a program that will read in a number and then output every digit of that number in a list. However, most of the things look fine until I try with number 8 and 9. The program only output \b \t instead.
if the input number contains 8 or 9, and in the same time there are other numbers, for example 283, it will print normally. Otherwise if there is only 8 or 9, such as8, 99, then it will give me that binary representation of 8 and 9 (if I remember correctly).
My program is as below:
digitize(0)-> 0;
digitize(N) when N < 10 -> [N];
digitize(N) when N >= 10 -> digitize(N div 10)++[N rem 10].

The function returns the expected list, but the shell shows lists of numbers which are ASCII-codes of characters as strings (because that's just what strings are in Erlang; there's no special string type). You can see it by just entering [8, 8] (e.g.) at the prompt and disable this behavior by calling shell:strings(false) (and shell:strings(true) when you need the normal behavior again).

Strings in Erlang are no separate type but a list of numbers. List printing has a heuristic to detect when it might be a string. If it thinks it's a string it will be printed as such. \b is the backspace character and \t is the tab character which are ASCII codes 8 and 9
See also:
Description what a string means
Erlang escape sequences
Explanation of this in LYSE

Related

How do you define the length of a parameter in ESC/POS?

I need to be able to print Hebrew characters on my Epson TM-T20ii. I am trying to get my printer to switch to character code page 36(PC862) using
ESC t36
for some reason the printer is switching to code page 3 and then printing the number 6.
Is there a way to let the printer know that the 6 is part of my command?
If you know of a different workaround please comment below.
Thanks
You are making a mistake, you aren't meant to replace n with an actual number.
The proper syntax in your case would be ←t$
Explanation: the manual says "ESC t n", n referring to the page sheet, however you don't replace n with a number rather with the ASCII character n, so in your example 36 = $ because $ is the 36th character on the ASCII table.

Regex for clearly single line words with wildcards in Swift

I'm attempting to construct a regex string in Swift 4 that gets characters at the start of a line where some are known and others aren't.
Let's say I've got a text file with line breaks for each word that reads as follows:
pucker
tuckered
duckerdinger
sucker punch
I'd like to get every word that contains "cker" in it that's 1 to 8 characters long.
I'm attempting to use this statement ^..cker..{1,8} as my RegEx string. All I'm getting is a partial match in Patterns (a Mac App), but Regex101.com's saying no match, and most importantly, Xcode says I'm using an invalid regex. I've also tried ^(..cker..) and a bazillion other variations.
What am I screwing up and how do I fix it? What I'm trying to do seems like it would be super simple, but I've wasted more time than I care to admit fiddling with it.
Update:
This has been the best I've been able to get so far...
"\\b..cker..", but I'm only able to get words that are exactly 8 characters long. I'd like to capture words that contain "cker" that are the 3rd, 4th, 5th, and 6th letters while capturing words up to 8 characters long.
Try this regex:
\b(?=.*cker)[a-zA-Z]{1,8}\b
Click for Demo
Explanation:
\b - matches a word boundary
(?=.*cker) - Positive Lookahead to make sure our string should contain the character sequence cker
[a-zA-Z]{1,8} - Matches 1 to 8 occurrences of a letter
\b - matches a word boundary

A roadblock working with erlang's List

I was working with list in erlang which is filled with a single value each time and I wanted to modify this list by multiplying its value with 10. But when I tried this the following thing happened:
E=[4*10].
"("
I searched the ascii table and found that ascii value 40 is stored for the symbol "(" only.
Can anybody trow some light on it and also tell me how I can get E=[40] by performing the multiplication inside the List only?
Strings are represented as lists of bytes in Erlang and thus saying "(" it's exactly the same as [40].
It's just a syntactic sugar. Every time Erlang displays a list, if it contains "displayable" ASCII characters it will display the string instead of the list of numbers.
You can user format to control de display:
io:format("Number ~w is character ~c\n", [40 40]).

Erlang: Strange chars in a generated list

Trying to generate a list through comprehension and at some point I start seeing strange character strings. Unable to explain their presence at this point (guessing the escape chars to be ASCII codes - but why?):
45> [[round(math:pow(X,2))] ++ [Y]|| X <- lists:seq(5,10), Y <- lists:seq(5,10)].
[[25,5],
[25,6],
[25,7],
[25,8],
[25,9],
[25,10],
[36,5],
[36,6],
[36,7],
"$\b","$\t","$\n",
[49,5],
[49,6],
[49,7],
"1\b","1\t","1\n",
[64,5],
[64,6],
[64,7],
"#\b","#\t","#\n",
[81,5],
[81,6],
[81,7],
"Q\b",
[...]|...]
In Erlang all strings are just list of small integers (like chars in C). And shell to help you out a little tries to interpret any list as printable string. So what you get are numbers, they are just printed in a way you would not expect.
If you would like to change this behaviour you can look at this answer.

COBOL accurate LENGTH OF (XML-TEXT) when ampersands escaped?

I'm using the intrinsic function XML-PARSE with XML that looks like this:
<MSGBODYTXT>
<LN>One & two</LN>
</MSGBODYTXT>
By my count, the following string is 13 bytes long.
"One & two"
But when I take $
LENGTH OF(XML-TEXT) $
I get only 9 bytes.
What can I do to get the correct, 13-byte length?
The problem is that XML PARSE translates & into the character it represents, an ampersand. If
you look at the CONTENT-CHARACTERS associated with the <LN> tag you will see: One & two which
is 9 characters long, just as the LENGTH OF operator on XML-TEXT says it is.
Note that if you were to use XML GENERATE on data item LN having the value One & two
it will generate as <LN>One & two</LN> which is a symetrical operation.

Resources