ObjectScript: Looping over a multidimensional global value and comparing

Hi,

I have a global that holds users name and date of birth.

I would like to loop through them and check if the date of birth is more than 2 years based on todays date

so I have ^checker("under2", "Eric David", "02-05-2018")

I want to check the birthdate based on todays date to see if it is greater than 2. How do I access the birthdate in the global

Please can you advice

I tried :

set values = $order(^checker("under2"))
set td = $zdate($horolog, 3)
set ol = $p(^checker("under2"), ",", 3) // this does not work
while (values ' = "")
{
set difdays = $system.SQL.DateDiff("d", ol, td)
if difdays > 730
{    
set ^value("greater than 2")
}
}
  • 0
  • 0
  • 111
  • 3
  • 2

Answers

Hello,

You have to navigate through subscript using $order.
Assuming your first node is "under2" and won't change, and assuming your date is "DD-MM-YYYY"

you can do:

ClassMethod TestGlobalAccess()
{
   set ^checker("under2", "Eric David", "02-05-2018") = ""
   set ^checker("under2", "Eric2 David2", "02-07-2016") = ""
   set ^checker("under2", "Name1 Name2", "07-06-2018") = ""
   set Name = ""
   for {
        set Name = $order(^checker("under2", Name))
        quit:Name=""
        set DateBirth = ""
        for {
             set DateBirth = $order(^checker("under2",Name,DateBirth))
             quit:DateBirth=""
             set CacheDateBirth = $zdateh(DateBirth,15)
             if ((+$h-CacheDateBirth)>730) {
                  write $$$FormatText("%1 with Birthdate=%2 is older than 2 years",Name,DateBirth), !
             }
        }
    }
}

I hope this help.

for date conversion I decided to convert the birthdate into Cache Date, instead of converting $horolog into "DD-MM-YYYY" format.
But your method work also.

If you have only one record you can do:

ClassMethod Test()
{
   set ^checker("under2", "Eric David", "02-05-2018") = ""
   set Name = $order(^checker("under2", ""))
   quit:Name=""
   set DateBirth = $order(^checker("under2",Name,""))
   quit:DateBirth=""
   set CacheDateBirth = $zdateh(DateBirth,15)
   if ((+$h-CacheDateBirth)>730) {
      write $$$FormatText("%1 with Birthdate=%2 is older than 2 years",Name,DateBirth), !
   }
}

I hope this help.

Cheers,

Jacques

Thank you so much Jacques. That was really helpful. I was able to build my solution

An old fashioned design:

ClassMethod TestA()
{
        s  (under,name,dob)=""
1      under=$o(^checker(under)) g:under="" end g:under'="under2" 1
2      name=$o(^checker(under,name)) g:name="" 1
3      dob=$o(^checker(under,name,dob)) g:dob="" 2
         s dob1=$zdateh(dob,15)
         if (+$h-dob1)>730 {name,?30,dob}
         g 3
end q
}

But you should take into account leap years.

In the common case:

ClassMethod TestB(underage,daysofyears)
{
       s (under,name,dob)=""
1    under=$o(^checker(under)) g:under="" end g:under'=underage 1
2    name=$o(^checker(under,name)) g:name="" 1
3    dob=$o(^checker(under,name,dob)) g:dob="" 2
      s dob1=$zdateh(dob,15)
      if (+$h-dob1)>daysofyears {name,?30,dob}
      g 3
end q
}

Hi Vladmir, thank you so much. I just saw your answer.