[solved] superdescriptor with strange behavior (packed variable)

Hi.

I have this superdescriptor in my view:

SP-REG-CLI-DISCON-EMIS           A    18
-------- SOURCE FIELD(S) -------        
CD-TIPO-REG(1-1)                        
REF-PENDTE(1-12)                        
IND-DISCON(1-1)                         
F-FACT-NUM(1-4)        

The fields:

1  AM CD-TIPO-REG                      N   1,0   
1  BP REF-PENDTE                       N  12,0  N D 
1  BW IND-DISCON                       A     1  N D
1  BQ F-FACT-NUM                       P   7,0  N D

the super coded in program:

1 #SP-REG-CLI-DISCON-EMIS           (A18)
1 REDEFINE #SP-REG-CLI-DISCON-EMIS       
  2 #CD-TIPO-REG (N1)                    
  2 #REF-PENDTE(N12)                     
  2 #IND-DISCON(A1)                      
  2 #F-FACT-NUM(P7)      

It happens that it doesn’t work if I reset the #SP and fill the fields #CD, #REF and #IND.

If I don’t reset the #F-FACT per se, it doesn’t bring the correct result on the read.

This doesn’t work:


RESET #SP-REG-CLI-DISCON-EMIS                           
DECIDE ON FIRST VALUE OF #I-FAT                         
  VALUE  1   #IND-DISCON := 'B'                         
  VALUE  2   #IND-DISCON := 'G'                         
  VALUE  3   #IND-DISCON := 'I'                         
  VALUE  4   #IND-DISCON := 'J'                         
  NONE VALUE #IND-DISCON := 'R'                         
END-DECIDE                                              
#CD-TIPO-REG := 1                                       
#REF-PENDTE  := POL                                     
READ MULTI-FETCH OF 10 SGC-RECI-PENDIENTES              
    BY SP-REG-CLI-DISCON-EMIS EQ #SP-REG-CLI-DISCON-EMIS

To make it work, I need to reset individually the variable, despite resetting the level 1 field.

Anyone can bring me a light on this?

thanks in advance,
Marcelo.

Certainly we can help. Because your F-FACT-NUM field is P7, it is causing trouble. When you reset the top level alpha field, it is reset to all spaces (hex ‘40’) so the packed numeric field contains ‘40404040’ instead of ‘00000000’. Resetting F-FACT-NUM sets it correctly to ‘0000000’ (the last byte may be something other than zero since that is where the sign is stored).

2 Likes

Hi Jerome, thanks for the quick reply.

So I understand that everytime I have a packed field inside a superdescriptor, I need to reset it individually, right? There is other way to do it? Just curious now.

thanks again.

If you reverse the definition/redefinition, then a reset will do what you intend.

1 #SP-DEF
  2 #CD-TIPO-REG (N1)                      
  2 #REF-PENDTE (N12)                       
  2 #IND-DISCON (A1)                        
  2 #F-FACT-NUM (P7)        
          1 REDEFINE #SP-DEF
  2 #SP-REG-CLI-DISCON-EMIS (A18)

RESET #SP-DEF

Just to add to Ralph’s solution:

The reason it works is quite simple.

When you RESET #SP-REG-CLI-DISCON-EMIS you were RESETing an A18 field.

As noted by Jerome, this resulted in a string of blanks being placed in #SP-REG-CLI-DISCON-EMIS, when you required
#F-FACT-NUM to be a string of zeroes.

What Ralph accomplished by reversing the definition was to RESET #SP-DEF which is NOT a field. As you can see from Ralph’s definition, #SP-DEF is what is called a Group-name in Natural. Note that #SP-DEF does not have a format or length. This is because only fields have format and lengths, not Group-names.

Group-names are basically abbreviations in Natural. At compile time, Natural replaces a Group-name with the name(s) of the component fields in the Group. Thus,

RESET #SP-DEF becomes RESET #CD-TIPO-REG #REF-PENDTE #IND-DISCON #F-FACT-NUM

Each field in the Group is RESET according to its format and length. Hence, #F-FACT-NUM is initialized to a string of zeroes.

I just can’t thank you guys enough.

Once I heard someone saying that was necessary to reset each variable always, but with no explanation on why.

The explanation given by Steve is what I understood as default behavior when you reset a variable that has a redefine.
I thought the compiler would automatically reset all of the “inner” variables.

This is truly awesome to understand.

The other way to define your superdescriptor field would be to put a group under the redefine:

1 #SP-REG-CLI-DISCON-EMIS           (A18)  
1 REDEFINE #SP-REG-CLI-DISCON-EMIS   
  2 GROUP
    3 #CD-TIPO-REG (N1)                      
    3 #REF-PENDTE(N12)                       
    3 #IND-DISCON(A1)                        
    3 #F-FACT-NUM(P7)        

Then RESET #SP-REG-CLI-DISCON-EMIS.GROUP. This will reset the individual fields.

I like Jerome’s technique as a quick fix of a badly defined superdescriptor, but I wouldn’t intentionally code it that way. The difference is a minor one, but his version requires at least one RESET GROUP statement to correct the initial contents of #F-FACT-NUM. In mine, all numeric fields start with valid contents and you might not even need to RESET for subsequent uses of the superdescriptor. Additionally, you can INITialize the superdescriptor’s components with default values without resorting to ASSIGN statements.

After all, the first rule of Natural programming is don’t code unnecessary statements.