How do I clear a GROUP containing a GROUP with a DIM() Array?

No need to be abusive when nice people are trying to help you out.

1 Like

No ! Translation difficulties are possible ā€¦
I am ready to read any answers, but I wanted to clarify my task.

1 Like

For testing I would change CSTRING to PSTRING because the 1st byte contains the length so messages clearly show Length=32 indicating it contains a Space i.e. <20h> = CHR(32). The Length of a CSTRING is based on finding the first CHR(0) which is not going to exist in a Group cleared to all Spaces, so LEN() returns is ???


You have proven that Clear(gBarCode.overlay) works. The Overlay GROUP has a DIM(3) so the Topic of ā€œHow do I clear a GROUP with an Array?ā€ is not complete.

You have proven that Clear(gBarCode) fails to clear an inner child GROUP with a DIM(). So adjusting topic to note somethng about ā€œinside a Parent GROUPā€.

Note this problem will also happen on CLEAR of a RECORD or QUEUE containing a GROUP with DIM().


The Clear(gBarCode) works if there is no DIM() on Overlay. So the RTL properly handles clearing Group child fields ā€¦ It fails that it treats Groups with DIM as a big STRING:
image


What will work for you is moving the DIM from the GROUP onto the Child fields:

image

What also would work is changing CSTRING to a normal Clarion STRING. But Clear() has trouble with all types e.g. DECIMAL would = -2020.20.

1 Like

I understand your need to generate a Clear for any type of group.
You could create a custom data type and store the original values to restore them when necessary. This way, you avoid depending on specific types.

Regarding the use of the STACK to clear a structure recursively, I donā€™t find a clear justification. However, it could be useful in more complex scenarios, such as generating JSON or XML from data structures.


TPrintList      group,type
Task                group,Dim(3)
Flag                    long,Dim(3)
Print                   bool(false)
type                    cstring(21)
text                    cstring(129)
Doc                     group,Dim(3)
Page                        long,Dim(3,3)
                        end
                    end 
                end
...
PrintListCleared    like(TPrintList)        ! Clear Structure.
PrintList           like(TPrintList)        ! Working Structure.
...


Hello again Genricke

I have made changes to your EasyClear to use howmany() for arrays as mentioned in my earlier message above.

EasyClear  Procedure(*Group MyGroup) !,long,proc
lCount     Long
aFieldRef  Any,Auto
pGroupRef  &Group
dimCount   long,auto
x          long,auto
  code  
  loop
    lCount += 1
    aFieldRef &= What(MyGroup, lCount)
    if aFieldRef &= Null then break.  ! End of Group

    dimCount = howmany(MyGroup, lCount)
    x = 1
    if dimCount > 1 then aFieldRef &= What(MyGroup, lCount, 1).
    loop
      if IsGroup(MyGroup, lCount)
        pGroupRef &= GetGroup(MyGroup, lCount, choose(dimCount > 1,x,0))
        if x >= dimCount
          lCount += easyClear(pGroupRef)
        else
          easyClear(pGroupRef)   
        end
      else
        clear(aFieldRef)
      end
      if x >= dimCount then break.
      x += 1
      aFieldRef &= What(MyGroup, lCount, x)
      if aFieldRef &= Null then break. ! just in case - should NOT happen
    end
  end
  return lCount - 1

OK well hopefully that works. If it doesnā€™t then please show an example where it fails and I will have a look at it - but as Jeff suggests, it is good to be polite and kind to those who are trying to help!

cheers again

Geoff R

2 Likes

Have a look at the c function ā€œmemsetā€ & try something like ā€¦
memset(address(theGroup),0,size(theGroup))

BTW much easier without a recursion, just iterate over group fields one by one and CLEAR those that are a GROUP (and optionally HOWMANY > 1).

1 Like

Interesting idea. For his Group it would work.

It would not work well if the Group contained the typical Clarion STRING types as they are not blank when filled with Chr(0).

It would work for CSTRING, PSTRING and all the numeric types. I think ASTRING is a LONG so it should work.

Thanks ! This is a beautiful and correct solution to my question.
I checked the nested GROUPS, arrays and ANY fields - everything is OK !
I need to learn how to make such code on my own ā€¦

1 Like

great thanks Genricke - glad you appreciated it - it is not everyday that someone comments that your code is ā€œbeautifulā€ :blush:

I guess everyone has their own idea of beauty, but by chance I was recently reading a book that may be of interest:

Beautiful Code: Leading Programmers Explain How They Think
Edited by Andy Oram and Greg Wilson

You can often learn from reading other peopleā€™s code and this book has contributions from 33 different developers. The foreword mentions:

In May 2006, I asked some well-known (and not so well-known) software designers to dissect and discuss the most beautiful piece of code they knew. As this book shows, they have found beauty in many different places. For some, it lives in the small details of elegantly crafted software. Others find beauty in the big pictureā€”in how a programā€™s structure allows it to evolve gracefully over time, or in the techniques used to build it.

5 Likes

Thanks for the recommendation - Iā€™ll try to find such a book.
I have such a desktop book with almost 900 pages.
There are also a lot of examples of beautiful code in it. :smiley:

1 Like

And it happens to be available as a PDF download!

Iā€™ll definitely be having a browse, it will make an interesting change from the SciFi Iā€™m reading ATM!

1 Like

Ive never found a device thats easy on the eyes, and legible to read electronic txt. Still prefer paper.

2 Likes