Rebol/RED parsing. Save ASCII codes from parsed string to variable? - string

I stumbled into RED language the other day and spend (more or less literally) the last 24h "learning" it. I exhausted my googling skills trying to find solution for a simple problem that just evades my skills and logic, so hopefully somebody here can lead me to path to righteousness.
As the title suggest, I tried to parse a simple string (any string of random text, really), get the individual char(acter)s and then tried to save them into a variable. (Another string/array/any type really)
The best I could do was with code using:
alpha: charset [#"a" - #"z"]
testString: "this is just random rambling to test parsing!"
as prerequisites and something like this when (trying to) parse:
probe parse teststring [copy text to alpha (append text2 to-integer(to-char text)) to end]
Saves the (first letter) ascii code to text2 and running the script several times properly adds the (same first letter) ASCII code several times in a row:
CONSOLE OUTPUT
>> probe parse teststring [copy text to alpha (append text2 to-integer(to-char text)) to end]
true
== true
>> probe text2
"34"
== "34"
>> probe parse teststring [copy text to alpha (append text2 to-integer(to-char text)) to end]
true
== true
>> probe text2
"3434"
== "3434"
Obviously my parsing is not really "looping" the individual characters of the string or not saving them properly while doing it. Maybe my parsing really takes the whole string from TO till END and I try to then convert that to ASCII code or something else is happening here?
ANY help will be greatly appreciated, as I cannot possibly advance with my RED learning before solving this dilemma and understanding how the parsing really works in RED.

I am not sure, if I understand your question and what you want to achieve, but if you are looking for all the ascii representation of the chars you can get that with
asciis: []
parse teststring [some [set a alpha (append asciis to-integer a )| skip]]
== true
>> asciis
== [116 104 105 115 105 115 106 117 115 116 114 97 110 100 111 109 114 97 109 98 108 105 110 103 116 111 116 101 115 116 112 97 114 115 105 110 103]
some is one of the available words responsible for the looping
There are some issues in your trial. It would give some errors before doing any conversion. text2 is probably declared before. to alpha would give an empty string "" as you try to copy up to the first alpha character. Remember to goes up to and not including the target. You can not convert an empty string to a character. If we assume until now no error occurred, then you are still in front of your string and you go straight to the end of your string.
Some documentation about Red parse. see Iteration about looping
You can debug your parsing either with parse-trace or just put a simple (probe text) after the part of your rule you want to investigate:

Related

How to replace an special character?

I am working on an excel vba macro which opens some files but I ran into a problem, some files have a special character and I cannot copy it to be able to make a replacement, I even tried to find the ASCII code but it throws me the same code as the common space, I can only see it in MS Word.
The tiny circle is the special char:
You can recognize characters in Word:
Sub PrintASCII()
s = Selection.Text
For i = 1 To Len(s)
Debug.Print Asc(Mid(s, i, 1))
Next
End Sub
Usage: select the symbols and run this Sub. See output in VBE Immediate window
Output
95
95
95
95
160
95
95
95
95
95
13

Replace Function not giving expected output in Python

I am trying to replace the tab with the expected output as "129hello3 78 0".Its showing same output after running the below mentioned lines of code.
I have given tab space between "129" and "3" and rest others are spaces
a="129 3 78 0"
b=a.replace('\t+','hello')
print(b)
There is no tab in a="129 3 78 0", but rather two spaces. For example, copy and paste tab from the text editor to ensure that the right character is in string a.
Here is the example:
a="129 3 78 0"
b=a.replace(' ','hello')
print(b)
a="129 3 78 0"
b=a.replace('\t','hello')
print(b)
The output is
129hello3 78 0
129hello3 78 0
In order to get this result, please open the editor of your choice (I am using Notepad here) paste the code there, and make sure that you have tab in required spaces (circled in red):
then copy the code from there and insert intoonline compiler of your choice ( I used https://www.programiz.com/python-programming/online-compiler/). Click Run
You are using replace instead of regex.
import re
a="129 3 78 0"
b=re.sub('[\t]+','hello', a)
print(b)
Or if you need replace:
a="129 3 78 0"
b=a.replace('\t','hello')
print(b)

Trying to remove only the characters F or C following numbers

I'm trying to find a regex (that will work in node.js) to remove the Fahrenheit and Celsius letters and replace them with " degrees" from the below weather forecast string.
"Clear skies. Low 46F. NNW winds shifting to ENE at 10 to 15 mph."
I want the above string to read as below:
"Clear skies. Low 46 degrees. NNW winds shifting to ENE at 10 to 15 mph."
There could be more than one instance of a temperature in the string.
NOTE: I only want to remove the F or C if it's immediately following a number with no space in-between. If "Florida" were in the above string, I'd want the letter "F" left untouched.
I've tried the below regex, but it finds the entire 46F. I just want it changed to 46 degrees.
/\d+[FC]/g
Thanks.
That is because the lack of the capturing group (parentheses):
Use this:
/(\d+)[FC]/g
$1 means the first capturing group. The \d+ in this case.
speechOutput = speechOutput.replace(/(\d+)[FC]/g, '$1 degrees');

ASCII text to Hexadecimal in Excel

I want to this but i don't know what to do, the only functions it seems to be useful is "DEC.TO.HEX".
This is the problem, i have in one cell this text:
1234
And in the next cell i want the hexadecimal value of each character, the expected result would be:
31323334
Each character must be represented by two hexadecimal characters. I don't have an idea how to solve this in excel avoiding make a coded program.
Regards!
Edit: Hexadecimal conversion
Text value Ascii Value (Dec) Hexadecimal Value
1 49 31
2 50 32
3 51 33
4 52 34
Please try:
=DEC2HEX(CODE(MID(A1,1,1)))&DEC2HEX(CODE(MID(A1,2,1)))&DEC2HEX(CODE(MID(A1,3,1)))&DEC2HEX(CODE(MID(A1,4,1)))
In your version you might need the .s in the function (and perhaps ;s rather than ,s).
DEC2HEX may be of assistance. Use, as follows:
=DEC2HEX(A3)
First split 1234 to 1 2 3 4 by using MID(), then use Code() for each character, and then again concentate. Below is the formula, Y21 is the cell in which 1234 is written
=CONCATENATE(CODE(MID(Y21,1,1)),CODE(MID(Y21,2,1)),CODE(MID(Y21,3,1)),CODE(MID(Y21,4,1)))
1234 >> 49505152

Maximum amount of characters

can anybody help me with this given assignment? "Write
a
function
that
takes
as
input
the
message
(a
string)
and
checks whether
the
number
of
characters
is
less
than
160
or
not.
If
the
length
of
the
message
is
less
than
160,
the
message
should
be
returned.
If
the
length
of
the
message
is
greater
than
160,
a
string
consisting
of
only
the
first
160
characters
should be
returned."
I am unsure how to design a program that prints a string consisting of only 160 letters if the amount of letters are above.
If the string contains more than 160 characters, loop from 1 to 160 (or 0 to 159 if your language is zero based), each time outputting the character at the position of the loop index. You can also use a substring function if your language has one.

Resources