Okay guys, i got weird error for tcl script.
This is working code:
foreach param $bucket {
if {[lindex $param 0] == "bucketState" &&\
([lindex $param 1] == "provisioned" ||\
[lindex $param 1] == "Active")} {
set activatedFlag "TRUE"
}
}
And this is not:
foreach param $bucket {
if {[lindex $param 0] == "bucketState" &&\
([lindex $param 1] == "provisioned" ||\
[lindex $param 1] == "active")} {
set activatedFlag "TRUE"
}
}
The only difference is in "active" vs "Active". O_o
I'm not sure what error you're getting, but it looks fairly clear that the list element you're comparing is case sensitive. If you want to a case-insensitive search, use string tolower but I can't really see why you would need to do that.
foreach param $bucket {
if {[lindex $param 0] == "bucketState" && \
([lindex $param 1] == "provisioned" || \
[string tolower [lindex $param 1]] == "active")} {
set activatedFlag "TRUE"
}
}
Related
Is there a built-in data type in Dafny like List in Java (or any type for dynamic list)?
I've looked for it in Dafny Reference Manual, but nothing found.
It seems that a self-defined class must be defined for it.
If it is the fact, then how can the performance be assured for the generated java program and how can the gernerality of Dafny as a programming language be assured?
Not criticism, just curious.
Dafny's first collection is undoubtedly seq, which is an immutable list.
function sum(s: seq<int>): int {
if |s| == 0 then 0 else s[0] + sum(s[1..])
}
For anything else, the Dafny team is working on a standard library, but you might be interestest by the first example given in the Dafny documentation that also explain why lists are non trivial objects to verify:
https://dafny.org/dafny/DafnyRef/DafnyRef#sec-example
In short, to define a list, you want to write a class and store a ghost model of all the elements to ensure there is no cycle, and possibly write this node into another data structure. But the proofs are not obvious. Here is what I got so far.
class ListNode<T> {
var head: T
var tail: ListNode?<T>
ghost var Repr: seq<ListNode<T>>
constructor(h: T, t: ListNode?<T>) requires t != null ==> t.Valid() ensures Valid()
{
head:= h;
tail := t;
Repr := [this] + (if t == null then [] else t.Repr);
}
predicate Valid() reads this, Repr decreases |Repr|
{
&& |Repr| > 0
&& Repr[0] == this
&& (if tail == null then |Repr| == 1 else
&& |Repr| > 1
&& tail == Repr[1]
&& tail.Repr == Repr[1..]
&& tail.Valid())
}
lemma ReprAreDecreasing(i: int)
requires Valid()
requires 0 <= i < |Repr|
ensures Repr[i].Repr == Repr[i..]
ensures Repr[i].Valid()
{
if i == 0 {
} else {
tail.ReprAreDecreasing(i-1);
}
}
}
class List<T> {
var head: ListNode?<T>
var last: ListNode?<T>
ghost var Repr: seq<ListNode<T>>
constructor() ensures Valid() {
head := null;
last := null;
Repr := [];
}
lemma ValidImpliesAllNodesValid(n: ListNode<T>)
requires Valid()
requires n in Repr
ensures n.Valid() {
if n == head {
assert n.Valid();
} else {
var i :| 0 <= i < |Repr| && Repr[i] == n;
head.ReprAreDecreasing(i);
}
}
method Append(node: ListNode<T>)
{
...
}
predicate Valid() reads this, Repr
{
(if head != null then
&& last != null
&& head in Repr
&& head.Repr == Repr
&& head.Valid()
&& last == head.Repr[|head.Repr|-1]
&& assert last.Repr == head.Repr[|head.Repr|-1..] by {
head.ReprAreDecreasing(|head.Repr|-1);
} last.Valid()
else
&& last == null
&& |Repr| == 0)
}
}
References:
https://dafny.org/dafny/DafnyRef/DafnyRef#sec-seq-comprehension
I have been trying to implement the shunting yard algorithm, but the output of my parser is incorrect.
let mut stack: Vec<String> = vec![];
let mut op_stack: Vec<String> = vec![];
for current in sub_tree {
if current.tok_type == TokenType::NUMBER || current.tok_type == TokenType::NEGNUMBER {
self.parse();
stack.push(current.content.clone());
}
if current.tok_type == TokenType::SUBBIN
|| current.tok_type == TokenType::PLUSBIN
|| current.tok_type == TokenType::DIVBIN
|| current.tok_type == TokenType::MULBIN
{
while op_stack.len() > 0 && op_stack.last().unwrap().to_string() != "(" {
if op_prec(&op_stack.last().unwrap().to_string()) > op_prec(¤t.content)
|| (op_prec(&op_stack.last().unwrap().to_string()) == op_prec(¤t.content)
&& op_asso(¤t.content) == "left")
{
stack.push(op_stack.pop().unwrap().to_string());
} else {
break;
}
}
op_stack.push(current.content.to_string())
}
}
The original equation I am parsing: 1 + 2 * 3
I expected the following output: 1 2 3 * +
Instead I get this: 1 2 3 + *
I think I am going wrong somewhere in my while loop but I don't really know. I tried to follow the example on the Wikipedia article.
I forgot I had to pop from the operator stack back into the output stack at the end.
Comparing your code
if current.tok_type == TokenType::SUBBIN
|| current.tok_type == TokenType::PLUSBIN
|| current.tok_type == TokenType::DIVBIN
|| current.tok_type == TokenType::MULBIN
{
while op_stack.len() > 0 && op_stack.last().unwrap().to_string() != "(" {
if op_prec(&op_stack.last().unwrap().to_string()) > op_prec(¤t.content)
|| (op_prec(&op_stack.last().unwrap().to_string()) == op_prec(¤t.content)
&& op_asso(¤t.content) == "left")
{
stack.push(op_stack.pop().unwrap().to_string());
} else {
break;
}
}
op_stack.push(current.content.to_string())
}
with the Wikipedia code https://en.wikipedia.org/wiki/Shunting-yard_algorithm
- an operator o1:
while (
there is an operator o2 other than the left parenthesis at the top
of the operator stack, and (o2 has greater precedence than o1
or they have the same precedence and o1 is left-associative)
):
pop o2 from the operator stack into the output queue
push o1 onto the operator stack
It looks like they are functionally identical.
So I suspect the problem is not with the code, but instead with the precedence table. If you have the precedence of + and * the wrong way round, then you would get this behaviour. It is easy to get this mixed up as some source have precedence going from tighter binding to loser one and some have the opposite. Compare Wikipedia order of operations and Operator Precedence in Java, use the former.
In Groovy/Jenkinsfile declarative syntax, why is the result of the boolean AND operation on dictionary, dictionary, and integer objects an integer instead of boolean true/false?
pipeline {
agent any
stages {
stage( "1" ) {
steps {
script {
a = [:]
a.a = [:]
a.a["a"] = "1"
a.a["b"] = "2"
echo "${a}"
echo "${a.a}"
echo "${a.a.size()}"
def my_bool = (a && a.a && a.a.size())
echo "my_bool ${my_bool}"
}
}
}
stage( "2" ) {
when {
expression { true == (a && a.a && a.a.size()) } // Fails because result is integer "2", not boolean "true"
}
steps {
script {
echo "hello, world!"
}
}
}
}
}
My biases from other programming languages led me to think that a && a.a && a.a.size() should implicitly be converted to a boolean value. The echo reveals that the value is integer 2.
What is the Jenkins/Groovy idiomatic way to deal with this? I.e. if a stage is conditional on "dictionaries being non-null and having nonzero size", what is the idiomatically correct/preferred way to write that conditional?
Update:
Note: the echo "my_bool ${my_bool}" statement prints "my_bool 2". This is with Jenkins version 2.222.3.
expression { a?.a?.size() }
or even
expression { a?.a }
I'm trying to replace a a blank line in a set of text files (*.txt) for a "--" if the previous line matchs a pattern. My code is
awk 'BEGIN{$headerfound=0} { if (/pattern/) {print> FILENAME ; $headerfound=1} else { if((/^\s*$/) && ($headerfound == 1)) { $headerfound=0; print "--" > FILENAME } else {print > FILENAME} } }' *.txt
But for some reason, output is limited to 4kbytes files (if the file is larger, it gets clipped). Do you know where is the limitation?
Thanks,
Ariel
See #glennjackman's comments for problems in your script.
Since you are using GNU awk (you used \s which is gawk-specific) you can use inplace-editing and write your script as (spread out with white space to improve readability):
awk -i inplace '{
if (/pattern/) {
print
headerfound=1
} else {
if((/^\s*$/) && (headerfound == 1)) {
headerfound=0
print "--"
} else {
print
}
}
}' *.txt
but you can do the same thing much more concisely (and awk-ishly) as:
awk -i inplace '
/pattern/ { headerfound=1 }
headerfound && !NF { $0="--"; headerfound=0 }
1' *.txt
If you don't have inplace editing then do it this way:
for file in *.txt; do
awk '
/pattern/ { headerfound=1 }
headerfound && !NF { $0="--"; headerfound=0 }
1' "$file" > tmp$$ &&
mv tmp$$ "$file"
done
You can probably get away with:
suffix=".$$.tmp" '
awk -v suf="$suffix" '
FNR == 1 {outfile = FILENAME suf}
/pattern/ {headerfound = 1}
headerfound && /^[[:blank:]]*$/ {$1 = "--"}
{ print > outfile }
' *.txt
for f in *.txt; do
echo mv "${f}$suffix" "$f"
done
Remove the echo from the for loop if you're satisfied it's working.
Missed the "just after" requirement (using Ed's use of NF to find a blank line):
awk -v suf="$suffix" '
FNR == 1 {outfile = FILENAME suf}
/pattern/ {lineno = FNR}
FNR == lineno+1 && NF == 0 {$0 = "--"}
{ print > outfile }
' *.txt
I have the following proc which basically looks upp a couple of values in a dictonary and returns them as a list.
proc GetAllow { PID Ply } {
# read a dictonary from a file
catch {
append PlyAndDirXt $Ply "_xt"
append PlyAndDirYt $Ply "_yt"
set x_allow_tens [ dict get $allowables $PID $PlyAndDirXt ]
set y_allow_tens [ dict get $allowables $PID $PlyAndDirYt ]
set allowables [ list $x_allow_tens $y_allow_tens ]
} res
if { $res == 0 } {
return $allowables
}
if { $res != 0 } {
return 999
}
}
As I understand "catch" if everything is ok $res should be 0 = TCL_OK. In that case I would like the proc to return the list $allowables.
In case the values are not found in the dict due to none matching keys. I would like it to return 999. But I always get 999 back. What am I'm doing wrong here ?
As per the manual:
If script raises an error, catch will return a non-zero integer value corresponding to the exceptional return code returned by evaluation of script. Tcl defines the normal return code from script evaluation to be zero (0), or TCL_OK.
If the varName argument is given, then the variable it names is set to the result of the script evaluation. When the return code from the script is 1 (TCL_ERROR), the value stored in varName is an error message. When the return code from the script is 0 (TCL_OK), the value stored in resultVarName is the value returned from script.
As such, $res will not be equal to 0 unless the result of your script returns 0.
You can set catch to a variable like this:
set err [catch {
append PlyAndDirXt $Ply "_xt"
append PlyAndDirYt $Ply "_yt"
set x_allow_tens [ dict get $allowables $PID $PlyAndDirXt ]
set y_allow_tens [ dict get $allowables $PID $PlyAndDirYt ]
set allowables [ list $x_allow_tens $y_allow_tens ]
} res]
Then check
if { $err == 0 } {
return $allowables ;# Or return $res since that's the last evaluated line
}
if { $err != 0 } {
return 999
}