XQuery substring for a sequence of items - substring

I want to extract all values of a column from a table, determine the maximum of those values, and return that value.
This is what I tried:
let $abc := doc("file:///some_local_file")//AOSCAT_MetricDetail//table[#class="pretty-table"]//tr/td[13]
return <li>{ fn:max(fn:substring($abc,1,1)) }</li>
But fn:substring can't handle a sequence of more than one item.
So I tried this:
let $set_all_mbd := doc("file:///some_local_file")//AOSCAT_MetricDetail//table[#class="pretty-table"]//tr/td[13]
for $xyz in $set_all_mbd
let $abc := fn:substring($xyz,1,1)
return <li>{ fn:max($abc) }</li>
It returns for every row of the table though, and I want just one single value returned.
I suppose this is very simple but XQuery is new to me and can't figure out how to do it...

If you want to apply substring to each item in the sequence, try
let $abc := doc("file:///some_local_file")//AOSCAT_MetricDetail//table[#class="pretty-table"]//tr/td[13]
return <li>{ max($abc/substring(.,1,1)) }</li>
(untested).
If that doesn't work then you can definitely do it with an explicit for:
let $abc := doc("file:///some_local_file")//AOSCAT_MetricDetail//table[#class="pretty-table"]//tr/td[13]
return <li>{ max(for $x in $abc return substring($x,1,1)) }</li>

Related

How to perform Find, Distinct & Sort all together using go mongo driver

I have a command which is made using "labix.org/v2/mgo" library
err = getCollection.Find(bson.M{}).Sort("department").Distinct("department", &listedDepartment)
this is working fine. But now I'm moving to the official golang mongo-driver "go.mongodb.org/mongo-driver/mongo" and I want to run this command in that library but there is no direct function that I can use with Find then Sort then Distinct. How can I achieve this command using this mongo-driver. The variable listedDepartment is type of []string. Please suggest me know the solutions.
You may use Collection.Distinct() but it does not yet support sorting:
// Obtain collection:
c := client.Database("dbname").Collection("collname")
ctx := context.Background()
results, err := c.Distinct(ctx, "department", bson.M{})
It returns a value of type []interface{}. If you know it contains string values, you may use a loop and type assertions to obtain the string values like this:
listedDepartment = make([]string, len(results))
for i, v := range results {
listedDepartment[i] = v.(string)
}
And if you need it sorted, simply sort the slice:
sort.Strings(listedDepartment)

Is it possible to initialize array in batch in AutoHotKey?

I know I can write
a := Object()
a[1] := "textA"
a[2] := "textB"
a[3] := "textC"
Can I write something like
a := {"textA", "textB", "textC"}
?
You can define an indexed array using the bracket syntax:
a := ["textA", "textB", "textC"]
or the array creation function:
a := Array{"textA", "textB", "textC"}
An indexed array is an object representing a list of items, numbered 1 and up. In this example, the value "textA" is stored in object key 1, the value "textB" in object key 2 and the value "textC" in object key 3.
https://autohotkey.com/docs/Tutorial.htm#s7

Series of Variables by name

In AHK script:
Code for finding a value between great numbers of variables above, for one variable:
If (Variable1 = "sin (90°)")
MsgBox Value is reached
How searching by this method between series of variables with different value of number in their names? From Variable5 to Variable15, Variable51 to Variable105, etc.
How modify this code if number from 5 to 15, 51 to 105, or 74 to 117 etc?
number = 5
If (Variable%number% = "sin (90°)")
.............
Is %Variable%number%% acceptable and will works surely?
And here may also be useful Associative Arrays. What is it by simple examples?
Best practice here would probably be to use an array in the first place.
myArray := []
myArray[1] := "bla"
myArray.Push("bla2") ;by using this you don't need to worry about the index number
myArray[3] := "sin (90°)"
myArray[4] := 63456
Loop % myArray.MaxIndex()
{
If (myArray[A_Index] = "sin (90°)")
{
MsgBox Value is reached
}
}
... another example
anotherArray := []
Loop, read, C:\Files\prog.txt
{
If (A_LoopReadLine = "FileRead, OutputVar, C:\Files\prog1.txt")
{
anotherArray.Push(A_LoopReadLine)
MsgBox, An interesting code line was found. And was added to the array.
}
Else If (A_LoopReadLine = "blablabla")
{
anotherArray.Push(A_LoopReadLine)
MsgBox, An interesting code line was found. And was added to the array.
}
Else If (A_LoopReadLine = "some other text line")
{
anotherArray.Push(A_LoopReadLine)
MsgBox, An interesting code line was found. And was added to the array.
}
;Else
;{
; MsgBox, Nothing important was found.
;}
}
Loop % anotherArray.MaxIndex()
{
currentArrayEntry := anotherArray[A_Index]
MsgBox, %currentArrayEntry%
}
In an expression you can use variable expansion to modify the name of the variable to use:
number = 5
If (Variable%number% = "sin (90°)")
.............
But consider using arrays instead.
Is %Variable%number%% acceptable and will works surely?
Not. More right way
% Variable%number%
but it may have a problem.
Possible way is use Var := expression
Variable:= number
may be.

With Maple how can i prompt the user to answer a boolean expression?

The exact question would be "is the equation you want to use x=f(Xo)". This is in an if statement already so if true then continue if not then prompt user to enter a different function.
Your bit about its already being inside an if statement isn't very workable because that doesn't allow for an alternative value to be assigned to something in the case that the initial response is negative.
You should be able to work with something like this. Call p(), and assign its result to a ans, say, and then work with that value (and/or test it for some properties).
restart:
p := proc()
local answer, oldprompt, res1, res2;
oldprompt := interface(':-prompt'=``);
try
printf("Is the equation you want to use x=f(Xo)? (y/n)\n");
res1 := readline(-1);
if member(res1,{"y;","y","yes;","yes"}) then
answer := x=f(Xo);
elif member(res1,{"n;","n","no;","no"}) then
printf("Enter your equation.\n");
res2 := readline(-1);
answer := parse(res2);
else
printf("Response not recognized\n");
end if;
catch:
finally
interface(':-prompt'=oldprompt);
end try;
if answer='answer' then NULL else answer end if;
end proc:
ans := p();
[edited below]
It is possible to get it a little closer to your original. With procedure p as below the returned result will be one of true/false/FAIL and could be used in a conditional. In the case that the return values if false (because of the response to the initial query) then a second query is made about the choice of another expression.
This version of p takes two arguments, the first is the suggested initial equation. The second is a name which can be assigned any alternative.
restart:
p := proc(candidate, resultvar)
local result, oldprompt, res1, res2;
oldprompt := interface(':-prompt'=``);
try
printf(sprintf("Is the equation you want to use %a? (y/n)\n",
candidate));
res1 := readline(-1);
if member(res1,{"y;","y","yes;","yes"}) then
result := true;
assign(resultvar,candidate);
elif member(res1,{"n;","n","no;","no"}) then
result := false;
printf("Enter your equation.\n");
res2 := readline(-1);
assign(resultvar,parse(res2));
else
printf("Response not recognized\n");
result := FAIL;
end if;
catch:
finally
interface(':-prompt'=oldprompt);
end try;
return result;
end proc:
Now we can test it out.
p(x=f(X0), 'ans');
ans;
We could also use the call to p inside an if statement. Eg,
if p(x=f(X0), 'ans') then
"accepted";
else
"new choice made";
end if;
ans;
Here, answering "n" to the first query will make the conditional test see a false value, but the named argument ans will get assigned to as a side-effect.

How can I use a variable as a key in the associative array in autohotkey?

In autohotkey_L, there is a associative data structure. For example,
hash := {key_hash:"value"}
val:= hash["key_hash"]
MsgBox %val%
But if I want to use a variable as a key to access the value in the assocative array, it fails. For example, the following doesn't work
hash := {key_hash:"value"}
other_val="key_hash"
val:= hash[other_val]
MsgBox %val%
and this doesn't work either:
hash := {key_hash:"value"}
other_val="key_hash"
val:= hash[%other_val%]
MsgBox %val%
** gave me an error: The following variable name contains an illegal character: ""key_hash""
How can I use a variable to access the value in an associative array?
I need this to get the key as an argument in a function.
Alby,
Your variable other_val contained the data: "key_hash" , not what you wanted: key_hash. Just remove the two double quotes and you are fine.
hash := {key_hash:"value"}
other_val=key_hash
val:= hash[other_val]
MsgBox %val%
Or use the assignment (:=)
hash:={key_hash:"value"} ; hash:=Object("key_hash", "value")
other_val:="key_hash"
val:=hash[other_val]
MsgBox, % val