Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scanning and Trimming

Status
Not open for further replies.

Ju5

Programmer
May 25, 2007
85
0
0
PH
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?
 
Is there a way to SCAN again starting from the position of the last SCAN performed?

Ex.

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 ';'?
 
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!
 
Why can't you do this in free form? The %REPLACE BIF was made for this.

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

      /Free

         // 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);        
           Endif;                                               
         Enddo;



Solum potestis prohibere ignes silvarum.

 
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:
Code:
     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

 
I usually do SUBSTR:

IF substr(nam:1,4) = "ATTN"
OR substr(nam:1,7) = "REPLACE"
DO.....
 
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.

 
arrow483 said:
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.
 
>>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.
 
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.

 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top