How to Remove special characters from name-value pairs in the Webspeed URL? - progress-4gl

I need to remove carriage return and linefeed characters that are present in Webspeed URL containing name-value pairs..How can that be done? any ideas please!

To replace characters you can use the REPLACE function
REPLACE function
Returns a string with specified substring replacements.
Syntax
REPLACE ( source-string , from-string , to-string )
Example:
DEFINE VARIABLE cTxt AS CHARACTER NO-UNDO FORMAT "x(20)".
DEFINE VARIABLE cNewTxt AS CHARACTER NO-UNDO FORMAT "x(20)".
cTxt = "abc123abc123abc123".
cNewTxt = REPLACE(cTxt, "a", "-").
DISPLAY cNewTxt .
You could target new lines using the control code ~n
REPLACE(cString, "~n", "replacing character").
Or target the individual %0d (decimal ascii code 13) and %0a's (decimal ascii code 10).
REPLACE(cString, CHR(13), "replacing character").
REPLACE(cString, CHR(10), "replacing character").

I have recently had a need to do something like this and found the following to be quite handy. This might be a bit drastic -- it removes all control codes and anything higher than ascii 126. But you can adjust those limits easily enough. (My usage is to populate text fields -- so all of that stuff is illegal input for me.)
define variable hd as character no-undo initial "0123456789ABCDEF".
function hex2char returns character ( h as character ):
define variable i as integer no-undo.
if length( h ) <> 2 or index( hd, substring( h, 1, 1 )) < 0 or index( hd, substring( h, 2, 1 )) < 0 then
return "".
i = ((( index( hd, substring( h, 1, 1 )) - 1 ) * 16 ) +
index( hd, substring( h, 2, 1 )) - 1
).
if i < 32 or i >= 127 then
return "".
else
return chr( i ).
end.
function url-decode returns character ( input url as character ):
define variable xurl as character no-undo.
define variable zurl as character no-undo.
define variable pct as integer no-undo.
/* fix known trouble makers
*/
assign
xurl = replace( url, "+", " " )
xurl = replace( xurl, "%0A%0D", "~n" ) /* <LF><CR> */
xurl = replace( xurl, "%0D%0A", "~n" ) /* <CR><LF> */
xurl = replace( xurl, "%0D", "~n" ) /* <CR> */
.
pct = index( xurl, "%" ).
do while pct > 0 and xurl > "":
assign
zurl = zurl + substring( xurl, 1, pct - 1 ) + hex2char( substring( xurl, pct + 1, 2 ))
xurl = substring( xurl, pct + 3 )
pct = index( xurl, "%" )
.
end.
return zurl + xurl.
end.
display url-decode( sampleUrl ) view-as editor size 60 by 25.

Related

How to split word files by the number of characters

Could you anybody help me how to split word file by character!
I can't find any way to split word files by the number of characters on the internet!
For example, to split a document into 500-character blocks:
Sub SplitDocument()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long, j As Long
Const Char As Long = 500
With ActiveDocument
' Process each character block
For i = 1 To Int(.Characters.Count / Char)
j = j + 1
' Get the character block
Set Rng = .Range((i - 1) * Char, i * Char)
' Copy the character block
Rng.Copy
Rng.Collapse wdCollapseEnd
Call NewDoc(ActiveDocument, (i - 1) * Char + 1, j)
Next
If Rng.End < .Range.End Then
i = i + 1: j = j + 1
Rng.End = .Range.End
' Copy the range
Rng.Copy
Rng.Collapse wdCollapseEnd
Call NewDoc(ActiveDocument, (i - 1) * Char + 1, j)
End If
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Sub NewDoc(DocSrc As Document, i As Long, j As Long)
Dim DocTgt As Document, HdFt As HeaderFooter
' Create the output document
Set DocTgt = Documents.Add(Visible:=False)
With DocTgt
' Paste contents into the output document, preserving the formatting
.Range.PasteAndFormat (wdFormatOriginalFormatting)
' Replicate the headers & footers
For Each HdFt In DocSrc.Sections(DocSrc.Characters(i).Sections(1).Index).Headers
.Sections(1).Headers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
Next
For Each HdFt In DocSrc.Sections(DocSrc.Characters(i).Sections(1).Index).Footers
.Sections(1).Footers(HdFt.Index).Range.FormattedText = HdFt.Range.FormattedText
Next
' Save & close the output document
.SaveAs FileName:=Split(DocSrc.FullName, ".doc")(0) & "_" & j & ".docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Set DocTgt = Nothing: Set DocSrc = Nothing
End Sub

copy text in square brackets in filemaker pro

I am trying to do script in Filemaker Pro to copy any text in square brackets in a field.
The text will vary in length and position, can't seem to get anything to work.
Any ideas?
Thanks
You can extract the text inside the first pair of square brackets by using the following calculation =
Let ( [
start = Position ( YourField ; "[" ; 1 ; 1 ) + 1 ;
end = Position ( YourField ; "]" ; 1 ; 1 )
] ;
Middle ( YourField ; start ; end - start )
)
Note that this assumes your field contains at least one pair of square brackets.

Cryptic TypeError: 'decimal.Decimal' object cannot be interpreted as an integer

I am struggling to understand why this function apparently fails in the Jupyter Notebook, but not in the IPython shell:
def present_value( r, n, fv = None, pmt = None ):
'''
Function to compute the Present Value based on interest rate and
a given future value.
Arguments accepted
------------------
* r = interest rate,
which should be given in its original percentage, eg.
5% instead of 0.05
* n = number of periods for which the cash flow,
either as annuity or single flow from one present value
* fv = future value in dollars,
if problem is annuity based, leave this empty
* pmt = each annuity payment in dollars,
if problem is single cash flow based, leave this empty
'''
original_args = [r, n, fv, pmt]
dec_args = [Decimal( arg ) if arg != None
else arg
for arg in original_args
]
if dec_args[3] == None:
return dec_args[2] / ( ( 1 + ( dec_args[0] / 100 ) )**dec_args[1] )
elif dec_args[2] == None:
# annuity_length = range( 1, dec_args[1] + 1 )
# Not allowed to add a Decimal object
# with an integer and to use it
# in the range() function,
# so we dereference the integer from original_args
annuity_length = range( 1, original_args[1] + 1 )
# Apply discounting to each annuity payment made
# according to number of years left till end
all_compounded_pmt = [dec_args[3] * ( 1 / ( ( 1 + dec_args[0] / 100 ) ** time_left ) ) \
for time_left in annuity_length
]
return sum( all_compounded_pmt )
When I imported the module that this function resides in, named functions.py, using from functions import *, and then executed present_value(r=7, n=35, pmt = 11000), I got the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-93-c1cc587f7e27> in <module>()
----> 1 present_value(r=7, n=35, pmt = 11000)
/path_to_file/functions.py in present_value(r, n, fv, pmt)
73 if dec_args[3] == None:
74 return dec_args[2]/((1 + (dec_args[0]/100))**dec_args[1])
---> 75
76 elif dec_args[2] == None:
77 # annuity_length = range(1, dec_args[1]+1)
TypeError: 'decimal.Decimal' object cannot be interpreted as an integer
but in the IPython shell, evaluating this function it works perfectly fine:
In [42]: functions.present_value(r=7, n=35, pmt = 11000)
Out[42]: Decimal('142424.39530474029537')
Can anyone please help me with this really confusing and obscure issue?

QBASIC Decimal to Binary conversion

I have converted a decimal number to binary using STR$() in QBASIC. But I need a way to convert decimal number to binary without using string functions. Thanks.
My Code :
CLS
INPUT N
WHILE N <> 0
E = N MOD 2
B$ = STR$(E)
N = FIX(N / 2)
C$ = B$ + C$
WEND
PRINT "Output "; C$
END
This code sample converts a numeric value to a binary string in Basic.
PRINT "Enter value";
INPUT Temp#
Out3$ = ""
IF Temp# >= False THEN
Digits = False
DO
IF 2 ^ (Digits + 1) > Temp# THEN
EXIT DO
END IF
Digits = Digits + 1
LOOP
FOR Power = Digits TO 0 STEP -1
IF Temp# - 2 ^ Power >= False THEN
Temp# = Temp# - 2 ^ Power
Out3$ = Out3$ + "1"
ELSE
Out3$ = Out3$ + "0"
END IF
NEXT
END IF
PRINT Out3$
END
When you want to display an integer value as binary, it seems logical to me to store it in a string variable, because it's only for display. So I'm not really sure what you are trying to do here.
Maybe you were looking for LTRIM$ so you would get outputs like 11010 instead of 1 1 0 1 0 ?
You could store it in an integer value like in the code below. But, although the integer value will look the same as the string variable, it will in fact be a completely different value.
CLS
INPUT "Type a decimal number:", N
S$ = ""
I = 0
P = 1
WHILE (N <> 0)
' get right most bit and shift right
E = N AND 1
N = INT(N / 2) ' bit shift right
' format for dsplay
S$ = LTRIM$(STR$(E)) + S$
I = I + (E * P)
P = P * 10
WEND
PRINT "Binary as string="; S$
PRINT "Binary as int="; I
END

Formatting Formulas by Code?

I have the following formula as the grouping for a Cross Tab Report:
{Command.Year} & ' ' & {Command.RF Period}
Year is a SmallInt and Period is a TinyInt.
The problem is that it shows on the report as:
2,009.00 9.00
The database values are actually:
2009 9
I can't remove the decimal places via formatting because they are in the formula together.
Ultimately I'd like it to be:
2009 09
Edit:
I found this link: http://www.kenhamady.com/form15.shtml
Now my code looks like this for period:
WhileReadingRecords;
StringVar text := Totext ( {Command.RF Period} , 6 , "" ) ; //put your numeric field in this line
NumberVar end := length ( text ) ;
NumberVar clip :=
(if Val ( text [ end - 6 to end ] ) = 0 then 1 else 0 ) +
(if Val ( text [ end - 5 to end ] ) = 0 then 1 else 0 ) +
(if Val ( text [ end - 4 to end ] ) = 0 then 1 else 0 ) +
(if Val ( text [ end - 3 to end ] ) = 0 then 1 else 0 ) +
(if Val ( text [ end - 2 to end ] ) = 0 then 1 else 0 ) +
(if Val ( text [ end - 1 to end ] ) = 0 then 1 else 0 ) +
(if Val ( text [ end - 0 to end ] ) = 0 then 1 else 0 ) ;
text [ 1 to Length ( text ) - clip ]
However, I don't use Crystal Language, I use VB. How do I append a 0 in front of the period if it does not begin with a 1?
The problem now is that September (9) shows up after October, Nov, and Dec because aphabetically 9 comes after 1.
Anybody?
The ToText function is very useful for this kind of thing, no loops required. In Crystal's VB Syntax :
Formula = ToText({Command.Year}, 0, "") & " " & ToText({Command.RF Period}, "00")
This should work if {Command.Year} and {Command.RF Period} are integers as you describe.