Get value field dynamically from another table ABAP - structure

help me for report Abap.
I have 2 table.
Itab head
Z1
Z2
Z3
A
B
C
Itab obbligatory field
Fieldname
Z2
i should create a file with only fields of itab2(itab obbligatory field) but the value of itab head.
The file will be:
tab file
Z2
B
Itab2 say me which field of itab1 are obbligatory for create itab file.

This gives you an idea of what to do using ASSIGN for dynamic field selection.
LOOP AT itab_a INTO DATA(wa_itab_a). " your data tab
LOOP AT itab_b INTO DATA(wa_itab_b). " your obligatory field list
TRANSLATE wa_itab_b-fieldname TO UPPER CASE. " important if they are not already in uppercase
ASSIGN COMPONENT wa_itab_b-fieldname OF STRUCTURE wa_itab_a TO FIELD-SYMBOL(<fs_field>).
" ... in <FS_FIELD> you will have the values of the obligatory fields
" so you can concatenate, par example, to file line
CONCATENATE wa_file-line <fs_field> INTO wa_file-line SEPARATED BY c_separator.
ENDLOOP.
" append file line here, something like this:
APPEND wa_file TO itab_file.
CLEAR wa_file.
ENDLOOP.

Related

Aligning numeric values on left with WRITE

I'm creating a calculation table and want to align the numbers on the left under the '+'.
But somehow the first number in each column from the counter has some space before it.
How can I eliminate that space and align my table so that the left side is all in one row?
Code:
DATA: counter TYPE i,
counter2 TYPE i.
ULINE /(159).
WRITE: /1 sy-vline , '+', sy-vline.
DO 11 TIMES.
counter = sy-index - 1 .
WRITE: counter, sy-vline.
ENDDO.
ULINE /(159).
DO 11 TIMES.
counter = sy-index - 1 .
WRITE: /1 sy-vline , counter , sy-vline.
ULINE /(159).
ENDDO.
The spaces in front of the number are there because of the data type. Type i is an elementary data type and can have numbers from -2147483648 to 2147483647, which means it can be 11 characters long. Some data types have an output length that is variable, but that is not the case for i. You can see that if you click on it in your output, it should have a red outline 11 characters long.
But if you would rather have the spaces at the end of the number, then you can use 'CONVERSION_EXIT_ALPHA_OUTPUT'. But the "table outline" will still have to be just as big, since the number can have 11 characters.
DATA: counterc TYPE c LENGTH 11.
...
MOVE counter TO counterc.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = counterc
IMPORTING
output = counterc.
...
WRITE: ... counterc ...
Alternatively, the output of a table looks way better if you use SALV. Look here for example, to see how to output a table using SALV.

How to compare two fields in same column in internal table? ABAP

How to compare fields in same column in internal table? ABAP
Example to compare in column A:
col A | col B
A | B
A | A
A | A
B | B
B | B
I would first loop through the contents of your internal table and do my comparison between field 1 and field 2 within the loop.
The comparison is done on a row by row level.
If the condition is true, I would add my business logic within the IF statement.
LOOP at itab.
IF itab-col1 EQ itab-col2
"Business logic.
ENDIF.
ENDLOOP.
Would this suffice?
field-symbols: <ls_line> type (line structure of itab)
loop at itab assigning <ls_line>.
if <ls_line>-column_a NE <ls_line>-column_b.
write: / sy-tabix, <ls_line>-column_a, <ls_line>-column_b.
endif.
endloop.
sy-tabix will give the line number where there is a difference between the 2 columns.

Cleaning empty cells in internal table

I'm trying to clean the following empty cells marked in red from this internal table before I display it in an ALV.
If a cell is found to be blank, look for any cells underneath that have value and move up.
I am struggling to figure out what is the best way in code to perform this.
Any help would be great.
It is undoubtedly that something is wrong with your merging logic, however your task is quite interesting and this is one of the possible ways it can be solved.
I took your structure and made an assumption that none of the rows in your table is fully filled, i.e. either first three columns are filled (struct_left) or last three (struct_right). This is how I feel it from your screenshots.
REPORT z_sections.
TYPES:
BEGIN OF struct_left, " left structure
LEFTDAMAGED TYPE c LENGTH 1,
LEFTDAMAGEDDESC TYPE c LENGTH 3,
LEFTDAMAGEDDESCT TYPE c LENGTH 30,
END OF struct_left,
BEGIN OF struct_right, " right structure
RIGHTDAMAGED TYPE c LENGTH 1,
RIGHTDAMAGEDDESC TYPE c LENGTH 3,
RIGHTDAMAGEDDESCT TYPE c LENGTH 30,
END OF STRUCT_right.
TYPES BEGIN OF ty_table.
INCLUDE TYPE struct_left.
INCLUDE TYPE struct_right.
TYPES END OF ty_table.
DATA: lt_current_table TYPE TABLE OF ty_table INITIAL SIZE 100,
ls_current_table LIKE LINE OF lt_current_table,
i TYPE i.
FIELD-SYMBOLS: <fld> TYPE clike.
DATA: r_random TYPE REF TO cl_abap_random_packed,
seed TYPE i.
seed = cl_abap_random=>seed( ).
CALL METHOD cl_abap_random_packed=>create
EXPORTING
seed = seed
min = -999999999999999
max = 999999999999999
RECEIVING
prng = r_random.
DEFINE randomize. " filling row with random data
ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fld>.
<fld> = r_random->get_next( ).
&1 = &1 + 1.
ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fld>.
<fld> = r_random->get_next( ).
&1 = &1 + 1.
ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fld>.
<fld> = r_random->get_next( ).
END-OF-DEFINITION.
START-OF-SELECTION.
* filling table with random stuff
DO 100 TIMES.
CLEAR ls_current_table.
IF sy-index MOD 3 = 0.
i = 1.
randomize i ls_current_table.
ELSE.
i = 4.
randomize i ls_current_table.
ENDIF.
APPEND ls_current_table TO lt_current_table.
ENDDO.
DATA: ls_left TYPE struct_left,
ls_right TYPE struct_right.
DATA lt_new LIKE lt_current_table.
* collapsing table
LOOP AT lt_current_table ASSIGNING FIELD-SYMBOL(<fs_current>) WHERE leftdamaged IS NOT INITIAL.
DELETE lt_current_table WHERE leftdamaged IS INITIAL AND leftdamageddesc IS INITIAL AND leftdamageddesct IS INITIAL AND
rightdamaged IS INITIAL AND rightdamageddesc IS INITIAL AND rightdamageddesct IS INITIAL. " remove empty lines
MOVE-CORRESPONDING <fs_current> TO ls_left.
READ TABLE lt_current_table ASSIGNING FIELD-SYMBOL(<fs_right>) WITH KEY leftdamaged = ''.
IF <fs_right> IS ASSIGNED.
MOVE-CORRESPONDING <fs_right> TO ls_right.
CLEAR: <fs_right>.
ENDIF.
CLEAR: <fs_current>.
IF ls_left IS NOT INITIAL AND ls_right IS NOT INITIAL.
CLEAR: ls_current_table.
MOVE-CORRESPONDING ls_left TO ls_current_table.
MOVE-CORRESPONDING ls_right TO ls_current_table.
APPEND ls_current_table TO lt_new.
CLEAR: ls_left, ls_right.
ENDIF.
ENDLOOP.
You can sort the internal table and store it in a temp internal table, and swap them. For instance:
data: lt_itab_temp like table of lt_itab.
move lt_itab[] to lt_itab_temp[].
clear:lt_itab[],lt_itab.
sort lt_itab_temp descending by rightdamagedesc rightdamagedesct.
move lt_itab_temp[] to lt_itab[].
OR, you can loop through the fieldcatalog, set "no_display" or "no_out" field to 'X'.

How to split a series of number and compare it with another set of digit?

Say I have a no. 20101105, I need to compare it with a series of other nos. say 20110105 , 20090105 and find the nearest no. of it.
I don't want to compare it on the whole, I need to compare it each digit wise by parsing thru it and then see which is the closest.
Can someone suggest on how to do this in ABAP language?
In general You should mention some more information. For example, are the numbers really integers ? Then You can put them into an internal table and sorting all of them is the easiest solution to find any "nearest" number relating to an actual scanned. This is just like integers work in sort, they are sorted like numbers, my friend. But If You want it character-wise ( what really makes no sense, if the numbers are integers ) i give You some help with this character-comparison in a do-loop, taking smaller string-length as iterator-counter. I omitted the else, that's Your "homework". :-D
DATA:
lv_length1 TYPE i,
lv_length2 TYPE i,
lv_cnt TYPE i,
lv_teststr1 TYPE string VALUE '123456',
lv_teststr2 TYPE string VALUE '1235'.
lv_length1 = strlen( lv_teststr1 ).
lv_length2 = strlen( lv_teststr2 ).
IF lv_length1 GE lv_length2.
DO lv_length2 TIMES.
IF lv_teststr2+lv_cnt(1) NE lv_teststr1+lv_cnt(1).
BREAK-POINT.
ENDIF.
ADD 1 TO lv_cnt.
ENDDO.
ENDIF.
The counter variable is also the index of, in this case, the first not matching character. This gets the job done.
Coded and tested by me just right now.
I don't know if I understood but maybe this helps.
report znearest.
data lv_value(8) type n.
parameters p_value(8) type n. " ---------> The value
select-options s_values for lv_value. " -> The list
start-of-selection.
data: wa like line of s_values,
lv_dif(8) type n,
lv_nearest(8) type n,
lv_nearest_dif(8) type n,
lv_first type c.
loop at s_values into wa.
lv_dif = abs( p_value - wa-low ). " Calculate the difference
if lv_first is initial.
lv_nearest_dif = lv_dif.
lv_first = 'X'.
endif.
if lv_dif le lv_nearest_dif. " Compare the differences
lv_nearest = wa-low.
lv_nearest_dif = lv_dif.
endif.
endloop.
write: 'The nearest from', p_value, 'is', lv_nearest.
Hope it helps.

Vi: how to automatically insert spaces

I'm trying to write a nice feature for crazy people like me who like there lines to be perfectly aligned.
I often write some file in which the format is "key = value".
Since the key may contain an indeterminate number of character, one have to manually align the "=" symbols which is not cool.
Is there a way to tell vi "when someone type the equal character, then insert as spaces as necessary to go to the column 25, then write an the equal symbol"?
The second step will be to define a shortcut to apply this format to an entire file.
Any help would be appreciated.
Ben.
Map the behavior of = in Insert Mode.
Next code will add spaces until column 24 from current cursor position and will add an equal sign after it. If there were characters after cursor position (suppose in a middle of a word), those characters will be moved after column 25. Add it to your vimrc file and try.
"" If length of the line is more or equal to 24, add an equal sign at the end.
"" Otherwise insert spaces from current position of cursor until column 24
"" and an equal sign, moving characters after it.
function My_align()
let line_len = strlen( getline('.') )
if line_len >= 24
s/$/=/
return
endif
let col_pos = col('.')
exe 's/\%#\(.\|$\)/\=submatch(1) . printf( "%' . (24 - col_pos) . 's%s", " ", "=" )/'
endfunction
inoremap = <Esc>:call My_align()<CR>A
For second step, use the multiple repeats command, check for an equal sign and insert spaces until column 25 just before it. Won't work if equal sign is after column 25 before executing it, but you get the idea.
:g/=/exe 's/=/\=printf( "%' . ( 24 - stridx( getline('.'), "=" ) ) . 's", " " ) . submatch(0)/'