Using parameter in Fortran90 format descriptor - format

How can I use parameters in format descriptor in Fortran90?
I want to make a matrix, say square(n*n), but I want to make it general. So, I declared a parameter like this: integer,parameter::n=3 (say n is 3 here)
Then after inputting the elements of the matrix in a do/implied do loop, I want to write it with the format function as follows:
format(ni5)
But it gives the error: Unexpected element 'N' in format string
Any simple way to solve this??

Little-known fact: format specifiers can be constructed at runtime as a character string. This means that you can programmatically define the format specifier and pass it to the write statement.
CHARACTER(len=30) fm
INTEGER :: i
i = 3
WRITE(fm,'(a,i1,a)')"(",i,"i5)"
WRITE(*,fm)1,2,3
In the above, we are generating the actual format specifier with the number of integers that you need to print for the given situation (i), then using that string as the format of the second write statement.
I very rarely see people use this trick, and its possible that it is not defined in the actual standard, but it does work in gfortran.

Related

Convert scalar from sif to hrf

I am looping over a set of scalars which contain quarterly sif values. I would like to convert them to hrf format and keep them stored in scalars.
However, I found that format %tq only accepts variables. Hence, the only workaround seems to i) convert the scalar to a variable ii) apply format %tq iii) convert the variable to a scalar.
Is there a more elegant and faster way to do this? (I am using Stata MP 15.1.)
You can have string scalars, so you can do this. I can't see why it would be useful, but that could be failure of imagination; you could enlighten us on why you want this.
. scalar foo = yq(2018, 4)
. scalar foo = string(scalar(foo), "%tq")
. scalar list
foo = 2018q4
What is quite different for scalars is that there is no sense whatsoever in which a display format is attached to or associated with a scalar. You can hold a numeric date or a string date in a scalar, but those are the only choices. You can't have a numeric value with a format on the side that Stata will use for display when suitable. You found that out when you attempted to format a scalar.
Goodness knows whether this is faster (than what?) or more elegant (who decides?). The major difference is that a variable manifestly can contain many dates and a changed format made just once with format can apply to them consistently, whereas changing how you show a bunch of scalars requires a loop every time you do it so far as I can see. Further, it follows from above that you might need to keep two sets of scalars, one numeric for calculation and one string for display.
I've used date constants and typically found that either I use them directly (subtracting 2000 as base doesn't requiring putting it into anything) or I use local macros to hold them. But I can't see anything wrong with using scalars, except possibly indirection.

Explanation of trailing character %

In an ancient PowerBasic file, I found this in the code:
%AppendRec= 1% '^a Write/Append Btrieve record to named file
%PrtBar= 2% '^b Print a Bar Code
My question deals with the numbers after the = sign. I assume the trailing % has a meaning, but I can't figure out what that meaning is.
I know that in QB, % denotes an Integer type but that normally leads the variable as shown at the beginning of the code lines. The trailing % has me confused.
It is used to specify the type of the constant, so you don't have e.g. "1" evaluate to a float and then have to be converted to an int.
This page shows you the defaults that PB uses if you don't explicitly specify the type of constants.
Like Charles said it defines the type as int. Prefixing it with % like the variable on the left is defining a numeric equate/constant. The page link is old now though and their new help is at PowerBasicHelp
Look for Numeric Equates and Data Types.

NumberFormatException while converting from string to integer in servlet

I know ths is a common question here. I have read the solutions and modified the code then also i am not getting the solution so I have posted my code here.
when I read a value from the text box and parse the value into int from String in servlet, why does it show NumberFormatException?
String ph=request.getParameter("phone");
int phone=Integer.parseInt(ph.trim());// exception is generated here
You can't have any characters or symbols in an int AND the maximum size of an int is 2,147,483,647 which means the phone number 555 555 5555 would be to large to store in an int.
Change the parse to a long and it should fix your problem
A good rule to follow is do not store any number like SSN or Phone numbers's in numerical primitives. You want to leave the numerical primitives for values you plan on doing something math related. Keep them as strings if at all possible.
Because the value you are feeding it is not a valid integer. Make sure it doesn't have strange characters in it.
9835008199 is higher then MAXINT, so that's probaly it.
The docs tell me:
Throws NumberFormatException - if the string does not contain a parsable integer.
On another note: do not store phone numbers as integers. You will get in trouble (you are in fact already). Rule of thumb: if you do not want to do math with a number, it's not an int, but a string.
Use isNumber function to check whether it is a valid number.

Jmeter - how to add prefix to a counter. Similar to a random variable

Currently in the random variable I can put a prefix or suffix in the output format of the variable. However this nice feature is not available for a simple counter controller. Concatenating string+${counter} every time when i use the variable is not a good option form me since i do this a lot.
Is there any way to achieve prefix+counter in a way random variable do this?
Thanks.
Are you talking about Counter Config Element ?
If so it is possible using Number Format attribute:
http://jmeter.apache.org/usermanual/component_reference.html#Counter
See:
Format Optional format, e.g. 000 will format as 001, 002 etc.
This is passed to DecimalFormat, so any valid formats can be used.
If there is a problem interpreting the format, then it is ignored.
[The default format is generated using Long.toString()]
On 'Number format' field of Counter, write your pattern with 0...0 represents for value count up.
Example:
'Number format' pattern: prefix_000_suffix
Real value: prefix_001_suffix, prefix_002_suffix, prefix_003_suffix,...
Hope this helps!

Fortran FORMAT statement

I am trying to read in the following in line:
110134458.602 7 20957861.900
My format line is currently as follows:
READ(7,110,END=999) L1,C1,D1
write(*,*) L1,C1,D1
110 FORMAT(F14.3,1x,F1.0,2x,F14.3)
However the output I am receiving is:
110134458.60200000 7.0000000000000000 20957861.899999999
Why do I have so many decimal places and why is the final value not match?
Thank you!
It looks that you are reading the values correctly. With list-directed IO for the output the compiler will typically use the maximum number of digits for the numeric type. Not all decimal values will have exact finite precision binary equivalents -- that is probably what you are seeing for the 3rd number. If you use a format statement for the output, specifying fewer digits, the value will get rounded and appear correct.

Resources