Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Scanning and Trimming

Scanning and Trimming

Scanning and Trimming

I am trying to use a query created database file as input for a RPG program. The database file has the following fields:
Field       length
Number        7
Company      40
Country      15
Name        164

Name is a concatenation of 4 fields.

I defined the file in the RPG program as:

DFile      DS
D  Num             7
D  Co             40
D  Cty            15
D  Nam           164

What I need to do is to search the Nam field for the characters 'ATTN:' or 'CONTACT:' and remove everything that preceds the ':'.

I think the SCAN command will be able to help me search the field. I've seen examples written in Free-form but not the standard form. Does anyone know how it should look like?

From what I read of the TRIM command it only removes leading/trailing blanks. Should I just Scan for the position of ATTN: and use it as the start of a SUBST?

RE: Scanning and Trimming

Is there a way to SCAN again starting from the position of the last SCAN performed?


Name field contains:
This street, that country ;Attn:Mr.x/Mr.Y   ;

I performed a scan for both ATTN and ; to get the length of a substring. In the example above, since the first ';' comes before 'ATTN' then the substring length is < 0. Is there a way to SCAN again so that I can get the position of the second ';'?  

RE: Scanning and Trimming

Set third parameter in scan to set start position.

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!

RE: Scanning and Trimming

Why can't you do this in free form? The %REPLACE BIF was made for this.


     D Valid_Chars     C                   '''-/ ABCDEFGHIJKLMNOPQRSTUVWXYZ0123-
     D                                     456789abcdefghijklmnopqrstuvwxyz&#'  
     D Pos             S              5U 0 Inz
     D Nullstring      S              1A   Varying Inz('')


         // Get rid of all invalid characters in CUSAD1 field.
         Dou Pos = 0;                                           
           Pos = %CHECK(Valid_Chars:Cusad1);                    
           If Pos > 0;                                          
             Cusad1 = %REPLACE(Nullstring:Cusad1:Pos:1);        

Solum potestis prohibere ignes silvarum.

RE: Scanning and Trimming

You want %scan or %replace, as suggested.  However, you are wrong about %trim.  It does only trim beginning/ending chars, but can trim ANY chars, not just blanks.
Also, if you have free-form and want to code in fixed, it pretty easy to convert back. (Can't think of a good reason to do so). For example flapeyre's code would look like this:


     C                   Dou       Pos = 0                                           
     C                   Eval      Pos = %CHECK(Valid_Chars:Cusad1)                    
     C                   If        Pos > 0                                          
     C                   Eval      Cusad1 = %REPLACE(Nullstring:Cusad1:Pos:1)        
     C                   Endif                                               
     C                   Enddo   

RE: Scanning and Trimming

I usually do SUBSTR:

        IF substr(nam:1,4) = "ATTN"
        OR substr(nam:1,7) = "REPLACE"

RE: Scanning and Trimming

Sorry, Didn't quite read the entire scope of your needs.

I usually use SUBSTR in conjuction with a do loop. Positioning
should all be a variable.

Check for the trailing spaces to give the length of the
name field to scan/test then subtract the length of your test variable "ATTN" and "REPLACE" this will give you the last position before you exit your do loop.

RE: Scanning and Trimming

Quote (arrow483):

It does only trim beginning/ending chars, but can trim ANY chars, not just blanks.
Sorry Arrow but this statement can lead to put a wrong interpretation on the TRIM or %TRIM function.
- In SQL the TRIM function removes blanks or another specified character from the end or beginning of a string expression.
- In RPG IV the %TRIM function returns the given string less any leading and trailing blanks and solely blanks.

RE: Scanning and Trimming

>>In RPG IV the %TRIM function returns the given string less any leading and trailing blanks and solely blanks <<

Not true anymore.

The second parm on the %trim, %trimL, %trimR  identifies the characters to be trimmed.  Esp useful for dumping things like $ and * , for example.

NewField = %trim(***$5.67***:'*$')  will trim off all the *'s and the $, but NOT any blanks (because blanks not in parm 2).  Without the 2nd parm, it trims only blanks.

RE: Scanning and Trimming

Good to know. However, that second parameter for %trim doesnt appear to be available at V5R3 and below. On those earlier releases, you can always use my little routine above (it worked on a V5R3 machine compiling to a previous release of V5R1).

Solum potestis prohibere ignes silvarum.

RE: Scanning and Trimming

I didn't know that and learnt something new today. I have to say that I work on a V5R2 box and the second parm on the %TRIM doesn't exist yet.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close