Binary Search in Dynamic Read

Here is the code
      read table <fs_it_crv> assigning <fs_wa_crv> with key
                                  ('objty') = wa_plfh-objty
                                  ('objid') = wa_plfh-objid
                                  binary search.
If i use binary search system giving error while activating as
You cannot use explicit or implicit index operations on tables with
types "HASHED TABLE" or "ANY TABLE". "" has the type "ANY
TABLE". It is possible that .
How could i use Binary search in dynamic read

Hello,
how do you create your internal table "<FS_IT_CRV>" ?
i found this thread [here|Need help regarding Dynamic Read Table statement;.
You can try the code lines of  David Klotz.
If you insert a sort statement before read and change read adding binary search it might work.
SORT <gt_itab> BY (ge_key_field1) (ge_key_field2) (ge_key_field3) .
* Finally, perform the search
  READ TABLE <gt_itab> ASSIGNING <gs_result>
          WITH KEY (ge_key_field1) = pa_cond1
                   (ge_key_field2) = pa_cond2
                   (ge_key_field3) = pa_cond3 BINARY SEARCH.
I tried the following code in my own program and it works:
SORT <wlf_t_data> BY (c_trkorr).
  read table <wlf_t_data> assigning <wlf_header> with key
         (c_trkorr) = 'DRR' binary search.
Where   <wlf_t_data> is a dynamic table created by method create_dynamic_table of class cl_alv_table_create.
Cordialement,
Chaouki

Similar Messages

  • Binary search option with Read statement

    Hi,
    There are any chances that the Read statement with Binary search option to fail, even though the key exists??
    Here the itab is sorted in descending order. and then some duplicates are removed using the delete adjacent statement.
    Regards,
    Sandhip.

    Hi,
    Here is an example:
    sort itab1 by a b c.
    loop at itab2 into wa_itab2.
      read table itab1 into wa_itab1
                       with key a = wa_itab2-a
                                b = wa_itab2-b
                       c = wa_itab2-c
                           binary search.
      if sy-subrc = 0.
        wa_output-a = wa_itab1-a.
      endif.
    endloop.
    Another alternative is to use sorted tables.
    Hope it helps...
    P.S. Please award points if it helps...

  • Read table and binary search

    Hi all,
    I have a simple query regarding read int_tab with binary search.
    Why reading  internal table with binary search fails if it is sorted in descending order table must be sorted in ascending order?
    I check fo the algorithm of binary search, it does not talk about sort order. As far as my understanding goes binary search only require sorted table but while reading table in SAP it has to be sorted in ascending order!!

    By default binary search assumes that the sort order is ASCENDING.
    If you sort the list in descending and then try to binary search your quires will fail. Look at an example:
    Let the descending order internal table as:
    Field1      Field2
    Sam        50000
    John       34786
    Boob      54321
    Alice       12345
    When you do binary search with key = 'Sam' then it will directly go to 2nd and 3rd records for comparision. The binary search algorithm compares 'Sam' with 'John' and it concludes that 'Sam' is greater than 'John' and it will continue to look downward into the internal table. And when it reaches the end of the internal table then the value of SY-TABIX = 5 and SY-SUBRC = 8 (Key is greater than the all).
    And if you do binary search with key = 'Alice' then the binary search algorithm compares 'Alice' with 'John' and it concludes that 'Alice' is lower than 'John' and it will continue to look upward into the internal table. And when it reaches above the first record in internal table then the value of SY-TABIX = 1 and SY-SUBRC = 4 (points to the next largest entry).
    The only correct result you will get is when you execute statement with key='John' (In this particular case) . SY-TABIX = 2 and SY-SUBRC = 0. I think you got this binary search algorithm.

  • "Binary search" in READ

    Hi frnds,
      I sumtyms  dont get the desired result while using the
    "BINARY SEARCH" addition with the "READ" statement.
    y is that? is there any rules to be followed to use binary search option in read statement.
    Regards,
    Madan...

    Hi
    Please go thru this.
    Binary Search in Standard Tables
    If you read entries from standard tables using a key other than the default key, you
    can use a binary search instead of the normal linear search. To do this, include the addition
    BINARY SEARCH in the corresponding READ statements.
    READ TABLE <itab> WITH KEY = <f> <result> BINARY SEARCH.
    and
    READ TABLE <itab> WITH KEY <k1> = <f1> ... <kn> = <fn> <result>
    BINARY SEARCH.
    The standard table must be sorted in ascending order by the specified search key. The BINARY
    SEARCH addition means that you can access an entry in a standard table by its key as quickly
    as you would be able to in a sorted table.
    Example
    DATA: BEGIN OF LINE,
    COL1 TYPE I,
    COL2 TYPE I,
    END OF LINE.
    DATA ITAB LIKE STANDARD TABLE OF LINE.
    DO 4 TIMES.
    LINE-COL1 = SY-INDEX.
    LINE-COL2 = SY-INDEX ** 2.
    APPEND LINE TO ITAB.
    ENDDO.
    SORT ITAB BY COL2.
    READ TABLE ITAB WITH KEY COL2 = 16 INTO LINE BINARY SEARCH.
    WRITE: 'SY-SUBRC =', SY-SUBRC.
    The output is:
    SY-SUBRC = 0
    The program fills a standard table with a list of square numbers and sorts them into
    ascending order by field COL2. The READ statement uses a binary search to look
    for and find the line in the table where COL2 has the value 16.
    http://help.sap.com/saphelp_nw2004s/helpdata/en/fc/eb35f8358411d1829f0000e829fbfe/content.htm
    Thanks
    Shiva

  • Replacemnt for read table with key binary search

    as read table with
    READ TABLE gt_INT_CURR_VALUE into gs_int_curr_value
               WITH KEY gs_FS_EINA_KEY  BINARY SEARCH.
    the statement read tabel with key is absolute in ecc 6 so how to replace it .

    Hi subratt,
    internal tables with header lines are obsolete and in oo context (CLASS / METHOD code) forbidden.
    OK, you'd better use SORTED TABLE and FIELD-SYMBOLS to gt optimal code::
    READ TABLE gt_INT_CURR_VALUE into gs_int_curr_value
    WITH KEY gs_FS_EINA_KEY BINARY SEARCH.
    may be replaced with
    DATA:
      gt_INT_CURR_VALUE_SORTED LIKE SORTED TABLE OF  gs_int_curr_value
        WITH [NON-]UNIQUE KEY <fields of  gs_FS_EINA_KEY>.
    FIELD-SYMBOLS:
      <nt_curr_value> LIKE gs_int_curr_value.
    gt_INT_CURR_VALUE_SORTED = gt_INT_CURR_VALUE.
      READ TABLE gt_INT_CURR_VALUE_SORTED ASSIGNING <nt_curr_value>
        WITH TABLE KEY <key1> = gs_FS_EINA_KEY-<key1> ..  <keyn> = gs_FS_EINA_KEY-<keyn>.
    Regards,
    Clemens

  • Usage of binary key in the read statement

    Hi Experts,
       For usage of binary search in the read statement, what is the minimum number of records (availablity) should be available in the internal table itab as per the below Example..
    SYNTAX: Read table itab with key X binary search.
    For eg: we have to use 16 primary keys only while creating a table.
    Regards,
    Maha

    Hi Pushpraj,
    I am sorry to say this. I am not at all speaking about the number of keys  in the read statement.
    I am talking abt "number of records to be there in the internal table to use binary search"
    Generally, we use binary search when there is more data in the internal table. Can u specify what is the minimum records available in the internal table for using the binary search???
    For Eg: Read table itab with key X binary search.
    Iam talking abt the number of records in the internal table ITAB available if we want to use BINARY SEARCH(keyword).

  • Binary Search

    Hi SDN Experts
    Is it must to use a sort statement before we use a binary search in a read statement. because in some cases its failing as SY-SUBRC = 4 if my internal table is not sorted.
    Please confirm me
    Regards
    Pratyusha

    Hi,
    Yes, it is mandatory to sort the table before doing a binary search.
    The principle of binary search only work in case of sorted tables.
    A binary search algorithm is a technique for finding a particular value in a sorted list. A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. A binary search is an example of a divide and conquer algorithm.
    More on binary search.
    The idea is to eliminate half of the search space with each comparison.
    First, the middle element of the sequence is compared to the value we are searching for. If this element matches the value we are searching for, we are done. If, however, the middle element is “less than” the value we are chosen for (as specified by the relation used to specify a total order over the set of elements), then we know that, if the value exists in the sequence, it must exist somewhere after the middle element. Therefore we can eliminate the first half of the sequence from our search and simply repeat the search in the exact same manner on the remaining half of the sequence. If, however, the value we are searching for comes before the middle element, then we repeat the search on the first half of the sequence.
    Hope this helps.
    Regards,
    Kate

  • Please explain how to use binary search in this loop.

    Hi,
    I want to use the binary search in below Read please give me solution....
    LOOP AT it_vbfa_temp ASSIGNING <fs_vbfa_temp>.
          CLEAR is_ekbz1.
          READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = space.
          IF  sy-subrc = 0.
            w_tabix = sy-tabix.
            is_ekbz1-tknum = <fs_vbfa_temp>-shipno.
            is_ekbz1-flag  = 'X'.
            MODIFY it_ekbz1 FROM is_ekbz1 INDEX w_tabix TRANSPORTING tknum flag.
    ENDLOOP.

    Hi,
    Thanks for the inputs given.. Please find the solution for the same ....
       CLEAR is_ekbz1.
        is_ekbz1-flag = 'X'.
        MODIFY it_ekbz1 FROM is_ekbz1 TRANSPORTING flag WHERE flag IS INITIAL.
        SORT it_ekbz1 BY ebeln xblnr lifnr flag.
      LOOP AT it_vbfa_temp ASSIGNING <fs_vbfa_temp>.
          CLEAR is_ekbz1.
          READ TABLE it_ekbz1 INTO is_ekbz1 WITH KEY xblnr = <fs_vbfa_temp>-deliv flag = 'X' BINARY SEARCH.
          IF  sy-subrc = 0.
            w_tabix = sy-tabix.
            is_ekbz1-tknum = <fs_vbfa_temp>-shipno.
            is_ekbz1-flag  = space.
            MODIFY it_ekbz1 FROM is_ekbz1 INDEX w_tabix TRANSPORTING tknum flag.
      ENDLOOP.
    Regards,
    Srinivas
    Edited by: Srininas on Apr 6, 2010 12:16 PM
    Edited by: Srininas on Apr 6, 2010 12:17 PM

  • When to use binary search ... little urgent.

    Hi evryone,
    Plz let me know under what kind of conditions can we use binary search addition cause if I use it for every read statement the database acees in se30 goes higher than normal.
    If i randomly select only a few big read table statements the database acees in se30 goes is slightly lesser.
    But I some how dont know where to use binary search and where not to with read statements.
    Expecting an early reply.
    Rgds,
    Anu

    Hi,
    If  you read entries from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements.
    READ TABLE <itab> WITH KEY <k1> = <f1>... <kn> = <fn> BINARY SEARCH.
    The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.
    BINARY SEARCH approaches the middle entry in the table, decides if it matches, or lexically greater than or equal to the key being searched. It accordingly skips either the top or bottom part of the internal table and searches the other half. It repeats this process till it finds the row.
    Using BINARY SEARCH with READ is the most efficient way to read standard internal tables which are sorted by the key used to search them
    BINARY SEARCH addition when reading a sorted table is not required, as it happens by default. It makes a good difference in performance if you are reading a large standard internal table without sorting and reading by BINARY SEARCH.
    Regards,
    Padmam.

  • Dynamic java applet for binary search tree

    dynamic java applet for deleting an element of a tree at any specified locationin the tree.

    sorry, having thought a bit more about it let me try to be more correct and concrete. say i have feature a at arbitrary chromosome position 7 and features b at positions 1, 2, 3, 5, 6, 9, 10. right now i have to get the closest distance (Math.abs(7-6)) by doing brute force comparison of all values (Math.abs(7-1), Math.abs(7-2), so on). what i want to do is set up a binary tree thus (please forgive poor ASCII):
    5
    2 9
    1 3 6 10
    and get back the keys that the tree tried (since i know that 7 is not in the tree and a traditional binary search e.g. TreeMap.get(new Integer(7)) will return a null). i should get back 5, 9, 6, in an array/ArrayList and be able to find the closest feature b in three comparisons rather than 7. that doesn't seem like much but when you have a million b features the difference between 1000000 comparisons and log2(1000000) is pretty appreciable. so i am wondering if there is an extant class that will give me back the trace of attempted keys through a binary tree as described or, if not, if this would be horribly difficult to implement as an extension of TreeMap.

  • Reg : Read statement using Binary Search....

    I have an Internal Table as below
    Value           Description
    100               Product
    2008             Production Year
    05                 Production Month
    I am using Read statement with Binary Search for getting Production Month.
    Read table itab with key Description = 'Production Month' binary search.
    I am getting sy-subrc as 4 eventhough data is present in the table for Production Month.
    What may be the problem.

    Hi suganya,
    use
    sort table itab ascending by <production month>.    
    Read table itab with key description = <production month> binary search.
    Remember always, while using binary search always sort the internal table.
    Regards,
    Sakthi.

  • READ...BINARY SEARCH for more than 1 row.

    Hi,
    i have an internal table that contains several same entries. now i want to search the table with READ ... BINARY SEARCH ( or in another efficient way ) and get all those entries and not just one/the first one.
    how could i do that ? thanks in advance!

    hi leider,
    plz c the below sample code.
    loop at i_head into workarea.
    READ TABLE i_zvfx_gts008_itm INTO wa_zvfx_gts008_itm
                                     WITH KEY
                                     bukrs     = wa_zvfx_gts008_head-bukrs
                                  zzinvoice = wa_zvfx_gts008_head-zzinvoice
                                     BINARY SEARCH.
        IF sy-subrc EQ 0.
          LOOP AT i_zvfx_gts008_itm INTO wa_zvfx_gts008_itm FROM sy-tabix.
            IF ( wa_zvfx_gts008_itm-bukrs NE wa_zvfx_gts008_head-bukrs ) OR
          ( wa_zvfx_gts008_itm-zzinvoice NE wa_zvfx_gts008_head-zzinvoice ).
              EXIT.
            ENDIF.
    endloop.
    this is something like avoiding loop at where condition.
    plz reward points if found helpful.

  • READ statement with binary search

    Hi friends,
    I know that while using the READ statement that we have to sort data and use BINARY SEARCH  for faster search.
    I have a situation
    following are internal table contents
    belnr          agent    action
    9000001   name1    BRW
    9000001   name1    API
    when i use READ statement with where condition (  ( belnr - 9000001 ) and  ( action = 'BRW' ) )  with binary search then the SY_SUBRC value is 4.
    if i remove the binary search then its giving SY-SUBRC value 0.
    Can anybody explain why BINARY SEARCH fails.
    Points will be rewarded for correct answers.
    Thanks and regards,
    Murthy

    try this i am not getting sy-subrc 4
    TYPES:BEGIN OF TY_ITAB,
    BELNR TYPE BELNR,
    AGENT(30),
    ACTION(5),
    END OF TY_ITAB.
    DATA:IT_TAB TYPE TABLE OF TY_ITAB,
         WA_TAB TYPE TY_ITAB.
    WA_TAB-BELNR = 9000001.
    WA_TAB-AGENT = 'name1'.
    WA_TAB-ACTION = 'BRW'.
    APPEND WA_TAB TO IT_TAB.
    CLEAR WA_TAB.
    WA_TAB-BELNR = 9000002.
    WA_TAB-AGENT = 'name 2'.
    WA_TAB-ACTION = 'API'.
    APPEND WA_TAB TO IT_TAB.
    loop at it_tab into wa_tab.
    read table it_tab into wa_tab with key belnr = wa_tab-belnr  binary search .
    write: sy-subrc, wa_tab-agent.
    endloop.

  • Read table  binary search

    Hi all,
    Can i use raed table binary search for Hased tables? I coded like this
    DATA:T_MODEL_HASH LIKE HASHED TABLE OF T_MODEL
    WITH UNIQUE KEY PBPINO  WITH HEADER LINE.
    READ TABLE t_model_hash WITH KEY pbpino = t_zwpbph-pbpapino
        BINARY SEARCH.
    But iam getting syntax error "cannot use explicit or implicit index operations on hased tables ".can any one let me know about this issue?

    Hi Priya,
    Hashed tables are managed by a hash algorithm. There is no logical index. The entries are not ordered in the memory. The position of a row is calculated by specifying a key using a hash function.
    A hashed table's performance in reads is NOT dependent on the number of records. However, it is intended for reads that will return only and only one record. It uses a "side-table" with a hash algorithm to store off the physical location of the record in the actual internal table. It is not NECESSARILY sorted/organized in an meaningful order (like a sorted table is). Please note that changes to a hashed tables records must be managed carefully.
    Please check this link to understand how to use hash table.
    http://help.sap.com/saphelp_erp2004/helpdata/en/fc/eb362c358411d1829f0000e829fbfe/frameset.htm
    Regards,
    Ferry Lianto

  • READ using Binary Search

    Hi,
    Is it necessary to use all the sorted keys in READ using BINARY SEARCH .
    For EX: Sort it_tab BY key1 key2
    While Reading i will use only key1 with BINARY SEARCH,Please tell me whether READ statement works?
    Thanks,
    Rathish

    Hello Ratish,
    There isnt any hard and fast rule that you will have to use all the fields on which you sort. The key fields which you may want to use in the READ statement depends on the logic with which you are reading the internal table. As Rob pointed out its better to use all the fields which could make a record unique in the READ statement else it would keep reading only the first occurence of the record.
    Vikranth

Maybe you are looking for