Formatting not working in 4gl [ USING clause ] - informix

I was trying to format the output variable as follows:
Scenario 1:
LET msg = "Roopesh Majeti here "
Empno Using "&&&&&"
Call Logmsg(msg)
In this scenario, the empno get formatted to 5 digits [ I should say spaces ] and gets displayed.
Scenario 2:
Let c_check = "&&"
Let msg ="Roopesh Majeti here"
Empno using c_check
Call Logmsg(msg)
Here, my expectation is that, whatever is the value of c_check [ say 10 &'s ] then the empno should be formatted to 10 positions/spaces.
But it's not working as expected.
Am I missing anything here?

You don't say what you do get, which makes it hard to know how it is not working.
You have some minor syntax errors, missing the commas after the constant strings.
You don't show the declaration of msg or c_check; could the problem be that the message is being truncated?
What do you get when you use:
DISPLAY "<<", msg CLIPPED, ">>"
DISPLAY "<<", msg, ">>"
You should be able to show us a complete program like this, and its output:
MAIN
DEFINE msg CHAR(64)
DEFINE c_check CHAR(20)
DEFINE empno INTEGER
LET empno = 12345
LET msg = "Roopesh Majeti here ", empno Using "&&&&&"
DISPLAY "<<", msg CLIPPED, ">>"
DISPLAY "<<", msg, ">>"
LET empno = 987654321
LET c_check = "&&"
LET msg ="Roopesh Majeti here", empno USING c_check
DISPLAY "<<", msg CLIPPED, ">>"
DISPLAY "<<", msg, ">>"
END MAIN

Related

Replace last comma on string Lua

I have this string
Apples, Oranges, Grapes
How can i replace last comma for and?
Havent learn enough of patterns but i've tried several ways such as
str:gsub(",$", "and", 1)
Isnt supposed that magic character $ reads string from end --> begin?
My problem becomes because im concatenating an array using table.concat
Your table:
local t = {"Apples", "Oranges", "Grapes"}
Method #1:
Concatenate the last item using "and":
local s = #t > 1
and table.concat(t, ", ", 1, #t-1).." and "..t[#t]
or table.concat(t, ", ")
print(s) --> Apples, Oranges and Grapes
Method #2:
Replace the last comma:
local s = table.concat(t, ", ")
s = s:gsub("(.*),", "%1 and")
print(s) --> Apples, Oranges and Grapes
local str = "Apples, Oranges, Grapes"
print(str:gsub(",(%s+%w+)$", " and%1"))

How to replace spaces between words with another word?

I have a cell G which stores random words like "Hello, How are you"
I want to replace all the spaces with %20 to make it
"Hello%20,%20How%20%are%20you"
How can i replace spaces with %20?
Thanks
try like this:
=SUBSTITUTE(G1; " "; "%20")
Select the cells you wish to change and run this:
Sub SpaceChanger()
Dim rng As Range, r As Range
Set rng = Intersect(Selection, ActiveSheet.UsedRange)
For Each r In rng
If r.Value <> "" Then
If InStr(r.Value, " ") <> 0 Then
r.Value = Replace(r.Value, " ", "%20")
End If
End If
Next r
End Sub
(using a macro will allow you to change the values "in place")

Checking variables exist before building an array

I am generating a string from a number of components (title, authors, journal, year, journal volume, journal pages). The idea is that the string will be a citation as so:
#citation = article_title + " " + authors + ". " + journal + " " + year + ";" + journal_volume + ":" + journal_pages
I am guessing that some components occasionally do not exist. I am getting this error:
no implicit conversion of nil into String
Is this indicating that it is trying to build the string and one of the components is nil? If so, is there a neat way to build a string from an array while checking that each element exists to circumvent this issue?
It's easier to use interpolation
#citation = "#{article_title} #{authors}. #{journal} #{year}; #{journal_volume}:#{journal_pages}"
Nils will be substituted as empty strings
array = [
article_title, authors ,journal,
year, journal_volume, journal_pages
]
#citation = "%s %s. %s %s; %s:%s" % array
Use String#% format string method.
Demo
>> "%s: %s" % [ 'fo', nil ]
=> "fo: "
Considering that you presumably are doing this for more than one article, you might consider doing it like so:
SEPARATORS = [" ", ". ", " ", ";", ":", ""]
article = ["Ruby for Fun and Profit", "Matz", "Cool Tools for Coders",
2004, 417, nil]
article.map(&:to_s).zip(SEPARATORS).map(&:join).join
# => "Ruby for Fun and Profit Matz. Cool Tools for Coders 2004;417:"

Openresty : getting issue with ngx.re.gsub to handle magic characters of lua

I want to replace a word into my body content from other string .
To implement this i am using ngx.re.sgub but i am getting a weird issue. ngx.re.gsub is not handling magic characters.
Example :
content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, body)
ngx.print(content2)
output is
Hi.00lHelo .00 how are you .00all he.00llo .00 how are you .00all
while output should like :
Hi$100.00Hello .00 how are you .00all.
Please let me know how can i achieve this .
In ngx regex, $1, $2, etc. are variable to be captured. Try escape the $ character:
body = "$$100.00"
Wrap the body with a function also avoids it:
content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, function()
return body
end, "o")
ngx.print(content2)

Capitalize first letter of every word in Lua

I'm able to capitalize the first letter of my string using:
str:gsub("^%l", string.upper)
How can I modify this to capitalize the first letter of every word in the string?
I wasn't able to find any fancy way to do it.
str = "here you have a long list of words"
str = str:gsub("(%l)(%w*)", function(a,b) return string.upper(a)..b end)
print(str)
This code output is Here You Have A Long List Of Words. %w* could be changed to %w+ to not replace words of one letter.
Fancier solution:
str = string.gsub(" "..str, "%W%l", string.upper):sub(2)
It's impossible to make a real single-regex replace because lua's pattern system is simple.
in the alternative answer listed you get inconsistent results with words containing apostrophes:
str = string.gsub(" "..str, "%W%l", string.upper):sub(2)
will capitalize the first letter after each apostrophe irregardless if its the first letter in the word
eg: "here's a long list of words" outputs "Here'S A Long List Of Words"
to fix this i found a clever solution here
utilizing this code:
function titleCase( first, rest )
return first:upper()..rest:lower()
end
string.gsub(str, "(%a)([%w_']*)", titleCase)
will fix any issues caused by that weird bug
function titleCase( first, rest )
return first:upper()..rest:lower()
end
string.gsub(str, "(%a)([%w_']*)", titleCase)
BunchOfText {"Yeppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"}
I have a feeling I will be returning to this question when I need to put something in proper title case.
Below is the Lua code to do exactly that.
It has the disadvantage of not preserving the original spacing between words but it's good enough for now.
-- Lua is like python in syntax, and barebones like C -_-
function Set (list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
function firstToUpper(str)
return (str:gsub("^%l", string.upper))
end
function titlecase(str)
-- We need to break the string into pieces
words = {}
for word in string.gmatch(str, '([^%s]+)') do
table.insert(words, word)
end
-- We need to capitalize anything that is not a:
-- - Article
-- - Coordinating Conjunction
-- - Preposition
-- Thus we have a blacklist of such words
local blacklist = Set {
"at", "but", "by", "down", "for", "from",
"in", "into", "like", "near", "of", "off",
"on", "onto", "out", "over", "past", "plus",
"to", "up", "upon", "with", "nor", "yet",
"so", "the"
}
for index, word in pairs(words) do
if(not (blacklist[word] ~= nil)) then
words[index] = firstToUpper(word)
end
end
-- First and last words are always capitalized
words[1] = firstToUpper(words[1])
words[#words] = firstToUpper(words[#words])
-- Concat elements in list via space character
local result = ""
for index, word in pairs(words) do
result = result .. word
if(index ~= #words) then
result = result .. ' '
end
end
return result
end
print(titlecase("the world"))
print(titlecase("I walked my dog this morning ..."))
print(titlecase("The art of Lua"))
--- Output:
----------------------
--- The World
--- I Walked My Dog This Morning ...
--- The Art of Lua

Resources