Fortran, write 2d array into excel file - arrays

Hello I am having trouble writing a 2d array into an excel file.
I would like to have the values in a 10 x 10 format in excel, but this error keeps popping up I am not sure how to fix it.
code3.f90:40:12:
write(10, (b(i,j), j = 1,10)
1
Error: Syntax error in WRITE statement at (1)
x-10-104-223-3:cht
Here is my initial code
do i = 1, 10
do j = 1, 10
b(i, j) = 1
end do
end do
do i = 1,10
open( unit = 10, file = "test.csv")
write(10, (b(i,j), j = 1,10)
end do

You have the writesyntax wrong. You need to specify the format as a second part in the parenthesis, the output items go outside:
write(10,*) (b(i,j), j = 1,10)
Here, the format * is used to indicate list-directed output to "let the compiler decide on the exact output format" (depending on the output items).
As remarked by#cars10 in a comment: opening the file inside the loop is a bad idea. The code will probably exit with an error in the second iteration. Put the statement in front of the loop body.

Related

Lua: alternative to goto operator in Lua 5.1

Since World of Warcraft runs Lua 5.1, I'd like to know if there is a way to get what I need without using the goto operator. This is my code: this code reads the contents of a tooltip line by line, as if it were a text file
-- _G["UniScan_wpnTooltipTextLeft"..n]:GetText() return the text at n line
local line, n = nil, 1
while (_G["UniScan_wpnTooltipTextLeft"..n]:GetText()) do ::redo:: --As long as there are lines of text
if string.find("Durability", _G["UniScan_wpnTooltipTextLeft"..n]:GetText()) then
line = n - 1
break
end
n = n + 1
end
if not line then goto redo end
If, due to a UI loading bug, at the end of the loop the line variable is equal to nil , just repeat the loop so the line variable has a finite value. How can I achieve this in Lua 5.1?
How about:
while ((not line) or _G["UniScan_wpnTooltipTextLeft"..n]:GetText()) do
...

Need help skipping an empty json array with VBA

I am looping through different process ids to access the data within the json and copy it to my spreadsheet. However, some of the process id contain no data and every time my code gets to these empty arrays I get an error. I have tried different variations of if statements to skip this but still get an error. The empty array is at "expectedRateSetList"
I have tried different variations of if statements to skip this but still get an error. I've tried 'If J is null, if J is nothing, If J is empty" etc but I still can't get it to work. I've also tried "On error go to" but this hasn't worked.
`````````````
For l = 2 To last_row(tb, 2)
Set J = setJSON(url)
Set J = CallByName(J, "expectedRateSetList", VbGet) <---This is the array that is empty
If J Is Null Then GoTo next_log
On Error GoTo next_log
Set J = CallByName(J, "0", VbGet)
Set J = CallByName(J, "expectedRateList", VbGet)
next_log:
Next l
'json array looks like this:
{"processId":X,"expectedRateSetList":[],"warehouseId":"warehouseX"}
J is definitely not an array. J is an object, likely a Dictionary. You can check if a dictionary contains any items by querying its Count property - that removes the need for the line label and GoTo jump, at the cost of increased nesting (but then, the loop body should probably be refactored into its own procedure anyway):
If J.Count > 0 Then
' there are items
End If
Next
Note that CallByName(J, "MemberName", vbGet) can be replaced by late-bound J.MemberName calls - but then again assuming your parsing isn't hand-crafted and you are getting nested dictionaries, that would be J("MemberName"); the property you're actually invoking is the (default) Items property: J.Items("MemberName") is equivalent.

VBA nested for loop won't Run

Hi I'm working on a macro in VBA for excel. I have a nested for loop in my code as seen below. The second loop does not run; even a MsgBox() command within the second loop doesn't result in any action and the program just seems to skip over the nested loop with no reported errors.
In plain English this nested loop should:
1) Take the string from the i entry in the array categories_string (first for loop).
2) iterate through the 300+ rows in column "AE" in the excel file (second for loop, "length" is the length of the column of data in "AE")
3) look for string matches and add 1 to the corresponding entry in the categories_value array which is populated with variables set to 0 (the if statement).
For i = LBound(categories_string) To UBound(categories_string)
For p = 1 To p = length
If Worksheets("sheet1").Cells(p + 2, "AE").Value = categories_string(i) Then
categories_value(i) = categories_value(i) + 1
End If
Next
Next
Change
For p = 1 To p = length
to
For p = 1 To length
Should fix you right up.
You may want to consider Scott Cranner's comment as well, especially if your categories_string is large and length is large. Or, just do it for the practice

Loop Swift keeps crashing app

I am attempting to use a repeat-while loop in Swift. I am trying to use it to grab the items within an array and add it to a variable.
var finalEquation = ""
var arrayPoint = 0
repeat {
arrayPoint += 1
finalEquation = finalEquation + String(equation[arrayPoint])
} while (arrayPoint < equation.count)
So I have a variable called "arrayPoint", this increments by 1 each time the loop runs. So the problem I'm having I'm having is with the line:
finalEquation = finalEquation + String(equation[arrayPoint])
This line takes the item in the array, which is equal to arrayPoint and adds it that array item to a variable.
When ever I build and run the code, it compiles without any errors and does not stop me. Then whenever the loop gets activated, the application freezes, and in the output window, I get something saying.
fatal error: Index out of range
(lldb)
Then on that line of code that I mentioned earlier, it says: Thread 1: EXC_BAD_INSTRUCTION
First of all you might run this loop in UI thread which is wrong.
2nd you are increment the variable before fetch the value. Please move arrayPoint+=1 just after finalEquation line.
Putting the incrementing before your usage of the arrayPoint makes the range [1, count], but the array only has indices [0, count - 1].
You should move the increment to be the last thing in the repeat block.
var finalEquation = ""
var arrayPoint = 0
repeat {
finalEquation = finalEquation + String(equation[arrayPoint])
arrayPoint += 1
} while (arrayPoint < equation.count)
On a side note, arrayIndex would be a more appropriate name. The number used to reference an element in an array is called an "index".
Also, you should read about breakpoints, so that you can better debug off-by-one errors like this yourself.

Read in file to single array and then check if a number is in that array

I've got a .txt file set up in the following format:
7
8
9
10
What I'm trying to do, is read in the numbers from the file into an array and then check if a number I'm getting from a different function is contained within that array.
ismember(ruleFunc{x+1},memFunc}
I'm pretty sure that will check if the element from ruleFunc is in the array memFunc and return 1/0 if it is or isn't. But I can't get the ismember function to work properly because the method I'm using to populate the memFunc array is wrong.
Additionally, how am I able to add another number to the .txt file on a new line?
EDIT:
Here is how I am populating memFunc currently. It's also the same method that populates ruleFunc.
mem=fopen('WorkingMemory.txt');
tline = fgets(mem);
workMem = {};
index = 1;
while ischar(tline)
workMem{index} = str2num(tline);
tline = fgets(mem);
index = index + 1;
end
The function ismember returns a matrix that is 1 where the inputs are equal. (See the documenation for more information.) You might actually want something that returns a number, 1 or 0, depending on weather or not your number is in the matrix at all. I've included both options below.
% read in file
filename = 'my_data.txt';
fid = fopen(filename);
data = textscan(fid, '%d');
data = data{1};
fclose(fid);
% determine if number is in the file
number = 33;
ismember(data,number) %this returns an array
length(find(data == number)) > 0 % this returns 1 or 0
%write a line to existing file
fid2 = fopen(filename,'a');
newnumber = 100;
fprintf(fid2, '%d\n', newnumber);
fclose(fid2);
Now I see your updated answer. That code will read each line into a different cell of a cell array. You want all your data in a matrix. You could rearrange your cell array and put the data into a matrix or you could use textscan as described above.
In response to your comment, you can make an if statement like this:
if (length(find(data == number)) > 0)
'do something'
end
Maybe you are actually creating an array containing strings instead of numbers?
If it's not as simple as that, more information / code snippets would be useful. You mention the method populating memFunc may be wrong, maybe you could post that code?

Resources