04-19-2006 12:17 PM
Hello friends
Can someone give me an example code in which sorted tables and hashed tables have been used.
Regards
04-19-2006 1:08 PM
hi Surya,
Sorted tables
This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.
REPORT ZREPORT_SORTED1.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
CLEAR LINE.
READ TABLE ITAB WITH TABLE KEY COL1 = 3
INTO LINE TRANSPORTING COL2.
WRITE: 'SY-SUBRC =', SY-SUBRC,
/ 'SY-TABIX =', SY-TABIX.
SKIP.
WRITE: / LINE-COL1, LINE-COL2.
The output is:
SY-SUBRC = 0
SY-TABIX = 3
0 9
REPORT ZREPORT_SORTED2.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
READ TABLE ITAB WITH KEY COL2 = 16 TRANSPORTING NO FIELDS.
WRITE: 'SY-SUBRC =', SY-SUBRC,
/ 'SY-TABIX =', SY-TABIX.
The output is:
SY-SUBRC = 0
SY-TABIX = 4
Hashed tables
This is the most appropriate type for any table where the main operation is key access. You cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries. Like database tables, hashed tables always have a unique key. Hashed tables are useful if you want to construct and use an internal table which resembles a database table or for processing large amounts of data.
REPORT ZREPORT_HASHED1.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
LINE-COL1 = 2. LINE-COL2 = 3.
READ TABLE ITAB FROM LINE INTO LINE COMPARING COL2.
WRITE: 'SY-SUBRC =', SY-SUBRC.
SKIP.
WRITE: / LINE-COL1, LINE-COL2.
The output is:
SY-SUBRC = 2
2 4
REPORT ZREPORT_HASHED2.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.
FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING <FS>.
<FS>-COL2 = 100.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.
The output is:
1 1
2 100
3 9
4 16
Regards
vijay
04-19-2006 12:20 PM
04-19-2006 12:49 PM
hi,
try that sample:
REPORT zforum34 LINE-SIZE 255.
PARAMETERS p_ktopl LIKE t001-ktopl.
PARAMETERS p_bukrs LIKE t001-bukrs.
PARAMETERS p_konto LIKE skb1-saknr.
DATA: t1 TYPE i,
t2 TYPE i,
tmin TYPE i.
DATA stab TYPE SORTED TABLE OF skb1 WITH UNIQUE KEY bukrs saknr.
DATA htab TYPE HASHED TABLE OF skb1 WITH UNIQUE KEY bukrs saknr.
DATA wa TYPE skb1.
SELECT * FROM skb1 INTO TABLE stab.
write: /'cnt:', sy-dbcnt.
uline.
htab = stab.
GET RUN TIME FIELD t1.
READ TABLE stab INTO wa WITH TABLE KEY bukrs = p_bukrs
saknr = p_konto.
GET RUN TIME FIELD t2.
tmin = t2 - t1.
WRITE:/ 'sorted table :', tmin, 'microseconds'.
ULINE.
CLEAR: t1, t2, tmin.
FREE stab.
GET RUN TIME FIELD t1.
READ TABLE htab INTO wa WITH TABLE KEY bukrs = p_bukrs
saknr = p_konto.
GET RUN TIME FIELD t2.
tmin = t2 - t1.
WRITE:/ 'hashed table :', tmin, 'microseconds'.
Andreas
04-19-2006 1:08 PM
hi Surya,
Sorted tables
This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.
REPORT ZREPORT_SORTED1.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
CLEAR LINE.
READ TABLE ITAB WITH TABLE KEY COL1 = 3
INTO LINE TRANSPORTING COL2.
WRITE: 'SY-SUBRC =', SY-SUBRC,
/ 'SY-TABIX =', SY-TABIX.
SKIP.
WRITE: / LINE-COL1, LINE-COL2.
The output is:
SY-SUBRC = 0
SY-TABIX = 3
0 9
REPORT ZREPORT_SORTED2.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
READ TABLE ITAB WITH KEY COL2 = 16 TRANSPORTING NO FIELDS.
WRITE: 'SY-SUBRC =', SY-SUBRC,
/ 'SY-TABIX =', SY-TABIX.
The output is:
SY-SUBRC = 0
SY-TABIX = 4
Hashed tables
This is the most appropriate type for any table where the main operation is key access. You cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries. Like database tables, hashed tables always have a unique key. Hashed tables are useful if you want to construct and use an internal table which resembles a database table or for processing large amounts of data.
REPORT ZREPORT_HASHED1.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
LINE-COL1 = 2. LINE-COL2 = 3.
READ TABLE ITAB FROM LINE INTO LINE COMPARING COL2.
WRITE: 'SY-SUBRC =', SY-SUBRC.
SKIP.
WRITE: / LINE-COL1, LINE-COL2.
The output is:
SY-SUBRC = 2
2 4
REPORT ZREPORT_HASHED2.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.
FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.
READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING <FS>.
<FS>-COL2 = 100.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.
The output is:
1 1
2 100
3 9
4 16
Regards
vijay