MUMPS Address Validation - intersystems-cache

I am working on prerequisite questions for a class I am trying to attend. I am working on revisions to two pieces of code. I have completed one and I am stuck on this one. I am trying to read an abbreviated address line. In this case FL33606. I am able to read the address. But I am receiving an undefined error for the Quit command "Q: done". Would someone be able to assist me in identifying what is wrong?
N prompt,val, done
S prompt="Enter State and Zip (StateZip): "
F W !,prompt R val Q:val="" D Q:done
. I val'="?2A5N" W !,"Invalid entry" Q
. S done=1
I val="" q
W !,"Valid Entry: ",val
Q

I have two errors
done variable should be defined before the first read
the pattern should not be in quotes, where ? is the operator not =
S prompt="Enter State and Zip (StateZip): "
S done=0
F W !,prompt R val Q:val="" D Q:done
. I val'?2A5N W !,"Invalid entry" Q
. S done=1
I val="" q
W !,"Valid Entry: ",val
Q
Why do you use short commands, and dots?
Is not this much better readable?
Set prompt = "Enter State and Zip (StateZip): "
For {
Write !,prompt
Read val
Quit:val=""
Quit:val?2A5N
Write !,"Invalid entry"
}
If val="" Quit
Write !,"Valid Entry: ",val
Quit

Related

VScode Exception occur: System.ArgumentOutOfRangeException

When I try to run any c++ program in vs code an exception occur and do not run the program
Oops, something went wrong. Please report this bug with the details below.
Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new
-----------------------------------------------------------------------
Last 101 Keys:
c d Space " c : \ U s e r s \ U S E R
\ D o c u m e n t s \ p r o g r a m m i n g \ " Space ; Space i f Space ( $ ?
) Space { Space g + + Space c o d e 2 . c p p Space - o Space c o d e 2 Space
} Space ; Space i f Space ( $ ? ) Space { Space . \ c o d e 2 Space } Enter
Exception:
System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: left
Actual value was -1.
at System.Console.SetCursorPosition(Int32 left, Int32 top)
at Microsoft.PowerShell.Internal.VirtualTerminal.set_CursorLeft(Int32 value)
at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor)
at Microsoft.PowerShell.PSConsoleReadLine.ForceRender()
at Microsoft.PowerShell.PSConsoleReadLine.Insert(Char c)
at Microsoft.PowerShell.PSConsoleReadLine.SelfInsert(Nullable`1 key, Object arg)
at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
-----------------------------------------------------------------------
By using manual command in terminal , I can run my c++ program.
I can find some issues in vscode github repository about this problem but can not understand any solution

Updating my current script which modifies multiple lines into a single one

My current script copies text like this with a shortcut:
:WiltedFlower: aetheryxflower ─ 4
:Alcohol: alcohol ─ 3,709
:Ant: ant ─ 11,924
:Apple: apple ─ 15
:ArmpitHair: armpithair ─ 2
and pastes it modified into a single line
Pls trade 4 aetheryxflower 3 alcohol 11 ant 15 apple 2 armpithair <#id>
As you can see there are already two little problems, the first one is that it copies only the number/s before a comma if one existed instead of ignoring it. The second is that I always need to also copy before hitting the hotkey and start re/start the script, I've thought of modifying the script so that it uses the already selected text instead of the copied one so that I can bind it with a single hotkey.
That is my current script, it would be cool if anyone can also tell me what they used and why exactly, so that I also get better with ahk
!q::
list =
While pos := RegExMatch(Clipboard, "(\w*) ─ (\d*)", m, pos ? pos + StrLen(m) : 1)
list .= m2 " " m1 " "
Clipboard := "", Clipboard := "Pls trade " list " <#951737931159187457>"
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard.
return
If the pattern of your copied text dont change, you can use something like this:
#Persistent
OnClipboardChange:
list =
a := StrSplit(StrReplace(Clipboard, "`r"), "`n")
Loop,% a.Count() {
b := StrSplit( a[A_Index], ": " )
c := StrSplit( b[2], " - " )
list .= Trim( c[2] ) " " Trim( c[1] ) " "
}
Clipboard := "Pls trade " list " <#951737931159187457>"]
ToolTip % Clipboard ; just for debug
return
With your example text, the output will be:
Pls trade aetheryxflower ─ 4 alcohol ─ 3,709 ant ─ 11,924 apple ─ 15 armpithair ─ 2 <#951737931159187457>
And this will run EVERY TIME your clipboard changes, to avoid this, you can add at the top of the script #IfWinActive, WinTitle or #IfWinExist, WinTitle depending of your need.
The answer given would solve the problem, assuming that it never changes pattern as Diesson mentions.
I did the explanation of the code you provided with comments in the code below:
!q::
list = ; initalize a blank variable
; regexMatch(Haystack, regexNeedle, OutputVar, startPos)
; just for frame of reference in explanation of regexMatch
While ; loop while 'pos' <> 0
pos := RegExMatch(Clipboard ; Haystack is the string to be searched,
in this case the Clipboard
, "(\w*) ─ (\d*)" ; regex needle in this case "capture word characters
(a-z OR A-Z OR 0-9 OR _) any number of times, space dash space
then capture any number of digits (0-9)"
, m ; output var array base name, ie first capture will be in m1
second m2 and so on.
, pos ? pos + StrLen(m) : 1) ; starting position for search
"? :"used in this way is called a ternary operator, what is saying
is "if pos<>0 then length of string+pos is start position, otherwise
start at 1". Based on the docs, this shouldn't actually work well
since 'm' in this case should be left blank
list .= m2 " " m1 " " ; append the results to the 'list' variable
followed with a space
Clipboard := "" ; clear the clipboard.
Clipboard := "Pls trade " list " <#951737931159187457>"
ClipWait, 0 ; wait zero seconds for the clipboard to change
If ErrorLevel ; if waiting zero seconds for the clipboard to change
doesn't work, give error msg to user.
MsgBox, 48, Error, An error occurred while waiting for the clipboard.
return
Frankly this code is what I would call quick and dirty, and seems unlikely to work well all the time.

Is there a Pythonic way to add a string to the start of each file in a directory made with the name of the file without its extension?

I have written a code to append a string which is made up of file name to the file with that file name, but it does not append just one line, but the name of all the files in that folder and the line gets added after the data in all the files. All I want is to append a string to the start of the file.
With my code, I am getting all the Three Lines printed in all the files, at the end of each file i.e.
previous data...
parent a A B C D
parent b A B C D
parent c A B C D
This is my code
import os
import glob
os.chdir("C://Users//folder_naming_test_python//")
files = os.listdir()
#print("files=" )
#print(files)
d = []
for k in os.listdir():
d.append( k.split('.')[0])
#print("names=")
#print(d)
prefix = 'parent '
postfix = ' A B C D'
Headers = list(map(lambda orig_string :prefix + orig_string + postfix, d))
#print("Headers = ")
#print(Headers)
array_len = len(Headers)
for file in files:
for i in range(array_len):
f = open(file, 'a+')
a = f.read()
f.seek(0)
f.write(Headers[i]+'\n')
f.close()
f = open(file, 'r')
print(f.read())
My input data example would say; 3 files in a folder with names
a.txt, b.txt, c.txt
what I expect is irrespective of the data in the files,
either
parent a A B C D or
parent b A B C D or
parent c A B C D
followed by the data in file.....
has to be printed on first line of each file respectively(Note. a, b and c strings have to go in individual files and not all together in all the files)
It can be done very easily using fstrings
import os
from pathlib import Path
for filename in os.listdir():
with open(filename, "r+") as f:
content = f.read()
f.seek(0, 0)
f.write(f"parent {Path(filename).stem} A B C D\n")
f.write(content)

Read .txt file value after a certain string (Matlab)

I'm trying to obtain the displacements of a node from an FEA .txt results file using matlab. I want to search for the node (e.g. 5151) and then read the displacements, the problem is the node is mentioned a couple of times before getting to the deformations.
The part of the results I'm interested in looks like this......
N O D E O U T P U T
THE FOLLOWING TABLE IS PRINTED FOR NODES BELONGING TO NODE SET ASSEMBLY_TIP_NODES
NODE FOOT- U1 U2 U3 UR1 UR2 UR3
NOTE
5101 2.6105E-03 -1.1943E-02 6.0023E-03 -8.6770E-02 -1.6432E-02 -1.1048E-02
5102 2.5224E-03 -1.1267E-02 5.6868E-03 -8.6763E-02 -1.6390E-02 -1.0943E-02
5103 2.4340E-03 -1.0589E-02 5.3709E-03 -8.6725E-02 -1.6269E-02 -1.0666E-02
5104 2.3455E-03 -9.9121E-03 5.0549E-03 -8.6542E-02 -1.6052E-02 -1.0267E-02
5105 2.2575E-03 -9.2374E-03 4.7396E-03 -8.6041E-02 -1.5708E-02 -9.7843E-03
5106 2.1710E-03 -8.5682E-03 4.4262E-03 -8.5111E-02 -1.5217E-02 -9.2317E-03
5107 2.0869E-03 -7.9085E-03 4.1164E-03 -8.3688E-02 -1.4585E-02 -8.6334E-03
5108 2.0063E-03 -7.2623E-03 3.8120E-03 -8.1750E-02 -1.3825E-02 -8.0049E-03
5109 1.9299E-03 -6.6336E-03 3.5150E-03 -7.9308E-02 -1.2958E-02 -7.3631E-03
5110 1.8586E-03 -6.0265E-03 3.2271E-03 -7.6400E-02 -1.2011E-02 -6.7206E-03
5111 1.7928E-03 -5.4442E-03 2.9500E-03 -7.3084E-02 -1.1010E-02 -6.0892E-03
5112 1.7329E-03 -4.8897E-03 2.6851E-03 -6.9435E-02 -9.9818E-03 -5.4777E-03
5113 1.6791E-03 -4.3652E-03 2.4334E-03 -6.5533E-02 -8.9528E-03 -4.8933E-03
etc, so what I want to do is search for a unique string ' N O D E O U T P U T' and then search for '5151' and import its displacements into an array.
Example output
nodeDisp = 1.6791E-03 -4.3652E-03 2.4334E-03 -6.5533E-02 -8.9528E-03 -4.8933E-03

How can you add (say a vectors name) to an object of type 'formula' within a function?

This is a very trimmed down version of what I want to do, I can't paste my exact problem cause the code is too long and complex but I think this gets at the root of issue. Thanks to Josh's answer to this question How do you code an R function so that it 'knows' to look in 'data' for the variables in other arguments? I'm part way there.
example <- function(model, xvar3=NULL, xvar4=NULL, data){
print(class(model))
#xvar3 <- eval(substitute(xvar3), envir=data, enclos=parent.frame())
#xvar4 <- eval(substitute(xvar4), envir=data, enclos=parent.frame())
print(class(xvar3))
xvar5 <- xvar4^2
mod <- glm( model + xvar3 + xvar5, data=data)
return(mod)
}
example(mpg ~ cyl, hp, wt, data=mtcars)
This fails. If you remove the comments (based on help from previous question) it solves the problem of 'finding' hp and wt. 'model' is of class formula and I would like that to become 'mpg ~ cyl + xvar3 + xvar5' so that the glm will run. But I can't seem to be able to add them to the formula.
I've been toying around with 'call' classes and further with 'eval', and 'as.formula' with variations of 'paste' and 'noquote' etc but can't see to get it to stick.
Here's one way. The trick I used is to create a new formula based on the given one + the two extra variables. I then did a trick with the environment of the formula so that both xvar3/xvar5 AND any variables local to the caller can be used.
glm will always look in the formula's environment AND in the data for variables (and nowhere else!). That's why the formula environment must the manipulated a bit in this case: it contains xvar3 and xvar5, and the parent environment is set to the original formula's environment so that it is also searched for variables (foo in the last example)...
example <- function(model, xvar3=NULL, xvar4=NULL, data){
e <- new.env(parent=environment(model))
e$xvar3 <- eval(substitute(xvar3), envir=data, enclos=parent.frame())
e$xvar4 <- eval(substitute(xvar4), envir=data, enclos=parent.frame())
e$xvar5 <- e$xvar4^2
model <- update(model, . ~ . + xvar3 + xvar5)
environment(model) <- e
mod <- glm(model, data=data)
return(mod)
}
example(mpg ~ cyl, hp, wt, data=mtcars)
# Using a local variable should work too:
doit <- function(d) {
foo <- d$cyl+1
example(mpg ~ foo, hp, wt, data=d)
}
doit(mtcars)
Here's how I'd do it:
add_vars <- function(model, xvar3=NULL, xvar4=NULL, data){
# Capture the unevalated calls to xvar3 and xvar4
xvar3 <- substitute(xvar3)
xvar4 <- substitute(xvar4)
# Use substitute to create the correct formula to supply to update
update_f <- eval(substitute(. ~ . + xvar3 + I(xvar4 ^ 2),
list(xvar3 = xvar3, xvar4 = xvar4)))
# Modify the original formula string
update(model, update_f)
}
add_vars(mpg ~ cyl, hp, wt)
# mpg ~ cyl + hp + I(wt^2)
Another option for this (from a colleague):
example <- function(model, xvar3=NULL, xvar4=NULL, data){
data$xvar3 <- eval(substitute(xvar3), envir=data, enclos=parent.frame())
data$xvar4 <- eval(substitute(xvar4), envir=data, enclos=parent.frame())
data$xvar5 <- data$xvar4^2
model <- as.formula(paste(model[2], paste(model[3], "xvar4","xvar5", sep="+"), sep="~"))
mod <- glm(model, data=data)
return(mod)
}
example(mpg ~ cyl, hp, wt, data=mtcars)
I like this it's quite clean.