printing numbers with the width of the length of a specific string in Julia - printf

In Julia, when I want to print a number "4" with the width of 10, I do:
#printf("%10d", 4)
If I want to set the width as the length of a specific string, for example:
mystr = "Hello World"
how to change 10 in "%10d" to length(mystr)?

Checkout the Formatting library:
using Formatting
printfmt("{:$(length("Hello World"))d}", 4)

Julia doesn't have support for runtime format strings. See https://github.com/JuliaLang/julia/issues/4248
However, there is a hack listed in that issue. See https://github.com/JuliaLang/julia/issues/4248#issuecomment-24196846. Essentially, you write your own printf function that will compile this on the fly:
printf(fmt::String,args...) = #eval #printf($fmt,$(args...))

Related

Postgresql "+" symbol for carriage return / new line for zsh script

So I query that pulls text from a column called "description."
Each description contains a list like so:
1) Some text here
2) Some text here
3) Some text here
The problem is when I run the query in my zsh script the new lines return a "+" symbol instead of a carriage return.
1) Some text here + 2) Some text here + 3) Some text here...
The code is
tempdescriptionday4=$(/Applications/Postgres.app/Contents/Versions/12/bin/psql -h 1.1.1.1 -p5555 -U myuser mytable -t -c "SELECT description FROM cycle_10 WHERE air_date = 2020-11-10"
I'm trying to inject this list into an XML file for an RSS feed but I'm stuck on how to format this properly. I tried replacing the + symbol with 
 but that didn't work--I still am not getting a new line. Any ideas?
Found this in the postgresql12 manual — it's called:linestyle
"Sets the border line drawing style to one of ascii, old-ascii, or unicode. Unique abbreviates are allowed. (that would mean one letter is enough.) The default setting is ascii. This option only affects the aligned and wrapped output formats"
So for me since I have multiple lines in a cell it "wraps" the text at the end making it a wrapped output format.
"ascii style uses plain ASCII characters. Newlines in data are shown using a + symbol in the right-hand margin." When the wrapped format wraps data from one line to the next without a new line character, a dot"." is shown in in the right-hand margin of the first line, and again in the left-hand margin of the following line."
There's also "old-ascii" style that uses a ":" for wrapping and a "unicode" style which uses an ellipsis symbol in the right and margin of first line.
So the problem I have is that the output format is using ASCII by default when there's no available option for XML output style for new lines. Bummer.
Turning linestyle off fixes removing the + symbol but I want the outputted linestyle to be XML so if it strips any new line indication there's no way for me to format it. "-A --no-align Switches to unaligned output mode. (The default output mode is aligned.) This is equivalent to \pset format unaligned."
So I used awk to substitute the + symbol for 
 and also tried
but those are not creating new lines for me? It validates as proper XML.

Visual Basic "Format" function turning Hex values that end with A into a time

I have inherited some code and am very new to VB.
The code is basically being fed decimal values, these are being converted into the Hex equivalent and (I think) the Format function is being used to make sure only 2 characters (i.e. a byte) are being used in another string.
The problem is this, when the Format function encounters a Hex value that Ends in an 'A', it seems to convert the string into a time format of some sort.
Example:
"4A" converts to 04:00:00
"7A" converts to 07:00:00
Here's the relevant code snippet:
Format("4A")
In the actual code I'd get a "00", as the function has the following optional additions:
Format("0A","00")
I'm assuming the "A" is some special character.
Anybody have an idea around this quirk? Thanks in advance!
A is being interpreted as AM just as P would be PM and output 16:00.
Format() is likely not the correct thing to use here, it would only pad as you want it to if the input were a number.
Better to pad after you convert the base:
hexa = Hex$(i)
If (i < 16) Then hexa = "0" & hexa

How can I sum two numbers in Latex with my own command?

I have to sum two numbers (integers) in LaTeX. I also have to "print" the process of sum. So it would look like 5+2=7 in text. Any ideas?
My code so far:
\newcommand{\Sum}
{\newcounter{cnt}
\setcountter{cnt}{1+1}
}
In LaTeX, first you have to define a counter with:
\newcounter{countername}
Then you can put a value in this counter with:
\setcounter{countername}{<value>}
where <value> is an integer. Or you can add one to this counter with:
\stepcounter{countername}
or you can add some arbitrary value to this counter with:
\addtocounter{countername}{<value>}
Then, to access this counter you use:
\value{countername}
so you can, for example, make calculations with this counter:
\setcounter{anothercounter}{%
\numexpr\value{countername}+10\relax%
}
Then, when you need to print the value of this counter to the pdf file, you can you the mighty \the:
\the\value{countername}
or you can use one of these:
\arabic{countername}
\roman{countername}
\Roman{countername}
\alph{countername}
\Alph{countername}
Perhaps a different syntax can be in order; instead of supplying each argument to calculate a sum, you can supply the operator with the operands. This allows you to be a little more flexible in terms of the input and also provide more functionality:
\documentclass{article}
\usepackage{xfp}
\NewDocumentCommand{\showcalculation}{o m}{$
\IfValueTF{#1}
{#1}{#2} = \fpeval{#2}
$}
\begin{document}
\showcalculation{7+3}
\showcalculation{1+2-3+4}
\showcalculation[22 \div 7]{22 / 7}
\showcalculation[10 \times 3^{7 - 7}]{10 * 3 ^ (7 - 7)}
\end{document}
The optional argument for \showcalculation uses a LaTeX formatting for the printable calculation.
I've managed to solve the problem.
\newcommand{\Sum} [2] {#1 + #2 = \the\numexpr #1 + #2 \relax \\}
And then I use my command as:
\Sum {7} {3}

change display format for all string variables to as short as possible

After compressing my data, I have several string variables with storage type str4 or str1 and format %9s. I would like to revert them all to the default display format, which help dformat reports should be %#s for str#. Is there a quick way to do this?
This is the structure of my best guess:
ds, has(type string)
foreach v of varlist `r(varlist)' {
format `v'
}
This does not work because, instead of reformatting to the default value with this command, the format function just displays the format.
A reproducible example:
clear
input str50 mystr
"b"
"a"
end
compress
format myst
This is the situation I was confronted with. I am not sure if it applies beyond strL formatted variables. (Roberto suspects that it does not; see comments.)
Addendum. My goal here was to make browse-ing my data easier. It seems that format is respected in the browser (truncating to length of one for %1s, say), while it is overridden by the actual length of the string when printing to the console.
I am surprised that you seem surprised, as with your syntax the format command (not function) does indeed just display the format, as is documented. Incidentally, you don't need a loop to do that as format will take a varlist as argument:
. clear
. set obs 1
obs was 0, now 1
. foreach t in a b c {
2. gen `t' = "`t'"
3. }
. format a b c
variable name display format
-----------------------------
a %9s
b %9s
c %9s
-----------------------------
Joshing apart, I think you need just one line here, which is something like
. format a b c %1s
or
. format a b c %-1s
to signal which justification. Stata doesn't truncate displayed strings just because they don't match the string display format; it might well truncate strings because there isn't space to show them, but (I'm naturally open to counter-examples) the above display formats for variables will do well for most purposes.
EDIT: The following device may help.
gen length = 0
ds, has(type string)
quietly foreach v in `r(varlist)' {
replace length = length(`v')
su length, meanonly
format `v' %`r(max)'s
}
drop length

Mathematica: exporting to a variable path

this is tricky.
Once the path to export data in Mathematica is under quotes, how can I insert a variable as part of the path? In other words, I'm inside a loop that increments VAL and want to export MyData to VAL.dat. Ideas?
Pseudocode:
Export["~/Documents/VAL", MyData]
In addition to Howard and Mr.Wizard's answers I could say that it would be good to look up FileNameJoin for a nice, system-independent way to compose path strings and IntegerString which you could use to convert integers to strings with a fixed number of positions, making your files sort more nicely:
In[33]:= VAL = 32;
IntegerString[VAL, 10, 4]
Out[34]= "0032"
I usually don't have much need for inter-OS compatibility (programming mostly for myself), so my usual style would be something like
Export["directoryPart\\FixedFileNamePart"<>IntegerString[VAL, 10, 4]<>".dat","TSV"]
Replace "TSV" with the file type you need if it isn't clear from the extension. Please note that I am on windows, which uses the backslash as separator. Since this is also the escape character, it has to be escaped with a backslash itself; this explains the double backslash. You seem to be on a UNIX derivate so there's no need for that. This does show the value of FileNameJoin which takes care of these details automatically.
How about converting your number to string and join it with the path:
"~/Documents/"<>ToString[VAL]
In direct answer to your question, you can use StringReplace:
Table[
StringReplace[
"~/Documents/#.dat",
"#" :> IntegerString[VAL, 10, 4]],
{VAL, 27, 29}
]
{"~/Documents/0027.dat", "~/Documents/0028.dat", "~/Documents/0029.dat"}
"#" was arbitrarily chosen as a placeholder. Another character or string of characters could be used just as well.