Question
· Apr 25

Complete novice question

I'm trying to learn M programing for an Epic db class prerequisite. They said to download the IRIS Studio software to do the testing. I'm having a very difficult time finding information the language. I'm trying to run some examples that Epic has provided (like in below) but the compiler complains that it isn't valid. Of course, it doesn't tell you why it isn't valid.

 

r !,"Enter the hour: ",hr  
 r !,"Enter the minute: ",min 
 r !,"Enter the second: ",sec 
 s Mtime=$$ConvertToMTime(hr,min,sec)
 w !,"Equivalent M time: ",Mtime  
 q
ConvertToMTime(h,m,s)
 q (h*3600)+(m*60)+(s)

I get these errors:

ERROR: zipcodevalidate.int(11) #8: <NOLINE>
 TEXT: s Mtime=$$ConvertToMTime(hr,min,sec)
ERROR: zipcodevalidate.int(14) #1026: Invalid command : 'ConvertToMTime(h,m,s)' : Offset:15
 TEXT: ConvertToMTime(h,m,s)

 

Would appreciate any guidance on why this doesn't work or some sources to better understand how to use Studio.

Product version: IRIS 2023.3
Discussion (8)4
Log in or sign up to continue

The first error means that you aren't calling the method/routine correctly. See the <NOLINE> error code in this doc page: https://docs.intersystems.com/iris20231/csp/docbook/Doc.View.cls?KEY=RER... . Are you sure $$ConvertToMTime is the right name?

As for the second error: unlike most other programming languages, just calling a routine/method does not work in M/ObjectScript. Every line of code must use a command; in this case what you are looking for is the DO command: https://docs.intersystems.com/iris20231/csp/docbook/Doc.View.cls?KEY=RCO...

So, when calling a method/routine, you would write 'do ConvertToMTime(h,m,s)'

Hi @James Rutledge 

welcome in our world !

Using this ROUTINE below :

ROUTINE test.1
    r !,"Enter the hour: ",hr  
    r !,"Enter the minute: ",min 
    r !,"Enter the second: ",sec 
    s Mtime=$$ConvertToMTime(hr,min,sec)
    w !,"Equivalent M time: ",Mtime," (Time:",$zt(Mtime),")",!

    /// Display some other internal time values :
    for a = 0,3600,43200,45296,86399 {
        w !,"Equivalent M time: ",a," (Time:",$zt(a),")",!
    }
    q
ConvertToMTime(h,m,s)
    q (h*3600)+(m*60)+(s)

Will display the following result :

USER>d ^test.1

Enter the hour: 12
Enter the minute: 34
Enter the second: 45
Equivalent M time: 45285 (Time:12:34:45)

Equivalent M time: 0 (Time:00:00:00)

Equivalent M time: 3600 (Time:01:00:00)

Equivalent M time: 43200 (Time:12:00:00)

Equivalent M time: 45296 (Time:12:34:56)

Equivalent M time: 86399 (Time:23:59:59)

You can also use classes instead of routines.

For instance :

Class test.a Extends %RegisteredObject
{

ClassMethod run() As %Status
{
    Set sc = $$$OK
    read !,"Enter the hour: ",hr  
    read !,"Enter the minute: ",min 
    read !,"Enter the second: ",sec     
    do ..getInternalTime(hr,min,sec)

     /// Display some other internal time values :
    for a = 0,3600,43200,45296,86399 {
        w !,"Equivalent M time: ",a," (Time:",$zt(a),")",!
    }
    Return sc
}

ClassMethod getInternalTime(hr As %Integer, min As %Integer, sec As %Integer) As %Status
{
    set sc=$$$OK
    set Mtime=$$ConvertToMTime(hr,min,sec)
    write !,"Equivalent M time: ",Mtime," (Time:",$zt(Mtime),")",!
    return sc
}

ClassMethod ConvertToMTime(h, m, s) As %Integer
{
        Return (h*3600)+(m*60)+(s)
}

}

Will give : 

USER>w ##class(test.a).run()

Enter the hour: 12
Enter the minute: 34
Enter the second: 45
Equivalent M time: 45285 (Time:12:34:45)

Equivalent M time: 0 (Time:00:00:00)

Equivalent M time: 3600 (Time:01:00:00)

Equivalent M time: 43200 (Time:12:00:00)

Equivalent M time: 45296 (Time:12:34:56)

Equivalent M time: 86399 (Time:23:59:59)
1

Hi James, I'm sending same exercise with asteroids 😂

novice
    do {
        for  {
            read !,"Enter time HH:MM:SS :",hr#2
            if hr="" quit
            read ":",min#2,":",sec#2
            if hr '?2n!(hr >23) write !,"hr  must be a number between 0 and 23" continue
            if min'?2n!(min>59) write !,"min must be a number between 0 and 59" continue
            if sec'?2n!(sec>59) write !,"sec must be a number between 0 and 59" continue
            quit
        }
        if $l(hr) {
            write !,"Equivalent M time: ",$$ConvertToMTime(hr,min,sec)
            write " ",$zth(hr_":"_min_":"_sec)
        }
    } while $l(hr)
    quit

ConvertToMTime(h,m,s)
    quit (h*3600)+(m*60)+s