Announcement

Collapse
No announcement yet.

%SCAN using hex

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • %SCAN using hex

    How can I use %SCAN ((or another solution) to scan a character string looking for '2' in the zone position of each byte of the string?

    Example, I want to find occurrences of x'22', x'2C', x'20', etc.

  • #2
    You could first use %XLATE to change every x'2n' to x'20' in a temporary string, and then use %SCAN to seach for x'20' in the temporary string.

    Code:
    dcl-pi *n;
       string char(5) const;
    end-pi;
    dcl-s temp like(string);
    dcl-s p int(10);
    dcl-s start int(10);
    
    temp = %xlate(x'20212223242526272728292a2b2c2d2e2f'
                : x'2020202020202020202020202020202020'
                : string);
    start = 1;
    p = %scan(x'20' : temp);
    dow p > 0;
       dsply ('found in position ' + %char(p));
       start = p + 1;
       if start > %len(temp);
          leave;
       endif;
       p = %scan(x'20' : temp : start);
    enddo;
    
    return;
    A sample run of my program:
    Code:
    4 > call bmorris/junk x'8121832223'
      DSPLY found in position 2
      DSPLY found in position 4
      DSPLY found in position 5

    Comment


    • #3
      You mind sharing the use case? I'm curious.

      Comment


      • #4
        I guess you could also loop through the string using %BitAnd to test each byte against x'20'.

        IN some ways it depends on why you want to do this in the first place. It is an unusual requirement and (other than as a test question) I find it hard to think of a possible use case.

        Comment


        • #5
          Originally posted by jtaylor___ View Post
          You mind sharing the use case? I'm curious.
          Since you asked:
          In our shop we have a lot of RPGLE source members that have been "colorized".

          Example:
          FILE SPECS - white/underline x'26'
          KLIST - yellow dashes x'36'
          .
          .
          .
          PLIST - red underline x'2C'
          .
          etc.

          you get the picture. Now, here is the problem:
          When I write a new program, I make a saved copy of it in one of my libraries before I move it to production. My saved copy doesn't have any colorization. A few weeks later somebody makes some changes to this program and then colorizes the code (there is a utility that does this) and move it back to production. Then later on I have to make changes to this source code (now colorized), so I run a source compare that compares by saved version with the production version to see what has changed since I wrote it and, presto, 90% of the code has changed, so I have to sort out what changes are actual logic changes and which one are due to colorization

          What I want to do is have a utility that removes the colorization. So, what I would do is take the production version (with colorization) and copy it to an off line source file, then remove the colorization from the offline copy and run a source compare between that and the version I saved right after I wrote it. Then, presto, I have a source compare that shows what has actually been changed logic wise.

          Comment


          • jtaylor___
            jtaylor___ commented
            Editing a comment
            Thanks....

        • #6
          What you really need to do is persuade your colleagues to use an editor that doesn't pre-date the ark! One that colorizes so all this nonsense is not needed! Heck there are good free ones these days so no excuse.

          I wonder given what you say whether a scan is even needed? Why not just use a modified version of the colorizer code and just put x'20' whenever it would have put one of the colors.

          Comment


          • #7
            Another way to go about this is to copy your saved version of the code to another source member and use the utility to colorize that (or colorize your saved version without copying), then the only differences should be what was changed.

            Comment


            • #8
              Your original question was to find '2' in the zone portion of a byte. Now you are saying that you want to find any 5250 attribute code (which is in range of x'20' - x'3F' -- note that half of that range has '3' in the zone.) You even used x'36' (yellow "dashes") as one of your examples. Why ask only about the '2' if you know you need to look for '3' as well?!

              Why not just use %XLATE to translate anything in that range to x'40' (blanks)? What's the point of scanning for them? It'd be a lot faster and easier.

              If you do need to identify them rather than just translating them, why not simply loop through the string and look for them?

              Comment

              Working...
              X