Writing a vector to disk in fortran [duplicate] - io

This question already has answers here:
Controlling newlines when writing out arrays in Fortran
(1 answer)
How to write matrix row by row with implied do loop? [duplicate]
(1 answer)
Closed 1 year ago.
I am having some issues with writing efficiently a vector in fortran to disk.
Here's a minimal working example:
integer :: ind
double precision, allocatable :: c_reshape
allocate(c_reshape(576000))
do ind = 1,576000
c_reshape(ind) = ind
end do
OPEN(UNIT=25,FILE='c_res',STATUS='replace',ACTION='write')
WRITE(25,*) c_reshape(:)
CLOSE(UNIT=25)
When I open the output file in notepad this is what I see:
Why does Fortran save it in 3 columns? Anyway of saving it in a single column without writing a loop?

Related

How to treat given integer as binary values? [duplicate]

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 2 years ago.
I have a integer value i.e. 1010110. How to treat this value as binary? So that i can find the integer value of that binary value.
You can pass a base parameter (2 in this case) to the int function:
s = "1010110"
i = int(s, 2)
# 86

How to check if two strings are equal in fortran? [duplicate]

This question already has an answer here:
Comparing two strings in Fortran
(1 answer)
Closed 3 years ago.
I want to check if two strings are equal and do some work.
character(len = 50) :: x, y ,z
x="amin"
y="amin"
if(llt(x, y)) then
z=x
end if
I wrote this but it just checks first character in my string.
How can i handle it?
In Fortran two strings can be compared via relational operations i.e. <, >, ==, /=, etc..
So in your case:
if ( x == y ) then
z = x
end if
The llt() function does something completly different:
The llt() function tests whether a string is lexically less than another string based on the ordering of the ASCII collating sequence.

Apply f-string interpolation to a string [duplicate]

This question already has answers here:
How to postpone/defer the evaluation of f-strings?
(14 answers)
Closed 3 years ago.
Is there a way to achieve applying f-strings interpolation dynamically on a string?
For example given the string a = '{len([1])}'
a.interpolate() to give '1'
Try this:
a = f'{len([1])}'
print(a)
#1

How to store numbers of a string in a list in python [duplicate]

This question already has answers here:
Convert string (without any separator) to list
(9 answers)
Closed 7 years ago.
I have a string s="12345678"
I need to store these numbers in a list like below format.
s=['1','2','3','4','5','6','7','8']
So can anybody help me how to do this.
Thanks,
Try this:
s = "12345678"
a = [c for c in s]

Convert string to table in lua [duplicate]

This question already has answers here:
String to Table in Lua
(2 answers)
Closed 9 years ago.
I am newbie in lua. I need to convert following string to lua table. How could I do this?
str = "{a=1, b=2, c={d=3,e=4} }"
I want to convert this string to lua table, so that I can access it like this:
print(str['a']) -- Output : 1
print(str['c']['d']) -- Output : 3
You could simply add a str = to the beginning of the string and let the interpreter load that string as a chunk for you. Note that loadstring doesn't run the chunk but returns a function. So you add () to call that function right away and actually execute the code:
loadstring("str = "..str)()
This would do the same thing:
str = loadstring("return "..str)()
If you don't generate the string yourself, that can be dangerous though (because any code would be executed). In that case, you might want to parse the string manually, to make sure that it's actually a table and contains no bad function calls.

Resources