This question already has answers here:
gfortran does not allow character arrays with varying component lengths
(2 answers)
Closed last year.
I am encountering a problem filling an array of character strings.
The problem occurs when using the [] declaration to assign elements
in an array. When I assign the array element my element, everything
is fine.
This is the output
Expected Output: redgreenyellowblue
Actual output: redgreyelblue
Here is the code
Character (len=*), Intent(in), Optional :: a2, &
a3, a4, a5, a6, a7, a8
Character (len=65), Allocatable :: str(:)
Character (len=65) :: b(512)
a1 = "red"
a2 = "green"
a3 = "yellow"
a4 = "blue"
a5 = "magenta"
a6 = "cyan"
a7 = "white"
Allocate (str(7))
str = [a1, a2, a3, a4, a5, a6, a7]
str(4) = a4
str(5) = a5
str(6) = a6
str(7) = a7
Write (*,*) "Expected output: ", Trim(a1), Trim(a2), Trim(a3), Trim(a4)
Write (*,*) "Actual output: ", Trim(str(1)), Trim(str(2)), Trim(str(3)), Trim(str(4))
I have conformed that the following works as expected
Character (len=65), Allocatable :: str(:)
Character (len=65) :: a1, a2, a3, a4, a5, a6, a7
a1 = "red" ; a2 = "green"; a3 = "yellow"; a4 = "blue"
a5 = "magenta"; a6 = "cyan"; a7 ="white"
Allocate (str(7))
str = [a1,a2,a3,a4,a5,a6,a7]
Write (*,*) Trim(a1), Trim(a2), Trim(a3), Trim(a4), Trim(a5)
Write (*,*) Trim(str(1)), Trim(str(2)), Trim(str(3)), Trim(str(4)), Trim(str(5))
Deallocate (str)
The solution is to include the character length parameter in the constructor
str = [Character(len=65) :: a1,a2,a3,a4,a5,a6,a7]
The solution is to include the character length parameter in the array constructor
str = [Character(len=65) :: a1,a2,a3,a4,a5,a6,a7]
Related
I have a Google spreadsheet with a tab called 'Updates'. In a separate tab, I want to use drop-down menus (contained in cells B3, B4 and B5 in the code below) to filter and subsequently view the data in the 'Updates' tab according to its text in columns B, C and D.
I have written the following code. Basically I want to be able to filter the data according to selections made in all 3 drop down menus (B3, B4 and B5), or just in two of them (e.g. B3 and B4, but B5 is left blank), or just in one of them (e.g. B3, and B4 and B5 are left blank).
=query(Updates!A1:E, " select * (where B = '"&B3&"' AND C = '"&B4&"' AND D = '"&B5&"') OR (where B = '"&B3&"' OR C = '"&B4&"' OR D = '"&B5&"') OR (where (B = '"&B3&"' OR C = '"&B4&"') AND D = '"&B5&"') OR (where B = '"&B3&"' AND (C = '"&B4&"' OR D = '"&B5&"') ) OR (where C = '"&B4&"' AND (B = '"&B3&"' OR D = '"&B5&"') ")
The AND and OR functions work separately in their own query functions, but when I combine them together I get the following error message:
Error
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "(" "( "" at line 1, column 11. Was expecting one of: "where" ... "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ...
Any help? Thanks!
you can use where only once in one QUERY. try:
=QUERY(Updates!A1:E,
"where 9=9 "&
IF(B3="",," and B = '"&B3&"'")&
IF(B4="",," and C = '"&B4&"'")&
IF(B5="",," and D = '"&B5&"'"), )
In F# I have a very long code file like
let rec f1 a1 a2 a3 a4 a5 .. aN =
...
and f2 a1 a2 a3 a4 a5 ... aN =
...
and f3 a1 a2 a3 a4 a5 ... aN =
...
and f40 a1 a2 a3 a4 a5 ... aN =
...
In other words there are many mutually recursive functions, each with a lot of parameters.
Now the problem is that the file is 17000 lines long, and Visual Studio has become too slow. (For example, I can't hover the mouse over an item to see its type; if I press the dot, there is no completion, and so on)
Therefore I need to split the file into many smaller files. But I can't see a mechanical and easy way to do it.
Could you please give me an advice? I would like a mechanical way to split the code into multiple files, which does not involve writing the type of all functions (preserve type inference).
In the meantime I found a solution (tested):
This is the initial situation (simplified to only have four functions, but in reality they are many more):
let rec f1 a b c =
f2 a b c;
f3 a b c;
f4 a b c;
and f2 a b c =
f1 a b c;
f3 a b c
f4 a b c
and f3 a b c =
f1 a b c;
f2 a b c
f4 a b c
and f4 a b c =
f1 a b c;
f2 a b c
f3 a b c
And here is the solution:
Suppose you decide to move f3 to another file. Then you can split the file above in two files as follows:
FILE 1
======
let callRef mf =
match !mf with
| None -> failwith "function ref is none"
| Some f -> f
let r_f3 = ref None;
let rec f1 a1 a2 a3 =
f2 a b c;
callRef r_f3 a1 b1 c1;
f4 a1 b1 c1;
and f2 a1 a2 a3 =
f1 a b c;
callRef r_f3 a1 b1 c1;
f4 a1 b1 c1;
and f4 a1 a2 a3 =
f1 a b c;
f2 a1 b1 c1;
callRef r_f3 a1 b1 c1;
FILE 2
======
let f3 a1 a2 a3 =
f1 a b c;
f2 a1 b1 c1;
f4 an bn cn;
Then, in the main initialization function (which is in a third file), you need to do
r_f3 := Some f3;
And that's it.
Repeat the same strategy to move f1, f2 and f4 out of the first file.
Update: This solution works well for functions which return unit, but unfortunately for functions which return an actual type it forces you to specify the function type explicitely, e.g.
let (r_f3 : (t1 -> t2 -> t3 -> t4 -> t5) option ref) = ref None;
or you can do this:
let (r_f3 : 'a option ref) = ref None;
but you'll get a compiler warning.
I am emulating an array with simple function (Tab) and it doesn't work as expected. If I code it in SMT2, it works well, but in Z3py it doesn't work.
from z3 import *
A = BitVec('A', 8)
B1 = BitVec('B1', 8)
B2 = BitVec('B2', 8)
B3 = BitVec('B3', 8)
B4 = BitVec('B4', 8)
# Emulate Array
def Tab(N):
if N == 0x01: return B1
if N == 0x02: return B2
if N == 0x03: return B3
if N == 0x04: return B4
s = Solver()
s.add(A == 0x01)
s.add(Tab(A + 0x02) == 0x09 )
s.check()
m = s.model()
print (m)
print("Pos:", m.eval(A + 0x02))
print("Tab(3a):", m.eval(Tab(A + 0x02)))
print("Tab(3):", m.eval(Tab(0x03)))
print("B1: ", m[B1])
print("B2: ", m[B2])
print("B3: ", m[B3])
print("B4: ", m[B4])
print("B3n:", m.eval(B3))
Output:
[B1 = 9, A = 1] <- Model
Pos: 3 <- this is OK
Tab(3a): 9 <- this is OK (based on our condition)
Tab(3): B3 <- why does this return name B3? (should return None or 9)
B1: 9 <- this is BAD, here should be None
B2: None
B3: None <- this is BAD, here should be 9
B4: None
B3n: B3 <- why does this return name B3?! (should return None or 9)
From the output we see that Tab always returns B1 for parameter 0x03 instead of B3. It looks like A + 0x02 is calculated as 0x01 where it's used as a parameter. Am I doing something wrong, or is it some Z3py implementation error? Or does this have to do with how BitVec terms work?
This looks like the same problem as in this post: How to correctly use Solver() command in Python API of Z3 with declared function.
The problem is that if N == 0x01: ... does not create a Z3 if-then-else expression; it literally check whether N is a Python-int with concrete value 1. To get the desired expression you need to use Z3's If(...) function.
I am working with the following C statement, and trying to convert it over to Swift:
if (c1 & c2 & c3 & c4 & c5 & 0xf000)
I am not very familiar with C, so I'm not exactly sure what the if statement is checking, but c1, c2, c3, c4, & c5 are integers, and I know that "&" is a bitwise operator. How could I implement this same statement in Swift?
In C (if I recall correctly), if the result of the paranthesised expression c1 & c2 & ... evaluates to a non-zero value, then that is considered "true".
In Swift, type safety being important, the result of bitwise operations are not automatically cast to a truth value (the Bool type), so you'd need to something like
if c1 & c2 != 0 {
// do this
}
You're probably looking for something like below:
let c1: UInt16 = 0x110F
let c2: UInt16 = 0x1101
let c3: UInt16 = 0x1401
let c4: UInt16 = 0x0001
let c5: UInt16 = 0x0A01
if c1 & c2 & c3 & c4 & c5 & 0xF000 != 0 {
println("Do Something, because some bits lined up!")
}
Swift bitwise operators are described here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html
Bitwise operators are the same in both C and Swift. In C that statement is checking to see if there is at least one bit position that each variable (and the literal) have in common where there is a '1' value. Those common ‘1’ positions are dropped into the new value. 11001 & 10101 would drop into 10001, for instance.
The resultant in both languages is (essentially) an integer. The real difference between C and Swift here is that in C any value that is not ‘0’ can be interpreted as the boolean true. Your C code snippet is doing just that; interpreting a non-zero value as true. Swift, on the other hand differentiates between an integer and a boolean, and requires specificity. That is why in my Swift snippet you have to specifically check for a value that is non-zero.
BTW, you could change your c snippet to what I have below and have the logical equivalent, as well as match the Swift snippet.
if ( (c1 & c2 & c3 & c4 & c5 & 0xf000) != 0)
Hope that was at least a little helpful.
I'm having some trouble figuring out how to separate a string which is tab delimited into chunks of data as an example if i have a text file which I'm reading from that looks like this
a1 b1 c1 d1 e1
a2 b2 c2 d2 e2
and i read the first line of my file and get a string which of
"a1 b1 c1 d1 e2"
I want to separate this into 5 variables a,b,c,d and e, or create a list (a b c d e). Any thoughts?
Thanks.
Try concatenating parentheses onto the front and back of your input string, then using read-from-string (I assume you're using Common Lisp, since you tagged your question clisp).
(setf str "a1 b1 c1 d1 e2")
(print (read-from-string (concatenate 'string "(" str ")")))
Yet another way to go about it (a tad more robust, perhaps), You can also easily modify it so that you could `setf' a character in the string once the callback is called, but I didn't do it that way because it seemed like you don't need this sort of ability. Also, in that later case, I'd rather use a macro.
(defun mapc-words (function vector
&aux (whites '(#\Space #\Tab #\Newline #\Rubout)))
"Iterates over string `vector' and calls the `function'
with the non-white characters collected so far.
The white characters are, by default: #\Space, #\Tab
#\Newline and #\Rubout.
`mapc-words' will short-circuit when `function' returns false."
(do ((i 0 (1+ i))
(start 0)
(len 0))
((= i (1+ (length vector))))
(if (or (= i (length vector)) (find (aref vector i) whites))
(if (> len 0)
(if (not (funcall function (subseq vector start i)))
(return-from map-words)
(setf len 0 start (1+ i)))
(incf start))
(incf len))) vector)
(mapc-words
#'(lambda (word)
(not
(format t "word collected: ~s~&" word)))
"a1 b1 c1 d1 e1
a2 b2 c2 d2 e2")
;; word collected: "a1"
;; word collected: "b1"
;; word collected: "c1"
;; word collected: "d1"
;; word collected: "e1"
;; word collected: "a2"
;; word collected: "b2"
;; word collected: "c2"
;; word collected: "d2"
;; word collected: "e2"
Here's an example macro you could use, if you wanted to modify the string as you read it, but I'm not entirely happy with it, so maybe someone will come up with a better variant.
(defmacro with-words-in-string
((word start end
&aux (whites '(#\Space #\Tab #\Newline #\Rubout)))
s
&body body)
`(do ((,end 0 (1+ ,end))
(,start 0)
(,word)
(len 0))
((= ,end (1+ (length ,s))))
(if (or (= ,end (length ,s)) (find (aref ,s ,end) ',whites))
(if (> len 0)
(progn
(setf ,word (subseq ,s ,start ,end))
,#body
(setf len 0 ,start (1+ ,end)))
(incf ,start))
(incf len))))
(with-words-in-string (word start end)
"a1 b1 c1 d1 e1
a2 b2 c2 d2 e2"
(format t "word: ~s, start: ~s, end: ~s~&" word start end))
assuming that they are tabbed (not spaced) then this will create a list
(defun tokenize-tabbed-line (line)
(loop
for start = 0 then (+ space 1)
for space = (position #\Tab line :start start)
for token = (subseq line start space)
collect token until (not space)))
which results in the following:
CL-USER> (tokenize-tabbed-line "a1 b1 c1 d1 e1")
("a1" "b1" "c1" "d1" "e1")