Issues with comparing Strings in Fortran? - string

I am trying to compare a string with a set of values read into a character array. But the comparison doesn't work the way it is expected to.I have attached the code below for the check.
PROGRAM MWE
CHARACTER(LEN=80) :: FILENAME,HEADER,REMARKS
CHARACTER*6,ALLOCATABLE :: RESNAME(:)
INTEGER :: NHEADER,NATOMS,I
ALLOCATE(RESNAME(10))
FILENAME = "./thy_thy_wat.psf"
OPEN(10,FILE=FILENAME,STATUS='OLD',FORM='FORMATTED')
READ(10,'(A4)') HEADER
READ(10,'(B10)')
READ(10,'(T6,I4)') NHEADER
DO I=1,NHEADER
READ(10,'(A50)') REMARKS
END DO
READ(10,'(B10)')
READ(10,'(T5,I4)') NATOMS
DO I=1,10
READ(10,'(T19,A5)') RESNAME(I)
END DO
WRITE(*,*) RESNAME
IF (RESNAME(1) == "THY") THEN
WRITE(*,*) "YES"
END IF
END PROGRAM MWE
I can visually check that the values match, but the code doesn't give that. Any help would be appreciated
https://drive.google.com/file/d/1WHOv65Lbb3PRSuE2YwMAZk1L-uqYDhXb/view?usp=sharing is the link to the file that is read in using the above code.
EDIT: I was able to solve the issue by playing around with the tabs. Thanks for the help everyone.

Related

VBA - cycle through number formats

I have put together the following code so that when I repeatedly press a shortcut, the number in a cell automatically switches formats between:
2020
2020A
2020E
2020A/E
2,020%
2,020x
2,020bps
Here's the code that isn't currently working:
Sub Number_Format_Cycle()
Dim x, myFormats
myFormats = Array("###0;(###0);-", "###0A;(###0)A;-", "###0E;(###0E);-", "###0A/E;(###0A/E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0bps;(#,##0)bps;0bps")
x = Application.Match(ActiveCell.NumberFormat, myFormats, False)
If IsError(x) Then x = 0
Selection.NumberFormat = myFormats((x Mod (UBound(myFormats) + 1)))
End Sub
Any help would be much appreciated
Cheers,
Thomas
You have to use a backslash (\) in order to add character literals to make them showing up exactly as typed.
Changing the myFormats assignment to the following codeline worked for me:
myFormats = Array("###0;(###0);-", "###0\A;(###0)\A;-", "###0\E;(###0\E);-", "###0\A\/\E;(###0\A\/\E);-", "#,##0.0%;(#,##0.0)%;0.0%", "#,##0.0x;(#,##0.0)x;0.0x", "#,##0\bp\s;(#,##0)\bp\s;0\bp\s")
Re-Check each number format you intend to add after a test setting against the array elements.
Caveat: I have to admit that I can't explain the last \bp\s format, nevertheless it works.

Use String text as Code in Visual Basic for Excel

For various reasons, I need to concatenate a text of the form [NAME].Value by changing the value of NAME to an inputbox entry.
Something like this:
Sub example()
data_in = InputBox("Give me something: ")
mystring1 = "[" & data_in & "].Value"
a = Evaluate(mystring1) 'I know this is wrong, but I don't know how to do so.
End Sub
I know it can be done in other ways, and the example in which I want to use this code is not exactly this one, and while it can be done in several ways here, in the original code it can only be done this way.
I want, based on the input in the imputbox, to concatenate the string in whatever way, and subsequently cast that string as code to store the value in another variable, to be used later in the code.
I am not able to get VBA to read the string text as code. I have seen that there is a way that consists of creating a macro from this first macro, execute it, and then delete the recently created macro. The problem with this solution is that doing that I can't save the variable when returning to the initial macro (I don't want to use global variables).
Surely there must be a way?
Thank you very much.
EDIT: The code above returns Error 2015
In order to use a string as if it was code, you can use the evaluate function (exists in most languages)
The official documentation mentions this example:
[a1].Value = 25
Evaluate("A1").Value = 25
trigVariable = [SIN(45)]
trigVariable = Evaluate("SIN(45)")
Set firstCellInSheet = Workbooks("BOOK1.XLS").Sheets(4).[A1]
Set firstCellInSheet = _
Workbooks("BOOK1.XLS").Sheets(4).Evaluate("A1")
I have figured out the easiest way to do it, sorry for posting the question so soon.
Thanks to #Andreas for the solution. I'll write it here in case than could be useful to someone.
Sub example()
data_in = InputBox("Give me something: ")
a = Range(data_in).Value
Debug.Print a
End Sub
In the end the simplest thing is the last thing you try...

Multiple space delimiter split string

I have a string like
string = "computer prog <5spaces> data mining <5spaces> oops concept"
As we can see clearly computer prog, data mining etc., are one continuous string and the delimiter is 5 spaces between the strings " ".
I need to split based on this in vb.net - so far I tried regex.split which works but results in giving 2 empty strings additionally and it's tedious to remove those additional strings.
I also tried using the string.split method but again it's taking even single white space also delimiters.
Below are the tried options:
regex.split
string.split
None give me the required result. I am not sure what I need to use. I even tried the option of stringsplitoption.removesapceentry (something like that) to get the desired result inside the split method, but none worked.
Dim array_keyskills As String() = res.Split(" ".ToCharArray,StringSplitOptions.RemoveEmptyEntries)
system.Windows.MessageBox.Show(array_keyskills(2) & array_keyskills.Length & " key skills") 'Display
The following short program:
Module Module1
Sub Main()
Dim s = "computer prog data mining oops concept"
Dim parts = s.Split({" "}, StringSplitOptions.None)
For Each p In parts
Console.WriteLine(p)
Next
Console.ReadLine()
End Sub
End Module
outputs:
computer prog
data mining
oops concept
If your data does not work that way then you should examine it to find which whitespace characters are in it which appear to be spaces but are not.
This did the trick:
array_keyskills = System.Text.RegularExpressions.Regex.Split(res," ").Where(
Function(s) Not String.IsNullOrWhitespace(s)
).ToArray()

Limit text to allowed characters only - (not by enumerating the wrong characters) | VBA

I would like to limit certain textboxes to accept only [A-Za-z]
I hope, a counterpart to Like exists.
With Like I would have to make a long list of not allowed characters to be able to filter.
Not MyString like [?;!°%/=....]
I can think of a solution in the form of:
For Counter = 1 To Len(MyString)
if Mid(MyString, Counter, 1) Like "*[a-z]*" = false then
MsgBox "String contains bad characters"
exit sub
end if
next
... but is there a more sophisticated 1liner solution ?
Until then, I have created a function to make it "Oneliner":
Function isPureString(myText As String) As Boolean
Dim i As Integer
isPureString = True
For i = 1 To Len(myText)
If Mid(myText, i, 1) Like "*[a-zA-Z_íéáűúőöüóÓÜÖÚŐŰÁÉÍ]*" = False Then
isPureString = False
End If
Next
End Function
If i add 1 more parameter, its also possible to define the allowed characters upon calling the function.
Ok, it seems my question was a bit of a duplicate, even though that did not pop in my search results.
So credits for #QHarr for posting the link.
The solution I can forge from that idea for my "oneliner" is:
If myText Like WorksheetFunction.Rept("[a-zA-Z]", Len(myText))=false then 'do something.
Using .rept is inspiringly clever and elegant in my oppinion.
So what is does: Multiplies the search criteria for each charater instead of looping through the characters.
EDIT:
In an overaboundance of nice and elegant solutions, the most recent leader is:
If not myText Like "*[!A-Za-z]*" then '... do something
Statistics update:
I have tested the last 3 solutions' performance:
I have pasted # in the below text strin at the beginning, at the end or nowhere.
The criteria were: "*[a-zA-Z \S.,]*"
For 100000 repetitions
text = "This will be a very Long text, with one unwanted in the middle, to be able to test the difference in performance of the approaches."
1.) Using the [!...] -> 30ms with error, 80ms if no error
2.) Using .Rept -> around 1800ms for all cases
3.) Using characterLoop+Mid -> around 3000ms if no error / 40-80ms ms if early error

Stop VBA Evaluate from calling target function twice

I am having trouble getting VBA's Evaluate() function to only execute once; it seems to always run twice. For instance, consider the trivial example below. If we run the RunEval() subroutine, it will call the EvalTest() function twice. This can be seen by the two different random numbers that get printed in the immediate window. The behavior would be the same if we were calling another subroutine with Evaluate instead of a function. Can someone explain how I can get Evaluate to execute the target function once instead of twice? Thank you.
Sub RunEval()
Evaluate "EvalTest()"
End Sub
Public Function EvalTest()
Debug.Print Rnd()
End Function
This bug only seems to happen with UDFs, not with built-in functions.
You can bypass it by adding an expression:
Sub RunEval()
ActiveSheet.Evaluate "0+EvalTest()"
End Sub
But there are also a number of other limitations with Evaluate, documented here
http://www.decisionmodels.com/calcsecretsh.htm
I don't know of a way to stop it, but you can at least recognize when it is happening most of the time. That could be useful if your computation is time consuming or has side effects that you don't want to have happen twice and you want to short circuit it.
(EDIT: Charles Williams actually has an answer to your specific quesion. My answer could still be useful when you don't know what data type you might be getting back, or when you expect to get something like an array or a range.)
If you use the Application.Caller property within a routine called as a result of a call to Application.Evaluate, you'll see that one of the calls appears to come from the upper left cell of of the actual range the Evaluate call is made from, and one from cell $A$1 of the sheet that range is on. If you call Application.Evaluate from the immediate window, like you would call your example Sub, one call appears to come from the upper left cell of the currently selected range and one from cell $A$1 of the current worksheet. I'm pretty sure it's the first call that's the $A$1 in both cases. (I'd test that if it matters.)
However, only one value will ever be returned from Application.Evaluate. I'm pretty sure it's the one from the second eval. (I'd test that too.)
Obviously, this won't work with calls made from the actual cell $A$1.
(As for me, I would love to know why the double evaluation happens. I would also love to know why the evaluator is exposed at all. Anyone?)
EDIT: I asked on StackOverflow here: Why is Excel's 'Evaluate' method a general expression evaluator?
I hope this helps, although it doesn't directly answer your question.
I did a quick search and found that others have reported similar behavior and other odd bugs with Application.Evaluate (see KB823604 and this). This is probably not high on Microsoft's list to fix since it has been seen at least since Excel 2002. That knowledge base article gives a workaround that may work in your case too - put the expression to evaluate in a worksheet and then get the value from that, like this:
Sub RunEval()
Dim d As Double
Range("A1").Formula = "=EvalTest()"
d = Range("A1").Value
Range("A1").Clear
Debug.Print d
End Sub
Public Function EvalTest() As Double
Dim d As Double
d = Rnd()
Debug.Print d
EvalTest = d + 1
End Function
I modified your example to also return the random value from the function. This prints the value a second time but with the one added so the second print comes from the first subroutine. You could write a support routine to do this for any expression.
I face the same problem, after investigation i found the function called twice because i have drop down list and the value used in a user defined function.
working around by the code bellow, put the code in ThisWorkbook
Private Sub Workbook_Open()
'set the calculation to manual to stop calculation when dropdownlist updeated and again calculate for the UDF
Application.Calculation = xlCalculationManual
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Source As Range)
'calculte only when the sheet changed
Calculate
End Sub
It looks like Application.Evaluate evaluates always twice, while ActiveSheet.Evaluate evaluates once if it is an expression.
When the object is not specified Evaluate is equivalent to Application.Evaluate.
Typing [expression] is equivalent to Application.Evaluate("expression").
So the solution is to add ActiveSheet and to make that an expression by adding zero:
ActiveSheet.Evaluate("EvalTest+0")
After seeing there is no proper way to work around this problem, I solved it by the following:
Dim RunEval as boolean
Sub RunEval()
RunEval = True
Evaluate "EvalTest()"
End Sub
Public Function EvalTest()
if RunEval = true then
Debug.Print Rnd()
RunEval = False
end if
End Function
problem solved everyone.

Resources