Accessing external (DSPF) fields using arrays in RPGLE free - db2

In the old RPG III and the non-free RPGLE/RPG IV you could "rename" fields you get from either a record of a PF/LF or a record from a DSPF.
This lead to possibilities like grouping several lines of input (additional order text) into a array. So I didn't have to MOVEL or EVAL ottxt1 to the external described field x1txt1, ottxt2 to x1txt2 and so on.
I'd only had to rename the LF record and the DSPF record fields to the array-fields, read the record and shift them from the one array to the other and display my DSPF record
H DECEDIT('0,') DATEDIT(*DMY.) dftactgrp(*no)
Fsls001 cf e workstn
Fordtxtl0 if e k disk
D ot s 20a dim(6)
D x1 s 20a dim(6)
Iordtxtr
I ottxt1 ot(1)
I ottxt2 ot(2)
I ottxt3 ot(3)
I ottxt4 ot(4)
I ottxt5 ot(5)
I ottxt6 ot(6)
Isls00101
I x1txt1 x1(1)
I x1txt2 x1(2)
I x1txt3 x1(3)
I x1txt4 x1(4)
I x1txt5 x1(5)
I x1txt6 x1(6)
C k$or00 klist
C kfld otonbr
C kfld otopos
C eval otonbr = 2
C eval otopos = 2
C k$or00 chain ordtxtr
C if %found(ordtxtl0)
C eval x1 = ot
C endif
C
C exfmt sls00101
C
C move *on *inlr
But is this also possible in *FREE RPGLE? And if so, how?

You can define data structures containing the fields from the files, and overlay them with an array.
Replace your I specs and array definitions with these data structures. You don't have to specify anything besides the field names for the fields from the externally-described file.
dcl-ds otDs;
ottxt1;
ottxt2;
ottxt3;
ottxt4;
ottxt5;
ottxt6;
ot like(ottxt1) dim(6) pos(1);
end-ds;
dcl-ds x1Ds;
x1txt1;
x1txt2;
x1txt3;
x1txt4;
x1txt5;
x1txt6;
x1 like(x1txt1) dim(6) pos(1);
end-ds;

Related

Applying an operation on each element of nested list

I have a complex nested list (depth can be >2 also):
p:((`g;`d1`d2);(`r;enlist `e1);(`r;enlist `p1))
How to add an element to each element of the nested list but retaining the original structure; e.g. adding `h to each element of p to get the following :
((`g`h;(`d1`h;`d2`h));(`r`h;enlist `e1`h);(`r`h;enlist `p1`h))
I tried this but doesn't give what I want :
q)p,\:`h
((`g;`d1`d2;`h);(`r;enlist `e1;`h);(`r;enlist `p1;`h))
q)raze[p],\:`h
(`g`h;`d1`d2`h;`r`h;`e1`h;`r`h;`p1`h)
You can use .z.s to recursively go through the nested list and only append `h to lists of symbols:
q){$[0=type x;.z.s'[x];x,\:`h]}p
g h d1 h d2 h
`r`h ,`e1`h
`r`h ,`p1`h
For this function I have made the assumption that your nested lists will only contain symbols. It checks the type of the list, if it is not a mixed list then it appends `h to each element. If it is a mixed list then it passes each element of that list back into the function separately to check again.
Although not recursive (and so requires some knowledge about the shape of your nested list), a more conventional approach would be
q).[p;2#(::);,';`h]
g h d1 h d2 h
`r`h ,`e1`h
`r`h ,`p1`h
Though Thomas has already answered the question; In case you want to specify any other operation apart from append, you can use the following :
q)f:{`$ "_" sv string x,y}
q){[o;a;e] $[-11<>type e; .z.s [o;a] each e; o[e;a]] }[f;`h] each p
`g_h `d1_h`d2_h
`r_h ,`e1_h
`r_h ,`p1_h
or when f is assigned as append operation
q)f:{x,y}
q){[o;a;e] $[-11<>type e; .z.s [o;a] each e; o[e;a]] }[f;`h] each p
g h d1 h d2 h
`r`h ,`e1`h
`r`h ,`p1`h

Adding Columns in kdb based on other columns value

I have a column like this in dataframe named test:
Name Client
A P
B Q
C R
D S
E T
I need to to create a new column clienttype in the same dataframe with condition that, if Client = P or Q clienttype = first. If client type = R or S clienttype = second and so on. Can anyone tell how to do this in kdb? Thanks
Could this be solved with a dictionary?
q)update ClientType:(`P`Q`R`S!`first`first`second`second)Client from tab
Name Client ClientType
----------------------
A P first
B Q first
C R second
D S second
E T
Extension to this: You can also use vector conditionals ?[;;] for these types of problems. Unfortunately in this case in would result in many nested vector conditionals:
update Clienttype:?[Client in`P`Q;`first;?[Client in`R`S;`second;`third]]from tab
Name Client Clienttype
----------------------
A P first
B Q first
C R second
D S second
E T third
If you only had two possible options (e.g. first and second) this approach could prove useful.
You did not tell us the types of the columns in your table, but if all your entries are single letter, they are best represented by q char type:
q)show t:([]Name:"ABCDE";Client:"PQRST")
Name Client
-----------
A P
B Q
C R
D S
E T
Now, the Clienttype is best stored as a number which can be computed as
q)update Clienttype:1+(("i"$Client)-"i"$"P")div 2 from t
Name Client Clienttype
----------------------
A P 1
B Q 1
C R 2
D S 2
E T 3
or if you must use symbols,
q)types:`first`second`third`fourth
q)update Clienttype:types(("i"$Client)-"i"$"P")div 2 from t
Name Client Clienttype
----------------------
A P first
B Q first
C R second
D S second
E T third

How to format print statements with two vector variables?

I'd like to write several messages and tables on the same .txt file.
For example:
x=[23.9,10.9,8.9,14.2]
y=[9.83,8.04,7.47,8.32]
file=fopen('Results.txt','wt');
fprintf(file,'Results1\n');
fprintf(file,'%.2f %.2f\r\n',x,y);
fprintf(file,'Results2\n');
fclose(file);
I get this result as .txt:
Results1
23.90 10.90
8.90 14.20
9.83 8.04
7.47 8.32
Results2
But I should get this one:
Results1
23.90 9.83
10.90 8.04
8.90 7.47
14.20 8.32
Results2
Instead of fprintf(file,'%.2f %.2f\r\n',x,y);), I was trying to use:
ResultsTable2 = table(x,y);
writetable(file,ResultsTable2);
but didn't succeed. How to write the required .txt file?
Careful examination of your output shows that all the elements of x were printed before all the elements of y.
The documentation confirms that this is the expected behavior. Check out this example
A1 = [9.9, 9900];
A2 = [8.8, 7.7 ; ...
8800, 7700];
formatSpec = 'X is %4.2f meters or %8.3f mm\n';
fprintf(formatSpec,A1,A2)
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm
Even though the arguments to fprintf are in the order A1, A2. It first prints all the values from A1, and then it prints all the values from A2 going in single index order.
Therefore, if you want to alternate values from x and y during printing, you need to interleave them in a new variable. There are several possible ways to do so.
One example,
XY = reshape([x;y], 1, []);
Then everything should print as expected
fprintf(file, '%.2f %.2f\r\n', XY);
% or if you want to print to command window
% fprintf('%.2f %.2f\r\n', XY);
23.90 9.83
10.90 8.04
8.90 7.47
14.20 8.32
The correct answer for how to output data with fprintf is given by Cecilia: each argument will be iterated completely through in the order it appears in the argument list, so you have to combine the data into one matrix argument that will be iterated through column-wise to generate the desired output.
You also mentioned trying to use a table and the writetable function, so I though I'd add the correct way to do that in case you were curious:
ResultsTable2 = table(x(:), y(:)); % Pass data as column vectors
writetable(ResultsTable2, 'Results.txt', 'WriteVariableNames', false);

Calculator doesn't execute the operations

I am trying to build a calculator in abap. The requirements are:
Reads
two numbers (ex. 56.3 and 78.2)
a character from the following list: q, w, e, r, t
Displays the result of the operation specified by the character
Addition for character q
Subtraction for character w
Multiplication for character e
Division for character r
Exponent for character t
I have created a table with the operations that I am using in the calculator.
The problem is when I execute the program it only prints my last option (else) "the operation is not possible".
Here's the code I wrote:
REPORT Z_CALCULATOR_V2.
TABLES: ZOPERATII.
DATA result type p decimals 2.
DATA Q type c.
DATA W like Q.
DATA E like Q.
DATA R like Q.
DATA T like Q.
PARAMETERS Nr_1 type p decimals 2 OBLIGATORY.
PARAMETERS Nr_2 like Nr_1 OBLIGATORY.
PARAMETERS Operatie LIKE zoperatii-operatie OBLIGATORY.
if Operatie = Q.
result = Nr_1 + Nr_2.
elseif Operatie = W.
result = Nr_1 - Nr_2.
elseif Operatie = E.
elseif Operatie = R.
result = Nr_1 / Nr_2.
elseif Operatie = T.
result = Nr_1 ** Nr_2.
else.
WRITE 'The operation is not possible'.
endif.
write result.
if you change the data declarations to:
DATA Q type c value 'Q'.
DATA W type c value 'W'.
DATA E type c value 'E'.
DATA R type c value 'R'.
DATA T type c value 'T'.
the code should run as you expect. That said, you should read up on the documentation as suggested in the comments.

Matlab: regexp usage

I am going to start illustration using a code:
A = 'G1(General G1Airlines american G1Fungus )';
Using regexp (or any other function) in Matlab I want to distinctively locate: G1, G1A and G1F.
Currently if I try to do something as:
B = regexp( A, 'G1')
It is not able to distinguish G1 with the G1A and G1F i.e. I need to force the comparison to find me only case with G1 and ignore G1A and G1F.
However, when I am searching for G1A then it should still find me the location of G1A.
Can someone please help ?
Edit: Another case for A is:
A = 'R1George Service SmalR1Al C&I)';
And the expression this time I need to find is R1 and R1A instead.
Edit:
I have a giant array containing A's and another big vector containing G1, R1, etc I need to search for.
If you want to find 'G1' but not 'G1A' or 'G1F' you can use
>> B = regexp(A, 'G1[^AF]')
B =
1
This will find 'G1' and the ^ is used to specify that it should not match any characters contained with []. Then you could use
>> B = regexp(A, 'G1[AF]')
B =
12 32
to find both 'G1A' and 'G1F'.