Iterating through all the Holidays Sets of a Holidays List

Navigation:  Appendices > Macro Language > Built-in Functions > Holidays > Using Holidays Functions >

Iterating through all the Holidays Sets of a Holidays List

Previous pageReturn to chapter overviewNext page

Sometimes you need to use macro holidays functions to present a list of holidays, grouped by Holidays Sets, such as in the example below :

 

The code below will iterate through all the Holidays Sets of a Holidays List.

 

sCurHolList = SetCurHolidaysList('a')

nCtrHolSet = 1 // the counter for the list of Holidays Sets

nCurHolSet = GetNthHolSetId(sCurHolList, nCtrHolSet)

// loop over the HolidaySets in current Holidays List

while nCurHolSet <> 0

 // use the current Holidays Set Id (nCurHolSet)

 // for example in : FindHolidayOnDate(nDate, nCurHolSet)

 . . .

 . . .

 // when you are done, prepare for next iteration        

 inc(nCtrHolSet)

 nCurHolSet = GetNthHolSetId(sCurHolList, nCtrHolSet)

endwhile

 

To place this code in the context of everyday Q++, we will analyze the salient point of the above code. The line

 

sCurHolList = SetCurHolidaysList('a')

 

corresponds to looking up the value of the a-Holidays List property of the International ScriptLine.

The lines

 

nCtrHolSet = 1

nCurHolSet = GetNthHolSetId(sCurHolList, nCtrHolSet)

 

correspond to looking at the first Holidays Set selected in the a-Holidays property editor.

If there is no first Holidays Set (ie. none are selected) then the function returns 0 and the whole WHILE block is skipped.

 

while nCurHolSet <> 0

 

Otherwise it will hold the value of the first elements Holidays Set ID. The block of code is performed and, at the end of the block, the counter variable is incremented and GetNthHolSetId  is called again.

 

inc(nCtrHolSet)        

nCurHolSet = GetNthHolSetId(sCurHolList, nCtrHolSet)

 

If there is another element in the list, then nCurHolSet will hold the value of the nCtrHolSet's element of the list, and the block of code will be executed again with this new value of nCurHolSet. The loop will end when there are no more elements and GetNthHolSetId  returns 0.

 

---

 

An actual example of what you may use the above loop for is to get and string together all the holidays that occur for a given Holidays List on a given date (this actually corresponds to evaluating the token [Fa]).

The sample code below includes a fleshed-out block of code to do something everytime we get a Holidays Set ID.

 

sCurHolList = SetCurHolidaysList('a')

nCtrHolSet = 1nCurHolSet = GetNthHolSetId(sCurHolList,

nCtrHolSet)// loop over the HolidaySets in current Holidays List

while NotEqualN(0,nCurHolSet) // loop over all holidays on nDate

 Reset_FindHolidayOnDate() // initialize table        

 while FindHolidayOnDate(nDate, nCurHolSet)                

         // table is now positioned on record, get the info ASAP

         sCurHol = GetHolNameFromTable()

         nCurHolId = GetHolIdFromTable()

         nCurHolDate = GetHolDateFromTable()

         // use this info to do something …

         sRESULT = sRESULT + '&s' + sCurHol

 endwhile // loop all holidays for CurHolSet on nDate

 inc(nCtrHolSet)

 nCurHolSet = GetNthHolSetId(sCurHolList, nCtrHolSet)

endwhile

 

Once we have our Holidays Set ID, we initialize the holidays table and look for a holiday on the current date.

 

Reset_FindHolidayOnDate() // initialize table        

while FindHolidayOnDate(nDate, nCurHolSet)

         

We use the 3 direct table access functions to get info about the specifics of the holiday found.

 

sCurHol = GetHolNameFromTable()                

nCurHolId = GetHolIdFromTable()                

nCurHolDate = GetHolDateFromTable()

 

And we list every holiday found on a line separated by a soft-return.

 

sRESULT = sRESULT + '&s' + sCurHol


Topic 108272 updated on 06-Mar-2002.
Topic URL: http://www.qppstudio.net/webhelp/index.html?iteratingthroughalltheholidays.htm