Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

READ TABLE on REF TO DATA Table

Former Member
0 Likes
12,794

Hi Folks!

And again I got a little question abount accessing dynamically created data structures 😃

I got a table type called zzcca_t_data which has a REF TO DATA as rowtype.

I am creating the type of each row in runtime, because it depends on users input.

I was wondering, if there is a way to random access a certain row in that table by using a key, like:

READ TABLE ..... WITH KEY = ...

I am afraid, that there is no standard way to do that, maybe some special object components etc?

My idea is to make sure, that the table is sorted by a certain criteria and the implement a binary search on it using an index and access the table using

READ TABLE ..... INDEX ...

Is there a better way to do that?

Thanks a lot in advance!!

Best regards!

Kamil

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
6,131

Hi,

You probably deref it to a field-symbol.



DATA: dref TYPE REF TO data,
CREATE DATA dref TYPE vbak.

assign dref->* to <fs>  "<fs> points to internal table

Now you can read this table using the following syntax.


data key(20) type c.
key = 'VBELN'   " for example

read table <fs> with key (key) = '0000000001'.

regards,

Advait

7 REPLIES 7
Read only

Former Member
0 Likes
6,131

Have you tried "FROM wa" instead of "WITH KEY ..."? But even "FROM wa" needs to know the key of the table, so this might not work...

Otherwise the form "WHERE table_key CS ..." might work...

But the above solutions are only tricks. I suggest that you build a new table where you store only the search key AND the tabix to the original table. Even if you don't know the key you should be able to use "WITH KEY table_line = ..." (note the equality operator) and then use the tabix to access the big table.

Read only

MarcinPciak
Active Contributor
0 Likes
6,131

I got a table type called zzcca_t_data which has a REF TO DATA as rowtype.

As each of your table row can only store reference of some data objects you can't read this table with read table ... with key.... You simply don't know what will be the address inside a row.

I suggest to rebuild your table adding first field as a key field


types: begin of t_tab, 
           key_field type ...,
           data type ref to data,
          end of t_tab.

types: tt_tab type table of t_tab with key key_field.

Then you can access in with a key. Otherwise you can't even sort such table.

Regards

Marcin

Read only

naimesh_patel
Active Contributor
6,131

You need to assign the data to the Field Symbol (type any table) to do any operation like: READ, LOOP etc.


  DATA: L_REF    TYPE REF TO DATA.

  FIELD-SYMBOLS: <LT_TEXT>     TYPE ANY TABLE,
                 <LA_TEXT>     TYPE ANY.

  ASSIGN L_REF->* TO <LT_TEXT>.

      READ TABLE <LT_TEXT> ASSIGNING <LA_TEXT> INDEX 1.

Regards,

Naimesh Patel

Read only

0 Likes
6,131

This cant work, check attached pic.2023-09-25-11-43-07-window.jpg

Read only

Former Member
0 Likes
6,132

Hi,

You probably deref it to a field-symbol.



DATA: dref TYPE REF TO data,
CREATE DATA dref TYPE vbak.

assign dref->* to <fs>  "<fs> points to internal table

Now you can read this table using the following syntax.


data key(20) type c.
key = 'VBELN'   " for example

read table <fs> with key (key) = '0000000001'.

regards,

Advait

Read only

0 Likes
6,131

EDITED:

Problem solved!! 😃

The Problem was that I haven created the table with a certain type but I created every row on its own. Now I created the data reference with a certain table type and now it works the way below.

Thank you very much!!

Best regards from Vienna!

Kamil

-


Hi all!!

Thank you very much for your helpful replies.

I tried to user the way of Advait Gode with



DATA: dref TYPE REF TO data,
CREATE DATA dref TYPE zzcca_t_data.
 
field-symbols:
<fs1> type zzcca_t_data, "Tabletype with REF TO DATA as ROWTYPE
<fs2> type any.

assign dref->* to <fs1> .

data key(20) type c.
key = 'POSNR'   " the structire zzccbo_belpos
 
read table <fs> with key (key) = '00001'. "NUMC5

Unfortunately I get a pretty shortdump "ITAB_ILLEGAL_COMPONENT" because it says that there is no element POSNR in <fs1>.

=/

Thanks in advance!!

Best regards,

Kamil

Edited by: Kamil Kubicki on Jan 30, 2009 11:35 AM

Read only

0 Likes
6,131

Hi Kamil,

key = 'POSNR' " the structire zzccbo_belpos

I am not sure what is structure zzccbo_belpos but here all you need is row type with POSNR field as key field.

What you wrote is interpreted like:


"create a pointer to a table (typed zzcca_t_data) 
CREATE DATA dref TYPE zzcca_t_data.


"declare a field symbol which is able to get the content of a table under address stored by pointer
field-symbols:
<fs1> type zzcca_t_data, 


"now poiter still has table address by also field symbol allows us to get table content (as it doesn't have name)
assign dref->* to <fs1> .


read table <fs> with key (key) = '00001'. "NUMC5
"now read table content (which is get throught field symbol) where key field is POSNR
"this is wrong because your table seems not to have this field, you said it is of row type ref to data so it is type like this

types: begin of zzcca_t_data,
            some_ref_field type ref to data,
          end of zzcca_t_data.

"and should be 

types: begin of zzcca_t_data,
            posnr type  numc5,        "this field is must exist otherwise you can't read this table supplyijng this field as key field
            some_ref_field type ref to data,   
           end of zzcca_t_data.

"I gusess what you did is that you replaced 

types: zzcca_t_data type table of ref to data.

"...with above 1st declaration, but these statements are excatly the same. In second case you only use
"field name to address table row but no difference in logic (still you can't read it with key access as there is no such)

"what I suggest you is either add POSNR field to a table type (as above) or read it with index access as my predeccesors said

Hope this claryfies you bug

Marcin

PS: Advait's code is correct because he is typing his table with VBAK so it already has key fields inside, therefore he is later able to access that table with particular key

Edited by: Marcin Pciak on Jan 30, 2009 2:32 PM