Article
· Sep 13, 2018 1m read
Find a table given its name

The following code snippet includes a class method "test" that runs code to find a class based on the class's name. "test" takes one argument, which is the name of the table:


Class objectscript.findTable Extends %RegisteredObject
{
    classmethod test(name as %String="mytable")  
    {
            #Dim result as %ResultSet
            #Dim tName as %String
            #Dim contain as %Integer
     
            Set contain=0
            Set result = ##class(%ResultSet).%New("%Dictionary.ClassDefinition:Summary")
            Do result.Execute()

            While(result.Next()) 
            {
                Set tName=$get(result.Data("Name"))
                &sql(select position (:name in :tName) into :contain)
                Write:contain'=0 tName, " ... ", name, " (", contain,")", !
            }
            Return $$$OK
     }
}

Here's a link to the code on GitHub

2 6
0 712
Discussion
· Feb 2, 2022
Code Golf: Paired Opposites

You will receive a string of comma-separated integers whose elements have both a negative and a positive value, except for one integer that is either only negative or only positive, our challenge will be to find that integer.
As usual shortest solution wins.

Input

"1,-1,2,-2,3"

Output

3

3 has no matching negative appearance

4 34
0 682

Points to remember before you start:

  1. It is not possible in a COS (Caché Object Script) job/process context to have multiple Named Pipes. It is a one Named Pipe per job/process limited line of communication.
  1. Named Pipes, in Caché, like most pipes on most operating systems are Unidirectional. That means you open them for either Read or Write, but not both.
3 0
0 680

Environment: Cache for Windows (x86-64) 2015.2.2 (Build 811U) Thu Mar 3 2016 12:55:48 EST [HealthShare Modules:Core:14.01.351

When try the following to send back SOAP Fault, is the following correct way?

Set fault=##class(%SOAP.Fault).%New()
Set fault.faultcode=$$$FAULTServer
Set fault.faultstring="Processing Error"
Set fault.detail= "Error on server"
Do ..ReturnFault(fault)

0 1
0 674
Article
· Mar 28, 2017 2m read
Map, Reduce and Filter Collections

Inspired by the article "Declarative development in Caché" that's still trending on the dev com. The OP explored a functional style of iterating over a collection. A comment today suggested "Caché would need syntax support for anonymous functions".

With Macros you can kind of get anonymous like syntax using dot notation.

This is not production code, but it does work. First the macros...

2 15
0 670
Question
· Aug 31, 2017
Extract piece of string

Hello guys,

I was wondering whether there is another alternative to extracting a certain string out from a bigger one, rather than using the function $piece.

Maybe regular expressions? Another specific function?

Example:

Big string:

NAD+SLA+++TextIsHereNAD+SLA+++TextIsHere

String I want to extract:

TextIsHere

Function I'm already using:

0 3
0 664

Hello

I made for fun a bash script that creates csv files from a %GOGEN exported file.
%GOGEN, the utility for exporting globals for Cache 5.xx

First I thought it would be very simply with bash, but ...
- if I want a header in the csv file
- if you export with GOGEN more globals together not only one
- if I want a csv file for every global/indexes has the global ..

The simply bash script goes up 100 lines of code. If I will start again with this, I would use Python or Go, not bash.

Example:

For a EXPORTED.GLO file

0 5
0 661
Article
· May 1, 2020 1m read
Code sample to concatenate JSON arrays

ObjectScript doesn't include any built-in method for appending one JSON dynamic array to another. Here's a code snippet I use that's equivalent to the JavaScript concat() method.

Call it with any number of arguments to concatenate them into a new array. If an argument is a dynamic array, its elements will be added. Otherwise the argument itself will be added.

5 2
2 658

We're developing Ensemble PoC and one day our frontend developer (who doesn't have Ensemble production running) said that Populate just doesn't cut it and he needs to see the real data. He needed only one object, but the problem was - it's a big object. Still, I checked ids of everything related and wrote this command (parts omitted, but you get the idea):

2 1
0 652
Discussion
· Jul 31, 2019
CodeGolf: FizzBuzz

Let's have a round of CodeGolf!

As usual the goal is to write the shortest solution for a specified problem.

Today we have one of the classics: FizzBuzz.

Write a program that prints the numbers from 1 to 100.

But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.

For numbers which are multiples of both three and five print “FizzBuzz”

4 22
1 617

I am trying to use %INLIST in SQL query using a cursor and the query fails to return results. It appears that the variable I use against %INLIST returns an empty string. All the examples I have seen use result sets and build the query as a string. Is it possible to use %INLIST in a cursor-based query? Below is a a snippet of the code I am using:

Property mylist as %String (MAXLEN="") [InitialExpression = "route1, route2, route3"];

set routeList = $LISTBUILD(mylist)

&sql(DECLARE MyReport CURSOR FOR

SELECT

ProcessStartDate,

0 3
0 586

I needed to know programmatically if last ran failed or not.

After some exploring, here's the code:

ClassMethod isLastTestOk() As %Boolean
{
  set in = ##class(%UnitTest.Result.TestInstance).%OpenId(^UnitTest.Result)
  for i=1:1:in.TestSuites.Count() {
    #dim suite As %UnitTest.Result.TestSuite
    set suite = in.TestSuites.GetAt(i)
    return:suite.Status=0 $$$NO
  }
  quit $$$YES
}

1 4
1 582
Question
· Apr 5, 2018
Drawing boxes

Hello, Community!

Here's an interesting task I found on the Internet.

Problem description

Write a method that would draw a box of a specified size.

The goal is to write the shortest method.

Here's a method signature (it can't be modified):

ClassMethod main(s As %Integer = 10)

And call sample:

>do ##class(ITPlanet.Task4).main(5)
#####
## ##
# # #
## ##
#####

>do ##class(ITPlanet.Task4).main(10)
##########
##      ##
# #    # #
#  #  #  #
#   ##   #
#   ##   #
#  #  #  #
# #    # #
##      ##
##########
 
>do ##class(ITPlanet.Task4).main(20)
####################
##                ##
# #              # #
#  #            #  #
#   #          #   #
#    #        #    #
#     #      #     #
#      #    #      #
#       #  #       #
#        ##        #
#        ##        #
#       #  #       #
#      #    #      #
#     #      #     #
#    #        #    #
#   #          #   #
#  #            #  #
# #              # #
##                ##
####################

2 15
0 577

Hello,

We would like to learn how would you approach this task.

We would need to check every day a set of folders which are outside our server (in fact we could see them as if they were "inside" the server), then we need to check for modified or created files in the last day, and move all the ones which match the criteria to just one folder in our server.

We have thought about doing the following:

1) Create a task to run it each day:

0 5
0 576