‎2008 Feb 22 6:48 AM
Hi all,
I am a total newbie in ABAP.
I need to process an array in ABAP.
If it was in .NET C#, it will be like this:
String strArr = new String[5] { "A", "B", "C", "D", "E" };
int index = -1;
for (int i=0; i<strArr.length; i++)
{
if (myData.equals(strArr<i>))
{
index = i;
break;
}
}
Can someone please convert the above code into ABAP?
Urgent. Please help. <REMOVED BY MODERATOR>
THank you.
Edited by: Alvaro Tejada Galindo on Feb 22, 2008 5:37 PM
‎2008 Feb 22 6:57 AM
Hi,
There is no concept of arrays in ABAP, we use internal tables to hold application data. U can use access internal tables with index.
Read table itab index l_index this is same as u do in case of arrrays a(l_index)
Instead use an Internal table to
populate the values and read them ..
say your Internal table has a field F1 ..
itab-f1 = 10.
append itab.
clear itab.
itab-f1 = 20.
append itab.
clear itab.
itab-f1 = 30.
append itab.
clear itab.
Now internal table has 3 values ...
use loop ... endloop to get the values ..
loop at itab.
write :/ itab-f1.
endloop.
Also you can do this way:
IT IS POSSIBLE TO DECLARE THE ONE -DIMENSIONAL ARRAY IN ABAP BY USING INTERNAL TABLES.
EX.)
DATA: BEGIN OF IT1 OCCURS 10,
VAR1 TYPE I,
END OF IT1.
IT1-VAR1 = 10.
APPEND IT1.
IT1-VAR1 = 20.
APPEND IT1.
IT1-VAR1 = 30.
APPEND IT1.
LOOP AT IT1.
WRITE:/ IT1-VAR1 COLOR 5.
END LOOP.
<REMOVED BY MODERATOR>
Cheers,
Chandra Sekhar.
Edited by: Alvaro Tejada Galindo on Feb 22, 2008 5:38 PM
‎2008 Feb 23 1:33 AM
the previous post is principally right, but uses a form of internal table (internal table with header line) that is depreciated. as a matter of fact it is even forbidden in an ABAP OO context.
This is the closest you can get to an array:
data: itab type standard table of i, "standard table of integer -> array of integer
var type i. "variable of type integer
i = 1.
append i to itab.
i = 2.
append i to itab
Now you have the two values in your "array". you have to ways of accessing in a very perfomant way using field symbols.
a) directly by index
data: itab type standard table of i, "standard table of integer -> array of integer
var type i. "variable of type integer
field-symbols:
<fs> type i.
read table itab index 1 assigning <fs>.
b) looping at the whole table
data: itab type standard table of i, "standard table of integer -> array of integer
var type i. "variable of type integer
field-symbols:
<fs> type i.
loop at table itab assigning <fs>.
"do something
endloop.