Right aligning using printf_P in C - c

I am attempting to get an output of:
Score:
0
but my output keeps coming out like
Score: 0
this is what I have implemented:
move_cursor(30,4);
printf_P(PSTR("Score : %8d\n"), get_score());
move_cursor(37, 8);
we are writing the score in Putty, from AVR to serial.
What am I doing wrong?

Q: If you want "0" on a separate line ... then shouldn't you put a matching `\n' in your format statement?
Q: If You want it right-aligned at column 6, then shouldn't your format statement be %6?
EXAMPLE: printf_P(PSTR("Score :\n%6d\n"), get_score());
PS:
As you're probably aware, "printf_P()" isn't standard C; it's AVR-specific.

Related

How to print variable name in Cobol

I am trying to pass to a C function the variable names from a cobol program.
01 Message.
03 varA PIC X(32).
03 varB PIC X(32).
Consider the fact that this function will be used in many programs and the structure of variable Message will be different everytime , how can i pass to the C function the names of the variables?
I allready consider making another group data item to contain the variable names, but this is not a good solution for me.
I am using Microfocus Server Express v5.1 on AIX.
how can i pass to the C function the names of the variables?
It is not possible to pass a variable name, directly, with CALL USING; at least, not one that would be better than a group data item.
What I did here is to create a JSON-like object using a STRING statement with a REPLACE statement to pass a name/value pair. I chose this method because there are many open source libraries that may be called from C to decode JSON objects. It also very flexible with regard to text lengths and the number of variables to be passed.
[Ed. Changed code to place all STRING statement data items in the REPLACE statement. Changed some names to reflect "name/value" pairings. Removed string length code to nested program.]
program-id. call-c.
data division.
working-storage section.
1 msg.
2 varA pic x(32) value "Message 1".
2 varB pic x(32) value "Message 2".
1 lengths binary.
2 len-1 pic 9(4).
2 len-2 pic 9(4).
replace
==name-1== by =="varA"==
==value-1== by ==varA(1:len-1)==
==name-2== by =="varB"==
==value-2== by ==varB(1:len-2)==
==newline== by ==x"0a"==
.
1 json-string pic x(256).
procedure division.
begin.
compute len-1 = function length (varA)
call "rtrim-len" using varA len-1
compute len-2 = function length (varB)
call "rtrim-len" using varB len-2
string
"{" newline
quote name-1 quote ": "
quote value-1 quote "," newline
quote name-2 quote ": "
quote value-2 quote newline
"}" x"00"
delimited size into json-string
call "c_prog" using
by reference json-string
stop run
.
program-id. rtrim-len.
data division.
linkage section.
1 str pic x(256).
1 str-len binary pic 9(4).
procedure division using str str-len.
begin.
perform varying str-len from str-len by -1
until str-len < 1 or str (str-len:1) not = space
continue
end-perform
exit program
.
end program rtrim-len.
end program call-c.
A COBOL program to substitute for a called C program.
program-id. "c_prog".
data division.
working-storage section.
1 json-string-count binary pic 9(4).
1 json-string-pos binary pic 9(4).
1 json-text-count binary pic 9(4).
1 json-text pic x(64).
linkage section.
1 json-string pic x(256).
procedure division using json-string.
begin.
move 0 to json-string-count
inspect json-string tallying
json-string-count for characters before x"00"
move 1 to json-string-pos
perform until json-string-pos > json-string-count
unstring json-string delimited x"0a" or x"00"
into json-text count json-text-count
pointer json-string-pos
display json-text (1:json-text-count)
end-perform
exit program
.
end program "c_prog".
Output:
{
"varA": "Message 1",
"varB": "Message 2"
}
If a TRIM function is available, the length calculations, data items, and nested program are not needed and the REPLACE statement becomes,
replace
==name-1== by =="varA"==
==value-1== by ==trim(varA)==
==name-2== by =="varB"==
==value-2== by ==trim(varB)==
==newline== by ==x"0a"==
.

Anychart tables: How to include thousand separators?

How can I put the text "100.000" in a table in Anychart? When I try to get the string "100.000" in, it is modified to "100".
For a working example see https://jsfiddle.net/Republiq/xcemvm9L/
table = anychart.standalones.table(2,2);
table.getCell(0,0).content("100.000");
table.container("container").draw();
If you want to use such number formatting for the whole table you can define numberLocale in the beginning. If the actual number is 100 and '.' - is a decimal separator and you want to show 3 zeros as decimals, put the following lines before creating the table:
anychart.format.locales.default.numberLocale.decimalsCount = 3;
anychart.format.locales.default.numberLocale.zeroFillDecimals = true;
And then put in the number as:
table.getCell(0,0).content(100);
If '.' - is a group separator and the actual number is 100000, put the following line:
anychart.format.locales.default.numberLocale.groupsSeparator = '.';
And then put in the number as:
table.getCell(0,0).content(100000);
If you want to use special format only for a single cell, we recommend you to use number formatter, which helps to configure all these options only for a single number. For example, it may looks like:
table = anychart.standalones.table(5,5);
table.getCell(0,0).content(anychart.format.number(100000, 3, ".", ","));
table.container("container").draw();
Also, you may learn more about this useful method and find examples in this article

Bash formatting text file into columns

I have a text file with data in it which is set up like a table, but separated with commas, eg:
Name, Age, FavColor, Address
Bob, 18, blue, 1 Smith Street
Julie, 17, yellow, 4 John Street
Firstly I have tried using a for loop, and placing each 'column' with all its values into a separate array.
eg/ 'nameArray' would contain bob, julie.
Here is the code from my actual script, there is 12 columns hence why c should not be greater than 12.
declare -A Array
for((c = 1; c <= 12; c++))
{
for((i = 1; i <= $total_lines; i++))
{
record=$(cat $FILE | awk -F "," 'NR=='$i'{print $'$c';exit}'| tr -d ,)
Array[$c,$i]=$record
}
}
From here I then use the 'printf' function to format each array and print them as columns. The issue with this is that I have more than 3 arrays, in my actual code they're all in the same 'printf' line. Which I don't like and I know it is a silly way to do it.
for ((i = 1; i <= $total_lines; i++))
{
printf "%0s %-10s %-10s...etc \n" "${Array[1,$i]}" "${Array[2,$i]}" "${Array[3,$i]}" ...etc
}
This does however give me the desired output, see image below:
I would like to figure out how to do this another way that doesn't require a massive print statement. Also the first time I call the for loop I get an error with 'awk'.
Any advice would be appreciated, I have looked through multiple threads and posts to try and find a suitable solution but haven't found something that would be useful.
Try the column command like
column -t -s','
This is what I can get quickly. See the man page for details.

What is ^X in in Linux?

For my homework assignment, I have to make sure that my output matches the solution output 100% or I don't get credit.
My output is:
hw05-data-10.txt : min = 5, max = 90, mean = 51.23, variance = 618.34
The solution output is:
hw05-data-10.txt : min = 5, max = 90, mean = 51.23, variance = 618.34
They look similar, however when I use diff mine.txt solution.txt, I get a difference.
I used cat -tev mine.txt and cat -tev solution.txt to find the difference, and I found that mine looks like:
hw05-data-10.txt^X : min = 5, max = 90, mean = 51.23, variance = 618.34$
and the solution looks like:
hw05-data-10.txt : min = 5, max = 90, mean = 51.23, variance = 618.34$
What is ^X? I've tried looking around but I can't find the answer. How can I remove this from my output? It's a C program.
^X is the CTRL-X character, made visible by the -v flag of cat.
If you want to get rid of it, you can pass the contents through something like:
tr -d '[:cntrl:]'
See the following transcript for an example (the ^X is inserted in bash with CTRL-VCTRL-X):
pax> echo '123^X456' | cat -v
123^X456
pax> echo '123^X456' | tr -d '[:cntrl:]' | cat -v
123456
If you want to remove it at the source rather than filtering after the event, you need to investigate the program that's creating your mine.txt file. It will be the one inserting the rogue character.
According to the man page of cat command,
-v flag: Display non-printing characters so they are visible. Control characters print as ^X for control-X; the delete character (octal 0177) prints as ^?. Non-ASCII characters (with the high bit set) are printed as M- (for meta) followed by the character for the low 7 bits
-e flag: Display non-printing characters (see the -v option), and display a dollar sign (`$') at the end of each line.
Therefore, the answer of ^X is a Control Character. To remove from output, remove the -v and -e flag from your cat command if possible. If can't, you have to investigate your C program: why it's generating this control character.
^X is ASCII CAN, cancel control character. Normally it is not used by the linux tty driver on output, so you will not see it when output to the terminal. If you use it on input, linux tty driver will interpret it as the STOP character and will send a SIGSTOP signal to the foreground process, causing it to stop execution until it receives a SIGSTART signal. The character you mention is probably somewhat inserted in the format string you used for the printf(3) call. (you have not shown your code snippet in the question)
The ^X character is also used in some window environments to Cut the selected text, so probably you inserted it at some undesired place in the program source code.

Select then parse using substring and cast gives different result from computation

I parsed a string which I use substring to get the last 11 i think numbers of characters. I accomplished that one but when I use cast and round it gives a different result from the manual computation I did.
Here's my query
SELECT round(cast(SUBSTRING('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 123,11)as float)/3600, 0, 1)
This gives me a result of 583. But when I try to manually compute using the computation below
282098820.9 / 3600
The result is
78360.7835
Is there something wrong with my query?
Thanks for the help.
The problem is with your SUBSTRING. It only returns 2098820.9 instead of 282098820.9. Try using RIGHT to extract the last 11 characters.
SELECT round(cast(right('351856040520298,241111;1R,141117003059,A,1420.4629N,12058.7028E,0.0,77,0.9,20000006;2R,141117003059,11,98.3,12.58,04.10,282098820.9', 11)as float)/3600, 0, 1)

Resources