How can I write conditional expression in output of a minizinc program? - conditional-statements

I have the following code:
output["\(w) \(l)\n\(n)\n"] ++ [if rotation[i] then "\(x[i]) \(y[i]) \(p_x[i]) \(p_y[i]) R\n" else "\(x[i]) \(y[i]) \(p_x[i]) \(p_y[i])\n" endif |i in CIRCUITS];
My purpose is to print each row and the vaue "R" whether rotation[i] is true, false otherwise.
For instance:
w l
n
x[1] y[1] p_x[1] p_y[1] "R"
x[2] y[2] p_x[2] p_y[2]
In this example rotation[1] is true and rotation[2] is false

Your sample MiniZinc code was almost there. The important change you will have to make if to force the "rotation" variable to take its "solution value" using the "fix" builtin function.
output ["\(w) \(l)\n\(n)\n"]
++ ["\(x[i]) \(y[i]) \(p_x[i]) \(p_y[i])"
++ if fix(rotation[i]) then " R" else "" endif
++ "\n"
| i in index_set(rotation)];
Additionally, I would suggest trying to make the conditional part as small as possible to improve readability, as incorporated in the above code fragment.

Related

HP/Tandem TACL How to use the % as a value in the SQL like clause

In my TACL, I'm trying to create a variable used as input to an SQLCI command. I want to use a LIKE clause with a % as a wildcard. Every time it replaces the % with a ? causing the SQL statement to not return the desired results.
Code snippitz:
?TACL macro
#Frame
#Set #informat plain
#Set #outformat pretty
#Push stoprun DC fidata var1 mailmast sqlvol sqlsvol IsOpen EMLFile ans emlline
#Push mailfile mfile likeit charaddr sqlin sqlout test
[#Def True text |body|-1]
[#Def False text |body|0]
Intervening code cut out to reduce length - the cutout code works
== select <program name> from <full mailmast filename> where mm_file_prefix
== like "<likename>%" for browse access;
[#If Not [StopRun]
|then|
#Setv test "~%"
#Set #trace -1
#Set SqlIn select mm_program_name from [mailmast] where mm_file_prefix
#appendv sqlin "like ""[LikeIt]~%"" for browse access;"
SQLCI/Inv Sqlin,outv sqlout/
] == end of if
When I run the code, I display the variables, and it has replaced the % with ?
-TRACE-
-19-st 1 v
Invoking variable :MAILMAST.1
#Set SqlIn select mm_program_name from $DATA5.SQL2510.MAILMAST where mm_file_p
refix
-TRACE-
-20-
#appendv sqlin "like ""[LikeIt]
^
-TRACE-
-20-
Invoking variable :LIKEIT.1
#appendv sqlin "like ""ED?"" for browse access;"
-TRACE-
-20-d test
?
-22-st 1 v
SQLCI/Inv Sqlin,outv sqlout/
-TRACE-
-23-d sqlin
select mm_program_name from $DATA5.SQL2510.MAILMAST where mm_file_prefix
like "ED?" for browse access;
-24-
Since the % is not there as a wildcard, this SQL statement fails to bring up the proper record.
The question is, how do I put a % into a TACL variable and not get it changed to a ?
Edited to replace duplicate code with the start of the TACL Macro - MEH
I really don't like answering my own question because that looks like I was just setting things up to make me look smart, but in continuing my research while waiting, I found the answer. See code snippitz below:
?TACL macro
#Frame
#Set #informat plain
#Set #outformat pretty
#Push stoprun DC fidata var1 mailmast sqlvol sqlsvol IsOpen EMLFile ans emlline
#Push mailfile mfile likeit charaddr sqlin sqlout test
[#Def True text |body|-1]
[#Def False text |body|0]
[#Def ascii struct
Begin
BYTE byt0 value 37;
CHAR pcent REDEFINES byt0;
End;
] == end of struct
Note the addition of the struct. This is to be able to assign a byte a decimal value of 37 (%) and redefine it as a character to be used in the like statement.
== select <program name> from <full mailmast filename> where mm_file_prefix
== like "<likename>%" for browse access;
[#If Not [StopRun]
|then|
#Set test [ascii:pcent]
#Set #trace -1
#Set SqlIn select mm_program_name from [mailmast] where mm_file_prefix
#append sqlin like "[LikeIt][ascii:pcent]" for browse access;
SQLCI/Inv Sqlin,outv sqlout/
] == end of if
Note the use of the struct [ascii:pcent] and this does work.
Thanks to all who read my question.

while loop end everything after it's complete

So I'm trying to make a lottery program where you pick a animal, letter, and number, those are put into a array and compared to another that has the parts chosen by random.
Testing with what I call the animal round.
I have a while loop for invalid entries, that it will not move on till one of the four animals is accepted. But when it does that, the variable invalid goes to false and the coding after it doesn't get used at all. I had this problem last night, and when I finally gave up and went to bed, I decided I'll write it out on flowgorithm (If you haven't heard it makes a flow chart and you can go through programming with it step by step).
I made it, and it worked like expected, I copy and paste it over, and I get the exact same problem as last night.
Here is the code.
#import library
import random
#get variables
game = True
invalid = True
animalarray = [""]
animalarray.append("tiger")
animalarray.append("cow")
animalarray.append("turtle")
animalarray.append("bird")
lotteryarray = [""]
#game loop
#animal round
print("Pick a animal: ")
print("tiger")
print("cow")
print("turtle")
print("bird")
print(" ")
lotteryarray[0] = input()
#while loop for invalid entry
while invalid == True:
if lotteryarray[0] == "tiger" or lotteryarray[0] == "cow" or lotteryarray[0] == "turtle" or lotteryarray[0] == "bird":
invalid == False
else:
print("Invalid entry!")
lotteryarray[0] = input()
print(" ")
print("You chose " + lotteryarray[0])
game == False
And this is all I get in the shell:
Pick a animal:
tiger
cow
turtle
bird
tiger
the tiger there is what I put in, it isn't being printed.
And here is the flowgorithm, like I said, in flowgorithm this works.
flowgorithm of lottery game
I figured it out.
I printed what invalid was after it was meant to change to False and it didn't change, I changed the two equal signs to one and it worked and changed the value.

PHP do while with multiple while conditions

I'm hoping somone might be able to help me here. I've had a look and found someone who has had he same issue:
Do-While Loop with Multiple Conditions
However I've checked and cannot see where i'm going wrong
Here's my code:
$x=0;
$y=0;
do {
$x = rand(0,5);
$y++;
} while ($x!=5 || $y<=5);
and so I was expecting the following: Either the code to stop when X was not == 5 OR y was more than 5. However the result i got was x=5 and y=>5
To put the theory to the test i changed my code to:
$x=0;
$y=0;
echo'<br />--x-y';
do {
$x = rand(0,5);
$y++;
echo'<br />--'.$x.'-'.$y;
if($y>5)
echo"-I should finish here";
} while ($x!==5 || $y<=5);
echo "<br />x=".$x;
echo "<br />y=".$y;
and in one instance i ended up with :
--x-y
--5-1
--3-2
--4-3
--4-4
--2-5
--4-6-I should finish here
--1-7-I should finish here
--3-8-I should finish here
--3-9-I should finish here
--3-10-I should finish here
--3-11-I should finish here
--5-12-I should finish here
x=5
y=12
Any suggestions as to where i'm going wrong?
You want to stop when (X = 5 or Y > 5). The condition inside while is for continuing, so you have to inverse the condition to: (X != 5 AND Y <= 5)
De Morgan Laws

How can I signal parsing errors with LPeg?

I'm writing an LPeg-based parser. How can I make it so a parsing error returns nil, errmsg?
I know I can use error(), but as far as I know that creates a normal error, not nil, errmsg.
The code is pretty long, but the relevant part is this:
local eof = lpeg.P(-1)
local nl = (lpeg.P "\r")^-1 * lpeg.P "\n" + lpeg.P "\\n" + eof -- \r for winblows compat
local nlnoeof = (lpeg.P "\r")^-1 * lpeg.P "\n" + lpeg.P "\\n"
local ws = lpeg.S(" \t")
local inlineComment = lpeg.P("`") * (1 - (lpeg.S("`") + nl * nl)) ^ 0 * lpeg.P("`")
local wsc = ws + inlineComment -- comments count as whitespace
local backslashEscaped
= lpeg.P("\\ ") / " " -- escaped spaces
+ lpeg.P("\\\\") / "\\" -- escaped escape character
+ lpeg.P("\\#") / "#"
+ lpeg.P("\\>") / ">"
+ lpeg.P("\\`") / "`"
+ lpeg.P("\\n") -- \\n newlines count as backslash escaped
+ lpeg.P("\\") * lpeg.P(function(_, i)
error("Unknown backslash escape at position " .. i) -- this error() is what I wanna get rid of.
end)
local Line = lpeg.C((wsc + (backslashEscaped + 1 - nl))^0) / function(x) return x end * nl * lpeg.Cp()
I want Line:match(...) to return nil, errmsg when there's an invalid escape.
LPeg itself doesn't provide specific functions to help you with error reporting. A quick fix to your problem would be to make a protected call (pcall) to match like this:
local function parse(text)
local ok, result = pcall(function () return Line:match(text) end)
if ok then
return result
else
-- `result` will contain the error thrown. If it is a string
-- Lua will add additional information to it (filename and line number).
-- If you do not want this, throw a table instead like `{ msg = "error" }`
-- and access the message using `result.msg`
return nil, result
end
end
However, this will also catch any other error, which you probably don't want. A better solution would be to use LPegLabel instead. LPegLabel is an extension of LPeg that adds support for labeled failures. Just replace require"lpeg" with require"lpeglabel" and then use lpeg.T(L) to throw labels where L is an integer from 1-255 (0 is used for regular PEG failures).
local unknown_escape = 1
local backslashEscaped = ... + lpeg.P("\\") * lpeg.T(unknown_escape)
Now Line:match(...) will return nil, label, suffix if there is a label thrown (suffix is the remaining unprocessed input, which you can use to compute for the error position via its length). With this, you can print out the appropriate error message based on the label. For more complex grammars, you would probably want a more systematic way of mapping the error labels and messages. Please check the documentation found in the readme of the LPegLabel repository to see examples of how one may do so.
LPegLabel also allows you to catch the labels in the grammar by the way (via labeled choice); this is useful for implementing things like error recovery. For more information on labeled failures and examples, please check the documentation.

Fixing the Rebol3 example calculator to use normal math operator precedence

In the example calculator:
REBOL [title: "Calculator"]
do %r3-gui.r3
stylize [
btn: button [
facets: [init-size: 50x50]
actors: [on-action:[set-face f join get-face f get-face face]]
]
]
view [
hgroup [
f: field return
btn "1" btn "2" btn "3" btn " + " return
btn "4" btn "5" btn "6" btn " - " return
btn "7" btn "8" btn "9" btn " * " return
btn "0" btn "." btn " / " btn "=" on-action [
attempt [set-face f form do get-face f]
]
]
]
...the resulting program doesn't (as Rebol traditionally doesn't) evaluate mathematical expressions with * having a higher precedence than +. E.g. 2 + 3 * 4 gives 20 instead of 14.
I thought I had read somewhere that Rebol3 contained a new function that would evaluate math expressions more the way folks are used from nearly every other context. Is that true? If so, can the above code be made to use it without a significant amount of change?
I'm not sure there's a specific function in Rebol 3 that evaluates operators according to formal precedence (I'd be pleased to be corrected), though there are attempts in the wild to implement such a function. If you were to locate such a function, you can just change the evaluator from do to do-expression (where do-expression is said function) in the on-action block of your "=" button.
Who said that "the normal order" to evaluate should be the best?
Some 'wiseguy' once came up with that because he could not handle being wrong unfortunately he was stronger than the others and threatened to hammer their heads in if they wouldnot do things like he had done, so we are taught to this day that multiplication precedes over addition despite of the order they were put in. Rebol rebels!
;)