FOREACH I4GL SE - informix

I am trying to get the customers last name, first name - dob, I am getting all but everything printing the same place, any idea?
declare n_curs cursor for
select unique pin,surname,given1,given2,dob from crcharge where
chargenum in (select chargenum from crbookdd where book_no = rpt.book_no)
order by surname,given1
print ESC, "&l4E"
foreach n_curs into t_pin, t_surname, t_given1, t_given2, t_dob
if kick_new then
print column 1, ESC, "&a0G", ESC, "&l3O", ESC, "&f4y3X",
ESC, "&l8D",ESC, "&l4E"
end if
let shtwrd_count = 0
if shtwrd_count > 6 then
let shtwrd[shtwrd_count] = t_shtwrd
let shtwrd_count = shtwrd_count + 1
Exit foreach
end if
print
print ESC,"&a6R", ESC, "&a3C",
upshift(t_surname) clipped, ",",
updown(t_given1) clipped," " ,
updown(t_given2) clipped,"-", t_dob clipped;
end foreach

Because your escape sequence is suppressing the LF (Line Feed) after the CR (Carriage return).

The semi-colon after the PRINT statement means 'suppress newline'. The next PRINT statement continues on the same line, therefore. Very useful when that's the effect you want; otherwise, not good.
Separately, as some advice, you should parameterize the escape sequences. If you don't, then life will become really difficult when you need to change printer or terminal type. Create functions which name the effect and return the correct string:
FUNCTION extra_bold()
RETURN ESC, "&a6R" -- Or whatever
END FUNCTION
Then use them:
PRINT extra_bold(), info.field

Related

How to remove last line from a string in Lua?

I am using Lua in World of Warcraft.
I have this string:
"This\nis\nmy\nlife."
So when printed, the output is this:
This
is
my
life.
How can I store the entire string except the last line in a new variable?
So I want the output of the new variable to be this:
This
is
my
I want the Lua code to find the last line (regardless of how many lines in the string), remove the last line and store the remaining lines in a new variable.
Thank you.
So I found that Egor Skriptunoff's solutions in the comments worked very well indeed but I am unable to mark his comments as an answer so I'll put his answers here.
This removes the last line and stores the remaining lines in a new variable:
new_str = old_str:gsub("\n[^\n]*$", "")
If there is a new line marker at the end of the last line, Egor posted this as a solution:
new_str = old_str:gsub("\n[^\n]*(\n?)$", "%1")
While this removes the first line and stores the remaining lines in a new variable:
first_line = old_str:match("[^\n]*")
Thanks for your help, Egor.
Most efficient solution is plain string.find.
local s = "This\nis\nmy\nlife." -- string with newlines
local s1 = "Thisismylife." -- string without newlines
local function RemoveLastLine(str)
local pos = 0 -- start position
while true do -- loop for searching newlines
local nl = string.find(str, "\n", pos, true) -- find next newline, true indicates we use plain search, this speeds up on LuaJIT.
if not nl then break end -- We didn't find any newline or no newlines left.
pos = nl + 1 -- Save newline position, + 1 is necessary to avoid infinite loop of scanning the same newline, so we search for newlines __after__ this character
end
if pos == 0 then return str end -- If didn't find any newline, return original string
return string.sub(str, 1, pos - 2) -- Return substring from the beginning of the string up to last newline (- 2 returns new string without the last newline itself
end
print(RemoveLastLine(s))
print(RemoveLastLine(s1))
Keep in mind this works only for strings with \n-style newlines, if you have \n\r or \r\n easier solution would be a pattern.
This solution is efficient for LuaJIT and for long strings.
For small strings string.sub(s1, 1, string.find(s1,"\n[^\n]*$") - 1) is fine (Not on LuaJIT tho).
I scan it backward because it more easier to remove thing from back with backward scanning rather than forward it would be more complex if you scan forward and much simpler scanning backward
I succeed it in one take
function removeLastLine(str) --It will return empty string when there just 1 line
local letters = {}
for let in string.gmatch(str, ".") do --Extract letter by letter to a table
table.insert(letters, let)
end
local i = #letters --We're scanning backward
while i >= 0 do --Scan from bacward
if letters[i] == "\n" then
letters[i] = nil
break
end
letters[i] = nil --Remove letter from letters table
i = i - 1
end
return table.concat(letters)
end
print("This\nis\nmy\nlife.")
print(removeLastLine("This\nis\nmy\nlife."))
How the code work
The letters in str argument will be extracted to a table ("Hello" will become {"H", "e", "l", "l", "o"})
i local is set to the end of the table because we scan it from the back to front
Check if letters[i] is \n if it newline then goto step 7
Remove entry at letters[i]
Minus i with 1
Goto step 3 until i is zero if i is zero then goto step 8
Remove entry at letters[i] because it havent removed when checking for newline
Return table.concat(letters). Won't cause error because table.concat return empty string if the table is empty
#! /usr/bin/env lua
local serif = "Is this the\nreal life?\nIs this\njust fantasy?"
local reversed = serif :reverse() -- flip it
local pos = reversed :find( '\n' ) +1 -- count backwards
local sans_serif = serif :sub( 1, -pos ) -- strip it
print( sans_serif )
you can oneline it if you want, same results.
local str = "Is this the\nreal life?\nIs this\njust fantasy?"
print( str :sub( 1, -str :reverse() :find( '\n' ) -1 ) )
Is this the
real life?
Is this

Splitting pattern into multiple tables with gmatch

I am trying to gsplit my text into multiple tables using a pattern.
So this is my input.
\x10Hello\x0AWorld
This is what I expect in my output,
\x0A <- similar inputs will always be 4 chars long
{{'\x10', 'Hello'}, {'\x0A', 'World'}}
This is what I have tried so far.
local function splitIntoTable(input)
local output = {}
for code, text in (input):gmatch('(\\x%x+)(.*)') do
print(code .. ' ' .. text);
table.insert(output, { code, text })
end
return output
end
I made 2 regex groups in gmatch the first group is for the hex and the second group is for the text, I am not sure why this isn't working. The print statement never gets executed so the loop is never being used.
The pattern '\\x%x+' matches a literal backslash, an x, and a sequence of hex digits. It does not match the ASCII character generated by a hexadecimal escape such as '\x0A'.
You need to replace it with a character class in square brackets such as '[\x10\x0A]'. You will have to fill in the character class with whatever ASCII characters (or other bytes) you are expecting in that position in the match.
Unfortunately, this pattern will only match once in a string like '\x10Hello\x0AWorld'. The second part of the pattern also needs to be modified.
local function splitIntoTable(input)
local output = {}
for code, text in (input):gmatch('([\x10\x0A])(.*)') do
print(code .. ' ' .. text);
table.insert(output, { code, text })
end
return output
end

extract data from string in lua - SubStrings and Numbers

I'm trying to phrase a string for a hobby project and I'm self taught from code snips from this site and having a hard time working out this problem. I hope you guys can help.
I have a large string, containing many lines, and each line has a certain format.
I can get each line in the string using this code...
for line in string.gmatch(deckData,'[^\r\n]+') do
print(line) end
Each line looks something like this...
3x Rivendell Minstrel (The Hunt for Gollum)
What I am trying to do is make a table that looks something like this for the above line.
table = {}
table['The Hunt for Gollum'].card = 'Rivendell Minstrel'
table['The Hunt for Gollum'].count = 3
So my thinking was to extract everything inside the parentheses, then extract the numeric vale. Then delete the first 4 chars in the line, as it will always be '1x ', '2x ' or '3x '
I have tried a bunch of things.. like this...
word=str:match("%((%a+)%)")
but it errors if there are spaces...
my test code looks like this at the moment...
line = '3x Rivendell Minstrel (The Hunt for Gollum)'
num = line:gsub('%D+', '')
print(num) -- Prints "3"
card2Fetch = string.sub(line, 5)
print(card2Fetch) -- Prints "Rivendell Minstrel (The Hunt for Gollum)"
key = string.gsub(card2Fetch, "%s+", "") -- Remove all Spaces
key=key:match("%((%a+)%)") -- Fetch between ()s
print(key) -- Prints "TheHuntforGollum"
Any ideas how to get the "The Hunt for Gollum" text out of there including the spaces?
Try a single pattern capturing all fields:
x,y,z=line:match("(%d+)x%s+(.-)%s+%((.*)%)")
t = {}
t[z] = {}
t[z].card = y
t[z].count = x
The pattern reads: capture a run of digits before x, skip whitespace, capture everything until whitespace followed by open parenthesis, and finally capture everything until a close parenthesis.

How to combine two strings in Fortran

Here's an example in python3 of what I want to do in Fortran:
str1 = "Hello"
str2 = " World!"
print(str1 + str2)
# And then the result would be "Hello World!"
When I do:
print "(A)", str1, str2
It puts it on a separate line. If anyone knows how to help please answer.
The literal answer to string concatenation, using the // operator, is given in another answer. Note, particularly, that you likely want to TRIM the first argument.
But there is another interesting concept your question raises, and that is format reversion.
With the format (A) we have one format item. In the output list str1, str2 we have two output items. In a general output statement we apply each format item (with repeat counts) to a corresponding output item. So, str1 is processed with the first format item A, and a string appears.
Come the second output item str2 we've already used the single format item, reaching the end of the format item list. The result is that we see this format reversion: that is, we go back to the first item in the list. After, crucially, we start a new line.
So, if we just want to print those two items to one line (with no space or blank line between them) we could use (neglecting trimming for clarity)
print "(A)", str1//str2
or we could use a format which hasn't this reversion
print "(2A)", str1, str2
print "(A, A)", str1, str2
The first concatenates the two character variables to give one, longer, which is then printed as a single output item. The second prints both individually.
Coming to your particular example
character(12), parameter :: str1="Hello" ! Intentionally longer - trailing blanks
character(12), parameter :: str2=" World!"
print "(2A)", TRIM(str1), TRIM(str2)
end
will have output like
Hello World!
with that middle space because TRIM won't remove the leading space from str2. More widely, though we won't have the leading space there for us, and we want to add it in the output.
Naturally, concatenation still works (I'm back to assuming no-trimming)
character(*), parameter :: str1="Hello" ! No trailing blank
character(*), parameter :: str2="World!"
print "(A)", str1//" "//str2
end
but we can choose our format, using the X edit descriptor, to add a space
print "(2(A,1X))", str1, str2
print "(A,1X,A)", str1, str2
print "(2(A,:,1X))", str1, str2
where this final one has the useful colon edit descriptor (outside scope of this answer).
Probably close to what you want:
Concatenate two strings in Fortran
zz = trim(xx) // trim(yy)
More info
Bing
It looks like this is covered but another useful feature, if you want to print a lot of data on the same line is the following:
character(len=32),dimension(100) :: str
do i=1,100
write(*,fmt="(A)", advance='no') str(i)
end do
write(*,*) ! to go to the next line when you are done
This will print 100 characters on the same line because of advance='no'
You can use another variable to put those into it, and write those two strings in that new variable:
Program combineString
character(len=100) :: text1, text2, text
text1 = "Hello,"
text2 = "My name is X"
write(text,'(A6, X, A20)') text1, text2
write(*,*) text
End Program
And output is:
Hello, My name is X

Break strings into substrings based on delimiters, with empty substrings

I am using LUA to create a table within a table, and am running into an issue. I need to also populate the NIL values that appear, but can not seem to get it right.
String being manipulated:
PatID = '07-26-27~L73F11341687Per^^^SCI^SP~N7N558300000Acc^'
for word in PatID:gmatch("[^\~w]+") do table.insert(PatIDTable,word) end
local _, PatIDCount = string.gsub(PatID,"~","")
PatIDTableB = {}
for i=1, PatIDCount+1 do
PatIDTableB[i] = {}
end
for j=1, #PatIDTable do
for word in PatIDTable[j]:gmatch("[^\^]+") do
table.insert(PatIDTableB[j], word)
end
end
This currently produces this output:
table
[1]=table
[1]='07-26-27'
[2]=table
[1]='L73F11341687Per'
[2]='SCI'
[3]='SP'
[3]=table
[1]='N7N558300000Acc'
But I need it to produce:
table
[1]=table
[1]='07-26-27'
[2]=table
[1]='L73F11341687Per'
[2]=''
[3]=''
[4]='SCI'
[5]='SP'
[3]=table
[1]='N7N558300000Acc'
[2]=''
EDIT:
I think I may have done a bad job explaining what it is I am looking for. It is not necessarily that I want the karats to be considered "NIL" or "empty", but rather, that they signify that a new string is to be started.
They are, I guess for lack of a better explanation, position identifiers.
So, for example:
L73F11341687Per^^^SCI^SP
actually translates to:
1. L73F11341687Per
2.
3.
4. SCI
5. SP
If I were to have
L73F11341687Per^12ABC^^SCI^SP
Then the positions are:
1. L73F11341687Per
2. 12ABC
3.
4. SCI
5. SP
And in turn, the table would be:
table
[1]=table
[1]='07-26-27'
[2]=table
[1]='L73F11341687Per'
[2]='12ABC'
[3]=''
[4]='SCI'
[5]='SP'
[3]=table
[1]='N7N558300000Acc'
[2]=''
Hopefully this sheds a little more light on what I'm trying to do.
Now that we've cleared up what the question is about, here's the issue.
Your gmatch pattern will return all of the matching substrings in the given string. However, your gmatch pattern uses "+". That means "one or more", which therefore cannot match an empty string. If it encounters a ^ character, it just skips it.
But, if you just tried :gmatch("[^\^]*"), which allows empty matches, the problem is that it would effectively turn every ^ character into an empty match. Which is not what you want.
What you want is to eat the ^ at the end of a substring. But, if you try :gmatch("([^\^])\^"), you'll find that it won't return the last string. That's because the last string doesn't end with ^, so it isn't a valid match.
The closest you can get with gmatch is this pattern: "([^\^]*)\^?". This has the downside of putting an empty string at the end. However, you can just remove that easily enough, since one will always be placed there.
local s0 = '07-26-27~L73F11341687Per^^^SCI^SP~N7N558300000Acc^'
local tt = {}
for s1 in (s0..'~'):gmatch'(.-)~' do
local t = {}
for s2 in (s1..'^'):gmatch'(.-)^' do
table.insert(t, s2)
end
table.insert(tt, t)
end

Resources