non-advancing WRITE...but not intentionally? - file-io

I have a brief snippet of code from a Fortran 95 program that should, in theory, spit out my results into some text files. It would be convenient, for readability if nothing else, for the data to be written in columns (so, one column for variable X, one for Y, etc.). In the first set of WRITE commands below (i.e., those associated with the first OPEN command), the idea is to have a text identifier for the user to read, followed by a numeric value. In the second write command, I just dump out four columns of data, each specific to a given variable.
open(unit=10,file='outs_sum.txt',status='replace')
do i=1,1
write(10,'(a12,f5.4)') 'Min. sigma: ',sigma_low
end do
do i=1,1
write(10,'(a12,f5.4)') 'Max. sigma: ',sigma_high
end do
write(10,'(a11,f5.4)') 'Sigma inc: ',0.005
write(10,*) '# of sigmas: ',ii
write(10,'(a9,f5.1)') 'Min. DL: ',dl_low
write(10,'(a9,f5.1)') 'Max. DL: ',dl_high
write(10,*) 'DL inc: ',1
write(10,*) '# of DLs: ',i
write(10,*) 'Total rows: ',(i*ii)
close(unit=10)
open(unit=10,file='outs.txt',status='replace')
do i=1,dls
do ii=1,sigmas
write(10,*) gtow_out(i,ii),ctsig_out(i,ii),sigout(i,ii),dlout(i,ii)
end do
end do
close(unit=10)
However, what happens on the output side is this: the latter WRITE does exactly what I'd expect and spits out the data in column form...but the former insists on writing everything to the same row. At least when I open things in Notepad. If I use GVIM, it looks as it should.
Why does the first set of WRITE commands write to the same row, and how can I force it to insert a line break after each command instead? Alternatively, is Notepad just showing me something that isn't really there?

Related

In MS Access VBA get Number of Processor Cores

I need to get the number of processor cores available on a computer programmatically from within MS Access. As an example, the computer I work from most frequently has one processor with 6 cores. I want to grab the number '6' through VBA.
Thus far, I have found two ways to find this information through CMD. (1) I can execute the line echo %NUMBER OF PROCESSORS% and the result is 6 (simple and clean, I like it). (2) I have also tried wmic cpu get numberorcores, but the result of that prompt is as follows:
NumberOfCores
6
I intend to pipe the output to and read from the clipboard. The reason I use the clipboard is to avoid creating, reading, and deleting little text files of data. Prompt (2) works, I can successfully pipe the output to the clipboard and read it into a variable in VBA, but it's messy and I would have to parse the result to get the information I need. I would much prefer using prompt (1), but it's not working and the problem seems to be echo. I have tried using shell() and CreateObject(WScript.Shell).Run without success. The strings I have used to try to execute the echo prompt are as follows:
str = "echo %NUMBER OF PROCESSORS% | clip"
str = "cmd ""echo %NUMBER OF PROCESSORS% | clip"""
So, is there a way to successfully send an echo prompt to CMD through VBA and get a result?
Alternatively, is there a different way in VBA to get the number of cores?
TIA!
Why not keep it simple like this:
Dim result As Variant
result = Environ("NUMBER_OF_PROCESSORS")
Debug.Print "Number of processors is " & result

How do I write a macro in WoW to yell one of 2 statements?

I want to write a wow macro that has the character say one of 2 statements and I want to be able to choose which statement. I'm fine with either having a sequence (alternates between each message), having the game detect if the player has a buff or not, or having the game detect if a modifier key (such as alt, shift, ctrl) is being held. If I wanted to create a macro for a spell, I would just use
/cast [mod] spell1
/cast [nomod] spell2
but this does not work with a /s command (I've tried). I've tried diving into basic Lua code, but have not been able to make it work. Here's what I have tried using a Lua if then else command that tests to see if the player has a buff or not (I used "Travel Form" as a placeholder buff on my druid)
/run if (UnitAura("player", "Travel Form"))
then SendChatMessage("Statement1","say")
else SendChatMessage("Statement2","say")
end
Thanks in advance for any help you can provide!
Here are two examples with using modifier keys. They are a bit condensed because I like my macros short.
https://wowpedia.fandom.com/wiki/API_IsModifierKeyDown
/run SendChatMessage(IsModifierKeyDown() and "Statement1" or "Statement2")
https://wowpedia.fandom.com/wiki/API_SecureCmdOptionParse
/run SendChatMessage(SecureCmdOptionParse("[mod] Statement1; Statement2"))
As for checking if you have e.g. a buff (by name), you need to iterate the indices for UnitAura() or use GetPlayerAuraBySpellID (less compact example)
/run if GetPlayerAuraBySpellID(783) then SendChatMessage("Statement1") else SendChatMessage("Statement2") end

To grep contents from a CSV/Text File using Autohotkey(AHK) Script

Can anyone please help me in writing a script in AHK based on below requirement.
Requirement:
I have a CSV/TXT file in my windows environment which contains 20,000+ records in below format.
So, when I run the script it should prompt a InputBox to enter an instance name.
Example : If i enter Instance4 , it should display result in MsgBox as ServerName4
Sample Format:
ServerName1,ServerIP,Instance1,Type
ServerName2,ServerIP,Instance2,Type
ServerName3,ServerIP,Instance3,Type
ServerName4,ServerIP,Instance4,Type
ServerName5,ServerIP,Instance5,Type
.
.
.
Also as the CSV/TXT file contains large no of records , pls also consider the best way to avoid delay in fetching the results.
Please post your code, or at least show what you've already done.
You can use a Parsing Loop with CSV as the delimiter, and make a variable for each 'Instance' who's value is that of the current row's 'ServerName'.
The steps are to first FileRead the data from the file, then Loop, Parse like so:
Loop, Parse, data, CSV
{
; Parses row by row, then column by column in each row.
; A_LoopField // Current value
; A_Index // Current loop's index
; Write a script that makes a variable named with the current value of column 3, and give it the value of column 1
}
After that, you can make a Goto loop that spams InputBox and following a command that prints out the needed variable using the MsgBox command, like so:
MsgBox % %input%

Print and write in one line?

Is it possible to print something in the screen and, at the same time, that what is being printed is also written in a file?
Right now, I have something like this:
print *, root1, root2
open(unit=10,file='result.txt'
write(10,*), root1, root2
close(10)
I feel like I'm wasting lines and making the code longer that it should be. I actually want to print/write much more lines that these, so that's why I'm looking for a cleaner way to do it.
Writing to standard output and writing to file are two different things, so you will always need separate instructions. But you don't have to open and close the file for every line you write.
Honestly, I don't think it's that much more of an effort:
open(unit=10, file='result.txt', status='replace', form='formatted')
....
write( *, *) "Here comes the data"
write(10, *) "Here comes the data"
....
write( *, *) root1, root2
write(10, *) root1, root2
....
close(10)
this is only one line more than what you would have to do anyway per write statement.
If you really think that you have too many write statements in your code, here are a few ideas that you might try:
If you are running on a Linux or Unix system (including MacOS), you can write a program that only writes to standard out, and pipe the output into a file, like this:
$ ./my_program | tee result.txt
This will both output the data to the screen, and write it into the file result.txt
Or you could write the output to a file in the program, and 'follow' the file externally:
$ ./my_program &
$ tail -f result.txt
I just had another idea: If you really often have the issue that you need to write data to both the screen and the file, you can place that into a subroutine:
program my_program
implicit none
real :: root1, root2, root3
....
open(10, 'result.txt', status='replace', form='formatted')
....
call write_output((/ root1, root2 /))
....
call write_output((/ root1, root2, root3 /))
....
call write_output((/ root1, root2 /))
....
close(10)
....
contains
subroutine write_output(a)
real, dimension(:), intent(in) :: a
write( *, *) a
write(10, *) a
end subroutine write_output
end program my_program
I am passing the values to be written here as an array because that gives you more flexibility in the number of variables that you might want to print. On the other hand, you can only use this subroutine to write real values, for others (integer, character, etc) or combinations thereof you'd need to still have two write statements, or write other specific 'write to both' routines.

Why doesn't io:write() write to the output file?

I'm writing a short script in Lua to replicate Search/Replace functionality. The goal is to enter a search term and a replacement term, and it will comb through all the files of a given extension (not input-determined yet) and replace the Search term with the Replacement term.
Everything seems to do what it's supposed to, except the files are not actually written to. My Lua interpreter (compiled by myself in Pelles-C) does not throw any errors or exit abnormally; the script completes as if it worked.
At first I didn't have i:flush(), but I added it after reading that it is supposed to save any written data to the file (see LUA docs). It didn't change anything, and files are still not written to.
I think it might have something to do with how I'm opening the file to edit it, since the "w" option works (but overwrites everything in my test files).
Source:
io.write("Enter your search term:")
term = io.read()
io.write("Enter your replace term:")
replacement = io.read()
io.stdin:read()
t = {}
for z in io.popen('dir /b /a-d'):lines() do
if string.match(string.lower(z), "%.txt$") then
print(z)
table.insert(t, z)
end
end
print("Second loop")
for _, w in pairs(t) do
print(w)
i = io.open(w, "r+")
print(i)
--i:seek("set", 6)
--i:write("cheese")
--i:flush()
for y in i:lines() do
print(y)
p, count = string.gsub(y, term, replacement, 1)
print(p)
i:write(p)
i:flush()
io.stdin:read()
end
i:close()
end
This is the output I get (which is what I want to happen), but in reality isn't being written to the file:
There was one time where it wrote output to a file, but it only output to one file and after that write my script crashed with the message: No error. The line number was at the for y in i:lines() do line, but I don't know why it broke there. I've noticed file:lines() will break if the file itself has nothing in it and give an odd/gibberish error, but there are things in my text files.
Edit1
I tried do this in my for loop:
for y in i:lines() do
print(y)
p, count = string.gsub(y, term, replacement, 1)
print(p)
i:write(p)
i:seek("set", 3) --New
i:write("TESTESTTEST") --New
i:flush()
io.stdin:read()
end
in order to see if I could force it to write regular text. It does but then it crashes with No error and still doesn't write the replacement string (just TESTESTTEST). I don't know what the problem could be.
I guess, one can't write to file while traversing its lines
for y in i:lines() do
i:write(p)
i:flush()
end