Ab initio to read comma delimited values - dml

What should be my DML for input file which has data like 1,2,34,43,66
The problem which i am facing when i put the below DML
record
decimal(",") val;
end;
is that the last number is not getting read properly.
Is there any way to read this by only using Input file component.

you have described only ONE numeric field, but you have 5 fields
"new line" (new record) also should be described
are you using BRE ? you could create DML in BRE

You can try the following DML I guess it should work:
record
decimal(",") val1;
decimal(",") val2;
decimal(",") val3;
decimal(",") val4;
decimal(",") val5;
string("\n") newline = NULL;
end

I presume you want to read each number as a record. The problem is: data is not compatible with your desire, last number is not delimited by a ",".
If you can't change the data, you could read all the input in a single record, using:
record
string("\n") val;
end
then use a normalize component to split the record:
out :: length(in) =
begin
out :: length_of(string_split(in.val, ","));
end;
out :: normalize(in, index) =
begin
let string(";")[5] my_vec;
my_vec = string_split(in.val, ",");
out.val :: decimal_lpad( my_vec[index], 5 );
end;
(there are probably better solutions, I am still not an expert, but there wasn't an answer yet)

Related

navision- looping through line nos in sales line

i am trying to retrieve the difference of qty and qts shipped for a sales order.
i am doing this through code.
setting range on sales line table fields and using findset does loop through all lines properly but while printing it gives the difference frm the last line.
clearing the variable is also not working.
i am new to NAV 2013 so not able to find out how to loop through all this lines so that it displays result properly.i tried using findfirst inside the if loop but no success.
You need to add to "Value", not overwrite it. (use +=, not :=)
CLEAR(Value);
SalesLine.RESET;
SalesLine.SETRANGE(SalesLine."Document No.","No.");
IF SalesLine.FINDSET THEN REPEAT
Value += SalesLine.Quantity - SalesLine."Quantity Shipped";
//MESSAGE('%1',Value);
UNTIL SalesLine.NEXT =0;
Try this code
local procedure DailyCalc(DailyLotNumber: Query DailyLotNumber): Decimal
var
SalesLine: Record "Sales Line";
NewRec: Decimal;
begin
SalesLine.SetRange("Document No.", DailyLotNumber.No_);
SalesLine.SetRange("Document Type", DailyLotNumber.Document_Type);
if SalesLine.FindSet() then begin
repeat
repeat
NewRec += (SalesLine.Quantity - SalesLine."Quantity Shipped") * SalesLine."Unit Price";
until SalesLine.Next() = 0;
exit(NewRec);
until SalesLine.Next() = 0;
end;
end;

How to index a table automatically and loop it in ipairs while keeping all data?

Table:
localization_strings = {
string_1 = "Text Here",
string_2 = "Some More Text Here",
string_3 = "More Text"
}
This is obviously not the whole table, but just a small sample. The real table is over 500+ lines. The reason I don't just redo the table is because other functions reference it and I don't have access to those files to fix them, so I have to find a work around. Also, because it would quite tedious work and can cause problems with other codes.
I have made 2 attempts at solving this problem, but I can only get one of the values I want (incorrect terminology, I think) and I need both as 1 is display text and 1 is data for a function call.
Attempts:
-- Attempt #1
-- Gives me the string_#'s but not the "Text"...which I need, as I want to display the text via another function
LocalizationUnorderedOpts = {}
LocalizationOpts = {}
for n,unordered_names in pairs(localization_strings) do
if (unordered_names) then
table.insert( LocalizationUnorderedOpts, n)
end
end
io.write(tostring(LocalizationUnorderedOpts) .. "\n")
table.sort(LocalizationUnorderedOpts)
for i,n in ipairs(LocalizationUnorderedOpts) do
if (n) then
io.write(tostring(i))
table.insert( LocalizationOpts, { text = tostring(LocalizationUnorderedOpts[i]), callback = function_pointer_does_not_matter, data = i } )
end
end
-- Attempt #2
-- Gives me the "Text" but not the string_#'s...which I need to as data to the callback to another function (via function pointer)
LocalizationUnorderedOpts = {}
LocalizationOpts = {}
for n,unordered_names in pairs(localization_strings) do
if (unordered_names) then
table.insert( LocalizationUnorderedOpts, localization_strings[n])
end
end
io.write(tostring(LocalizationUnorderedOpts) .. "\n")
table.sort(LocalizationUnorderedOpts)
for i,n in ipairs(LocalizationUnorderedOpts) do
if (n) then
io.write(tostring(i))
table.insert( LocalizationOpts, { text = tostring(LocalizationUnorderedOpts[i]), callback = function_pointer_does_not_matter, data = i } )
end
end
If I understand it correctly, you need to sort the non-array table. Your first attempt has done most of the work: build another table, which has the values the same as the keys in the original table.
What's left is how to get the original values like "Text Here", for that you need to index the original table:
for k, v in ipairs(LocalizationUnorderedOpts) do
print(v) --original key
print(localization_strings[v]) --original value
end

Query to fetch data between two characters in informix

I have a value in informix which is like this :
value AMOUNT: <15000000.00> USD
I need to fetch 15000000.00 afrom the above.
I am using this query to fetch the data between <> as workaround
select substring (value[15,40]
from 1 for length (value[15,40]) -5 )
from tablename p where value like 'AMOUNT%';
But, this is not generic as the lenght may vary.
Please help me with a generic query for this, fetch the data between <>.
The database I am using is Informix version 9.4.
It's a diabolical problem, created by whoever chose to break one of the fundamental rules of database design: that the content of a column should be a single, indivisible value.
The best solution would be to modify the table to contain a value_descr = "AMOUNT", a value = 15000000.00, and a value_type = "USD", and ensure that the incoming data is stored in that fashion. Easier said than done, I know.
Failing that, you'll have to write a UDR that parses the string and returns the numeric portion of it. This would be feasible in SPL, but probably very slow. Something along the lines of:
CREATE PROCEDURE extract_value (inp VARCHAR(255)) RETURNING DECIMAL;
DEFINE s SMALLINT;
DEFINE l SMALLINT;
DEFINE i SMALLINT;
FOR i = 1 TO LENGTH(inp)
IF SUBSTR(inp, i, 1) = "<" THEN
LET s = i + 1;
ELIF SUBSTR(inp, i, 1) = ">" THEN
LET l = i - s - 1;
RETURN SUBSTR(inp, s, l)::DECIMAL;
END IF;
END FOR;
RETURN NULL::DECIMAL; -- could not parse out number
END PROCEDURE;
... which you would execute thus:
SELECT extract_value(p.value)
FROM tablename AS p
WHERE p.value LIKE 'AMOUNT%'
NB: that procedure compiles and produces output in my limited testing on version 11.5. There is no validation done to ensure the string between the <> parses as a number. I don't have an instance of 9.4 handy, but I haven't used any features not available in 9.4 TTBOMK.

Pascal runtime error (range overrun) while reading array from file

I am trying to write a program to read a long list of book(1000 books),isbn etc
but when the program runs, it shows range overrun
the format of txt is
1
1234567890
ABC book
peter
20
2
1234567896
...
the code is:
const maxbk=1000;
type bookrecord = record
book_no:string;
isbn:string;
book_name:string;
author:string;
borrowed:string;
end;
var booklist : array[1..maxbk] of bookrecord;totalbook:integer;
procedure readbooklist(var bklist:array of bookrecord;var totalbk:integer);
var f:text;temp:string;code:integer;
begin
totalbk:=0;
assign(f,'bklist.txt');
reset(f);
while not eof(f) do
begin
readln(f,bklist[totalbk+1].book_no);
readln(f,bklist[totalbk+1].isbn);
readln(f,bklist[totalbk+1].book_name);
readln(f,bklist[totalbk+1].author);
readln(f,bklist[totalbk+1].borrowed);
totalbk:=totalbk+1;
end;
close(f);
writeln('read file done');
end;
begin
readbooklist(booklist,totalbook);
end.
who can help to fix the problem??
I think the problem is in your handling of the array parameter. Try this (highlighted in bold are the changes I've added):
const maxbk=1000;
type bookrecord = record
book_no:string;
isbn:string;
book_name:string;
author:string;
borrowed:string;
end;
var booklist : array[1..maxbk] of bookrecord; totalbook:integer;
procedure readbooklist(var bklist:array of bookrecord;var totalbk:integer);
var f:text;temp:string;code:integer;
begin
totalbk:=Low(bklist);
assign(f,'bklist.txt');
reset(f);
while not eof(f) do
begin
readln(f,bklist[totalbk].book_no);
readln(f,bklist[totalbk].isbn);
readln(f,bklist[totalbk].book_name);
readln(f,bklist[totalbk].author);
readln(f,bklist[totalbk].borrowed);
totalbk:=totalbk+1;
end;
totalbk := totalbk - Low(bklist);
close(f);
writeln('read file done');
end;
begin
readbooklist(booklist,totalbook);
end.
Also, a few choice spaces would help with readability (like a space after each comma and around assignment operators).
Note, too, that your code (and the changed code I'm providing) don't check for incomplete records in your input text file or properly check for blank lines, etc (e.g., invalid book_no values). You should attempt to add some code which makes it a little more resilient to problems in the input file. As others have pointed out, there are probably better ways to structure the input and read it as well.

Update after a table field change

I have a problem with a old Delphi system, this system insert data into a SQL Server table.
After 10 years change a field of the table from 100 to 255 chars long.
The system select all the registris of the table, and put them on an other table after a transformation. That works fine.
The problems are when the system update a field.
That show me the error
EDBEngineError with message 'Couldn't perform the edit because another user changed the record.
sConsulta:='SELECT * FROM cuentas WHERE (WALL= 2) AND (SEND_DATE = '01/01/1970')';
m_oQryLeg.Close;
m_oQryLeg.SQL.Clear;
m_oQryLeg.SQL.Add(sConsulta);
m_oQryLeg.Open;
m_oTblNov.Close;
m_oTblNov.TableName:='des_table';
m_oTblNov.Open;
with m_oTblNov do
begin
while (not m_oQryLeg.EOF) do
begin
Insert;
FieldbyName('COD_HOME').AsString:= m_oQryLeg.FieldByName('USR_HOME').AsString;
(...)
Post;
m_oQryLeg.Edit;
m_oQryLeg.FieldByName('SEND_DATE').AsDateTime:= Date; //<-- HERE THE ERROR
m_oQryLeg.Post;
m_oQryLeg.First;
m_oQryLeg.MoveBy(i);
inc(i);
end;
end;
m_oTblNov.Close;
m_oQryLeg.Close;
UpdateMode: upWhereAll
cuentas table:
NUM_SOL nvarchar 6 *PK
WALL tinyint 1
SEND_DATE smalldatetime 4
OBS_CRED nvarchar 255
FLCC real 4
STREET nvarchar 30
You're trying to update a query you're using on a table you're editing at the same time, and it's not going to work.
Since you know you're going to insert every row from the query into m_oTblNov, why not just do it like this instead?
sConsultaSELECT :='SELECT * FROM cuentas';
sConsultaUPDATE := 'UPDATE cuentas SET Send_Date = :New_Date';
// Separate WHERE so you can use it twice. Note the leading space
// between the first ' and WHERE.
sWhere := ' WHERE (WALL = 2) and (SEND_DATE = ''01/01/1970'')';
m_oQryLeg.Close;
m_oQryLeg.SQL.Text := sConsulta + sWhere;
m_oQryLeg.Open;
m_oTblNov.Close;
m_oTblNov.TableName:='des_table';
m_oTblNov.Open;
with m_oTblNov do
begin
while (not m_oQryLeg.EOF) do
begin
Insert;
FieldbyName('COD_HOME').AsString:= m_oQryLeg.FieldByName('USR_HOME').AsString;
(...)
Post;
// Don't run these any more. See below.
// m_oQryLeg.Edit;
// m_oQryLeg.FieldByName('SEND_DATE').AsDateTime:= Date; //<-- HERE THE ERROR
// m_oQryLeg.Post;
m_oQryLeg.First;
m_oQryLeg.MoveBy(i);
inc(i);
end;
end;
m_oTblNov.Close;
m_oQryLeg.Close;
m_oQryLeg.SQL.Text := sConsultaUPDATE + sWHERE;
m_oQryLeg.ParamByName('New_Date').AsDateTime := Date;
try
m_oQryLeg.ExecSQL;
finally
m_oQryLeg.Close;
end;
The problem here is that your object is trying to refresh the row from the database to make sure nothing has changed and more than likely the object truncated or rounded some value that is causing the refresh to return no rows.
This could be caused by either a float value being truncated or some other value being off.
if you don't/can't change that column to fix this issue I would advise changing to either upWhereChanged or upWhereKeyOnly.
Given that dates are treated as doubles in most windows databases, I would think that upWhereKeyOnly would be best.
EDIT:
After looking at your table, it may have to do with the fact you are using a single based smalldatetime. Delphi treats all DateTime data as a double and the conversion back and forth may be causing small rounding issues.

Resources