No announcement yet.

%scan + %subst

  • Filter
  • Time
  • Show
Clear All
new posts

  • %scan + %subst


    is it known (or even documented?) if an expression such as

    x = %scan(str: %subst(buffer: 1: toLen));

    (where buffer is a 'large' character field, 'str' is a 'short', usually single character, string and 'toLen' is an integer)

    will be 'optimized' in the sense that it will simply search for characters in the first 'toLen' positions of 'buffer' or if it will be relatively inefficient in the sense that it will first extract the substring, possibly copying it somewhere, and then search the resulting substring?

    BTW, I need to run this on V7R2 for the forseeable future.

    Thanks in advance

    John Lewis

  • #2
    I would expect that it first extracts the substring and then it scans the resulting substring.
    The substring of the buffer field does not necessarily start at position 1.

    It's good you notice you are on V7R2 -- start and length positions in %scan() was first implemented in V7R3.

    If the field buffer is a varying length field then according to the manual for %scan():
    When any parameter is variable in length, the values of the other parameters are checked against the
    current length, not the maximum length.

    A little optimization in case buffer is a fixed length character string -- add the search argument to the end of buffer.

    %subst( buffer : toLen + 1 ) = str; // there must of course be enough characters available in buffer for this

    x=%scan(str : buffer);
    if x <= toLen and x > 0;
    // do something
    // str not found in buffer


    • #3
      It would not be normal for a substring to "physically" be taken - all such operations are typically pointer based and copying of the data would not be needed. It would also be normal for the comparison operation to be short-circuit optimized - although that may occur down in the microcode or hardware. Don;t know how the current compiler implements these things at teh w-code level.