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...

Similar Messages

  • 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).

  • Problem with READ Statement in the field routine of the Transformation

    Hi,
    I have problem with read statement with binary search in the field routine of the transformation.
    read statement is working well when i was checked in the debugging mode, it's not working properly for the bulk load in the background. below are the steps i have implemented in my requirement.
    1. I selected the record from the lookuo DSO into one internal table for all entried in source_packeage.
    2.i have read same internal table in the field routine for each source_package entry and i am setting the flag for that field .
    Code in the start routine
    select source accno end_dt acctp from zcam_o11
    into table it_zcam
    for all entries in source_package
    where source = source_package-source
         and accno = source_package-accno.
    if sy-subrc = 0.
    delete it_zcam where acctp <> 3.
    delete it_zcam where end_dt initial.
    sort it_zcam by surce accno.
    endif.
    field routine code:
    read table it_zcam with key source = source_package-source
                                                 accno  = source_package-accno
                                                 binary search
                                                 transportin no fields.
    if sy-subrc = 0.
    RESULT  = 'Y'.
    else.
    RESULT = 'N'.
    endif.
    this piece of code exist in the other model there its working fine.when comes to my code it's not working properly, but when i debug the transformation it's working fine for those accno.
    the problem is when i do full load the code is not working properly and populating the wrong value in the RESULT field.
    this field i am using in the report filter.
    please let me know if anybody has the soluton or reason for this strage behaviour.
    thanks,
    Rahim.

    i suppose the below is not the actual code. active table of dso would be /bic/azcam_o1100...
    1. is the key of zcam_o11 source and accno ?
    2. you need to get the sortout of if endif (see code below)
    select source accno end_dt acctp from zcam_o11
    into table it_zcam
    for all entries in source_package
    where source = source_package-source
    and accno = source_package-accno.
    if sy-subrc = 0.
    delete it_zcam where acctp 3.
    delete it_zcam where end_dt initial.
    endif.
    sort it_zcam by surce accno.
    field routine code:
    read table it_zcam with key source = source_package-source
    accno = source_package-accno
    binary search
    transportin no fields.
    if sy-subrc = 0.
    RESULT = 'Y'.
    else.
    RESULT = 'N'.
    endif.

  • 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

  • Issue with read statement with one more key missing in mapping

    Hi All ,
    I have such data in two internals table :
    IT_bdc
    vbeln            posnr
    90000593     10
    90000576     10
    90000672     10
    90000672     20
    90000672     30
    it_konv
    kbetr          vbeln
    6250          90000576
    12160000          90000593
    500000          90000672
    600000          90000672
    700000          90000672
    My current program statement is :
    LOOP AT it_bdc.
    READ TABLE it_konv WITH KEY
          vbeln = it_bdocs-vbeln.
      currency =   it_konv-waers.
    endloop.
    as you can see the posnr is missing in it_konv how can i modify this read statement so
    that vbeln posnr from it_bdc should get correct kbetr from it_konv.
    Kindly help in this mapping.

    Hi
    sort it_konv by vbeln
    then
    loop at it_bdc.
    read table it_konv with key vbeln = it_bdc-vbeln binary search.
    if sy-subrc = 0.
    perform your logic/task.
    endif.
    endloop.
    also it depends what you want to do after reading it_konv.
    in my logic if there is a vbeln in it_konv which s present in it_bdc then sy-subrc will be 0
    and you can perform your logic.
    and if there will be no matching vbeln in it_konv then sy-subrc will not be 0.
    check the values in debugging.
    Thanks
    Lalit

  • Binary search question with objects

    Hi
    I have a class called MyClass which is as below
    public class MyClass
    String from;
    String to;
    double d;
    // with getter and setter method
    }I create a ArrayList of MyClass,
    List<MyClass> list = new ArrayList();
    list.add(myClass);
    //add about 8000 myClass object from database; I want to do a search for object from, to;
    I have been doing
    for(MyClass myClass:list)
    if(myClass.equals(from) && myClass.equals(to))
    return myClass;
    }Rather i would like to do a randomSearch, how can i do it,
    will it be faster, then going through all the list
    Any ideas

    Binary search requires that your list is sorted, so you'll have to implement Comparable<MyClass>, and use Collections.sort(list) before doing the search.
    I also think that this:
    if(myClass.equals(from) && myClass.equals(to))implies that you aren't using equals() properly. I think it should look more like this:
    if(myClass.getFrom().equals(from) && myClass.getTo().equals(to))Here's a quick and dirty example of implementing Comparable and doing a binary search:
    static class MyClass implements Comparable<MyClass> {
         private String from, to;
         public MyClass(String from, String to) {
              this.from = from;
              this.to = to;
         public String getFrom() { return from; }
         public String getTo() { return to; }
         public int compareTo(MyClass obj) {
              int toComp = to.compareTo(obj.getTo());
              int fromComp = from.compareTo(obj.getFrom());
              return toComp - fromComp;
         public boolean equals(Object obj) {
              if (obj instanceof MyClass == false) {
                   return false;
              return ((MyClass)obj).getFrom().equals(from) && ((MyClass)obj).getTo().equals(to);
         public int hashCode() {
              return from.hashCode() + to.hashCode();
         public String toString() {
              return "from: "+from+", to: "+to;
    public static void main(String[] args) {
         int num = 5;
         List<MyClass> tests = new ArrayList<MyClass>();
         for (int i = 0; i < num; ++i) {
              MyClass test = new MyClass(i+"", (i*2)+"");
              tests.add(test);
         Collections.sort(tests);
         for (MyClass test : tests) {
              System.out.println(test);
         MyClass theTest = new MyClass("4", "8");
         int index = Collections.binarySearch(tests, theTest);
         MyClass result = tests.get(index);
         System.out.println("index: "+index+", result: "+result);
    }Hope that helps.

  • Search Option with filter

    Dear all,
    Searching threads with options/list like answered,very helpful answers,unaswered,
    This functionality will further simplify search process & saves time.
    I'm not able to register my point in idea place,it requires s-user login ?
    Regards,
    A.Jeyakanthan

    >
    Keshav.T wrote:
    > Jeyakanthan,
    >
    > Isn't that already available ?
    >
    >
    > Narrow results by
    > Resolved (25551)
    > Possibly Resolved (18123)
    > Open (9742)
    > Assumed Resolved (267)
    > closed_unanswered (3)
    >
    How do search & get above results ?

  • Problems with READ STATEMENT

    Hello ABAPers,
    I have this code:
      SELECT ebeln bukrs
      FROM ekko
      INTO TABLE it_ekko
      FOR ALL ENTRIES IN idata
      WHERE ebeln = idata-vbeln_p.
      LOOP AT idata WHERE ebeln IS NOT INITIAL.
        READ TABLE it_ekko WITH KEY ebeln = idata-ebeln
                                    bukrs = idata-bukrs_r.
        MODIFY idata.
      ENDLOOP.
    I'm getting this error:
    The internal table IT_EKKO has no header line - explicit specification of an output area with INOT wa or ASSIGNING <fs> is required.
    I don't see what I'm doing wrong. Please advise.
    Thanks in advance.
    Ol Pom.

    Hi Ol Pom
      What Neil was pointing to is highlighted below. Please check the same to have some understanding.
    <b>Current Logic</b>:
    SELECT ebeln bukrs
       FROM ekko
       INTO TABLE it_ekko
       FOR ALL ENTRIES IN idata
       WHERE ebeln = idata-vbeln_p.
    LOOP AT idata WHERE ebeln IS NOT INITIAL.
         READ TABLE it_ekko WITH KEY ebeln = idata-ebeln
         bukrs = idata-bukrs_r.
         MODIFY idata.
    ENDLOOP.
    --> Nowhere between the loop and end, any data of
    internal table <b>idata</b> is modified, hence the
    statement MODIFY IDATA doesnt make any modifications.
    For avoiding the syntax error, declare a work area of
    type IT_EKKO or declare IT_EKKO with header line.
    Options to do the same:
    data: wa_ekko like it_ekko.
    <b>OR</b>
    data: begin of it_ekko <b>occurs 0</b>,
          end of it_ekko.
    Above will rectify the syntax error.
    Please see that your code resembles something like below:
    data: wa_ekko like it_ekko.
    IF NOT it_ekko[] is INITIAL.
       SELECT ebeln bukrs
         FROM ekko
         INTO TABLE it_ekko
         FOR ALL ENTRIES IN idata
         WHERE ebeln = idata-vbeln_p.
    ENDIF.
    LOOP AT idata WHERE ebeln IS NOT INITIAL.
         READ TABLE it_ekko into it_ekko
              WITH KEY ebeln = idata-ebeln
                       bukrs = idata-bukrs_r.
         check sy-subrc eq 0.
    *     ... change contents of idata w.r.t it_ekko.
         MODIFY idata.
    ENDLOOP.
    Kind Regards
    Eswar

  • Not getting correct signing options with Reader XI

    I orignally asked this question in the Reader forum and it was suggested that I ask it over here. The problem may be related to reader settings or something that I'm unfamiliar with.
    I've been beating up this problem for a while.
    Created a form using Acrobat Pro XI and added two signature fields. The intent is to use this to have a customer sign a workorder with a stylus and a touch screen PC carried by my employee. I need the "place a signature" option.
    Problem: I get different options on two different computers. The one with Pro and the Reader installed gives the the options I want. The one with just the Reader installed only gives me "Work with Certificates" signing option.
    The form is saved "Reader Extended" and it doesn't matter, I still don't get the options to "place a signature" if I save it with the "reader extended" options or not.
    If I set it up as an EchoSign enabled form, I get the exact same results.
    Here is a screen shot of the options from the computer with just the reader installed. You can see I have no signing options aside from the "Work with Certificates" options.
    Here is a screen shot from the other computer that has both the reader and acrobat pro installed. It gives me the ability to palce a signature.
    Here is a sample form.
    Thanks for any help and guidance you have.

    Hi George, here is a sample form that is not reader-enabled and has no signature field. 
    With both forms, when I open them on the PC that only has Reader XI installed on it, the only group of options I get under the sign feature is the "Work with Certificates" group. For the reader enabled form, it will lets me sign using a certificate. With the form that is not reader enabled, all options are grayed out.
    If I use the PC that I have both Acrobat Pro XI and the Reader XI installed on, when I open the form with the reader, I get the options that I would expect as shown in the original post.
    I looked through all of the preference settings in Reader to see if there was someplace I could enable the "I need to sign" group of options or something like that and I don't find anything.
    Here is the same forms with signature fields added and I get the same results. This one is reader-enabled and this one is not.
    Thanks again.

  • Problem with READ statement

    hi,
    my data declarations and code is as follows:
    types:ty_bapi_return     LIKE  bapiret2.
    data: t_return            TYPE  STANDARD TABLE OF ty_bapi_return,
          wa_return           TYPE                    ty_bapi_return.
    CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
                 EXPORTING
                      documentheader = wa_bapi_hdr
                 TABLES
                      accountgl      = t_accountgl
                      currencyamount = t_currencyamount
                      return         = t_return.
            READ TABLE t_return INTO wa_return WITH KEY type = 'E'.
              MOVE: wa_return-type       TO wa_msg_table-type,
                    wa_return-message    TO wa_msg_table-message,
                    wa_return-message_v2 TO wa_msg_table-message_v2,
                    wa_bkpf-belnr     TO wa_msg_table-belnr,
                    wa_bkpf-gjahr     TO wa_msg_table-gjahr,
                    wa_bkpf-bukrs      TO wa_msg_table-bukrs.
              APPEND wa_msg_table TO t_msg_table.
              CLEAR wa_msg_table.
              CONTINUE.
    ELSEIF wa_return-type = 'S'.
    call bapi_transaction_commit
              CLEAR   wa_commit_return1.
              REFRESH t_commit_return1.
              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                   EXPORTING
                        wait   = 'X'
                   IMPORTING
                        return = t_commit_return1.
    Now when i debug and see wa_return , i cant find any values in wa_return.but the table t_return has 2 rows with type 'S'.can anyone tell me what the problem is..thank you.
    regards,
    Challa.

    READ TABLE t_return INTO wa_return WITH KEY type = 'E'.
    If you reading the table looking for TYPE "E"...And the table only got 2 rows with TYPE "S"...You will get a line into WA_RETURN...
    If you want records in WA_RETURN, you should write this...
    READ TABLE t_return INTO wa_return WITH KEY type = 'S'.
    "E" stands for error...If your BAPI have been well performed...You're not going to get any errors -;)
    Greetings,
    Blag.

  • "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

  • 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

  • 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.

  • Read Itab binary search not working.

    Hi,
    I have the following issue:
    From a table, I am picking up 5 fields into an internal table (in the correct sequence):
    A B C D E.
    Now, I am sorting the above internal table as follows:
    sort itab by A B C descnding.
    Now, I am writing the read stmt as follows:
    read table itab into wa_itab with key
                                  A = var1
                                  B = var2
                                  C = car3
                                 Binary Search.
    But, the read fails in most of the cases even though the exact values exist in the itab. It does not fail always, but fails for majority of the cases, though the exact matches are present in the itab.
    If I remove the Binary Search option, it works fine.
    Could you please let me know if I am missing out on anything here?
    Please let me know if any more information is needed.
    Thank You,
    Anjana Banerjee

    Hi anjana,
    please do not ask questions before reading the online help:
    ["The standard table must be sorted in ascending order by the specified search key. "|http://help.sap.com/saphelp_nw73/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/frameset.htm]
    Regards,
    Clemens

  • Read statement with repeated key field

    Hi Experts ,
    We  are in the process of UCCHECK in an upgrade program and come across an issue with read statement using repeated key fields which is not allowed in a unicode compatable environment.
                READ TABLE it_vbpa WITH KEY
                                          vbeln = l_vbeln
                                          parvw = '0'
                                          parvw = 'ZN'.
    I checked this in 4.6c environment and observed that the Read statement uses the last key value for reading and doesnt consider other values even if the last value is not present in the table  .
    I want to know if I can use only that last value for read statement ? If so, what was the use of the repeated key fields in a read statement?
    Thanks and Regards
    Sanu

    Hi,
    Your main aim in a upgrade would be to successfully replicate the 4.6x functionalities in the ECC 6 version with the unicode checks. So it would be a safe option to only consider the last key condition. I dont have access to a 4.6C environment. May be it was a mistake corrected by SAP in the new version.
    Vikranth

Maybe you are looking for

  • Facing issues in single step processing

    We are using single step processing, But facing issues while running GenData only. We get the desired output if we run GenData and GenPrint. And our Input file is XML Below is our AFGJOB.JDT and FSIUSER.INI(what we are using) AFGJOB /* This base (thi

  • IPhoto Library's Lost All Photos

    OS 10.4.10 Ibook G4 1.5 GB Ram iPhoto 5.0.4 Hi, My first time on iPhoto group. I've been using iPhoto successfully since I got my mac, 2 years ago. I may not be using many of the features available. I download digital pictures, and I take pictures of

  • Help! Photoshop Elements 9. Need to resize images to clone parts! Resize isn't working!

    Trying desperately to clone the sky from one painting image to the other. The images are in different scales/sizes -  and tho I TRY to resize them the same, the darn resize will NOT cooperate! How do I scale them the same so I can clone?! Thank you!

  • Updating plug ins from CS2 to CS3

    I have written a grifics intense book in the trial version of CS3 (which has expired.)  I was given CS2 Premium but cannot open my documents because I am needing to "update" plugins. How do I do this?  and where do I get them? Cheryl

  • Premier Pro CS4 cumbersome on Mac.

    Problem # 1: So my timeline has  a 2 minute edit beyond the point I want to highlight. Select the track select tool, shift+click, spinning beach ball, wait for aproximately 10+ seconds before the tracks are highlighted. Problem # 2: Even tho all you