Extract "Red" "Blue" "Circle" "Black" from "RedBlueCircleBlack" using capital as delimiter? - regex

Is this a RegEx problem?
To note: there will always be only four items, each starts with a capital letter, each will be in order (color,color,shape,color):
"BlackWhiteTriangleGreen" etc.
So,
a="BlackWhiteTriangleGreen"
yields:
c1 = "Black"
c2 = "White"
S = "Triangle"
c3 = "Green"
EDIT: referencing the post suggested by Alex K., an AS3 solution as follows works:
private function UpperCaseArray(input:String):void {
var result:String = input.replace(/([A-Z]+)/g, ",$1").replace(/^,/, "");
var b:Array=result.split(",");
c1 = b[0];
c2 = b[1];
S = b[2];
c3 = b[3];
}

referencing the post suggested by Alex K., an AS3 solution as follows works:
private function UpperCaseArray(input:String):void {
var result:String = input.replace(/([A-Z]+)/g, ",$1").replace(/^,/, "");
var b:Array=result.split(",");
c1 = b[0];
c2 = b[1];
S = b[2];
c3 = b[3];
}

Related

PineScript formulation of condition?

Say I have 3 conditions, and I want to perform some action if 2 of these conditions are fulfilled. With a few number of combinations, like 3, the solution could be something like:
c1=...
c2=...
c3=...
if (c1==true and c2==true) then
elseif (c1==true and c3==true) then
elseif (c2==true and c3==true) then
This is not very practical with, say, 100 conditions of which 90 should be fulfilled.
Is there a more compact way to implement this in PineScript?
If you represent the boolean true/false of your condition as 1/0 ints, you could count them to see how many are true.
The shortest way I can think of, is putting them in an array, and calculate the sum of that array.
//#version=5
indicator("My Script")
var int c0 = 1
var int c1 = 0
var int c2 = 1
var int c3 = 1
var int c4 = 1
var int c5 = 0
var int c6 = 0
var int c7 = 1
var int c8 = 1
var int c9 = 1
var int[] a = array.from(c0,c1,c2,c3,c4,c5,c6,c7,c8,c9)
mySum = array.sum(a)
plot(mySum)

Reference a column by a variable

I want to reference a table column by a variable while creating another column but I can't get the syntax:
t0 = Table.FromRecords({[a = 1, b = 2]}),
c0 = "a", c1 = "b",
t1 = Table.AddColumn(t0, "c", each([c0] + [c1]))
I get the error the record's field 'c0' was not found. It is understanding c0 as a literal but I want the text value contained in c0. How to do it?
Edit
I used this inspired by the accepted answer:
t0 = Table.FromRecords({[a = 1, b = 2]}),
c0 = "a", c1 = "b",
t1 = Table.AddColumn(t0, "c", each(Record.Field(_, c0) + Record.Field(_, c1)))
Another way:
let
t0 = Table.FromRecords({[a = 1, b = 2]}),
f = {"a","b"},
t1 = Table.AddColumn(t0, "sum", each List.Sum(Record.ToList(Record.SelectFields(_, f))))
in
t1
try using an index as below
let t0 = Table.FromRecords({[a = 1, b = 2]}),
#"Added Index" = Table.AddIndexColumn(t0, "Index", 0, 1),
c0 = "a",
c1 = "b",
t1 = Table.AddColumn(#"Added Index", "c", each Table.Column(#"Added Index",c0){[Index]} + Table.Column(#"Added Index",c1){[Index]} )
in t1
Expression.Evaluate is another possibility:
= Table.AddColumn(t0, "c", each Expression.Evaluate("["&c0&"] + ["&c1&"]", [_=_]) )
Please refer to this article to understand the [_=_] context argument:
Expression.Evaluate() In Power Query/M
This article explains that argument specifically:
Inside a table, the underscore _ represents the current row, when working with line-by-line operations. The error can be fixed, by adding [_=_] to the environment of the Expression.Evaluate() function. This adds the current row of the table, in which this formula is evaluated, to the environment of the statement, which is evaluated inside the Expression.Evaluate() function.

C++ String ASCII Communication to SerialCOM with VB example

I have a VB 5.0 code that does what I need, but I need to implement it in Visual Studio C++ (2013), the thing is that I'm not getting there, and I have no idea what I'm doing wrong, so I'll show you some code (VB vs C++ - mine) and hope someone is able to help.
For now, thank you for reading this.
I've tried to send the string in very different formats and I think that I finally got it, in how to send, the problem still is reading the answer, I have no idea what I'm doing wrong.
The machine returns (I think 2 bytes) and they are START OF HEADING and ?, I can see it by printing in the console the numbers 1 and 63.
I'll just leave some code.
For asking the current temperature the VB program is:
Private Sub cmdGetTemperaturePV_Click()
If MSComm.PortOpen Then
MSComm.Output = Chr(1) & Chr(0) & Chr(0) & Chr(1) & Chr(13) & Chr(10)
txtMsg(1).Text = "1,0,0,1,13,10"
txtPVTemperature.Text = ""
Else
txtMsg(1).Text = "COM Port OFF"
Beep
End If
End Sub
And mine(C++) is:
String^ a1 = "\x1";
String^ a2 = "\x0";
String^ a3 = "\x0";
String^ a4 = "\x1";
String^ a5 = "\xD";
String^ a6 = "\xA";
String^ enviar = a1 + a2 + a3 + a4 + a5 + a6;
this->serialPort1->Write(enviar);
By using the program "Hercules" I can simulate the reception of the machine and I can see that I'm sending exactly the same thing as the VB program.
Now I think that the problem is receiving, so about that, there's this:
Private Sub tmrRun_Timer()
Dim i As Integer
Dim apt As Byte, B1 As Byte, B2 As Byte
Dim stx As String
If MSComm.PortOpen Then
If MSComm.InBufferCount >= 6 Then
stx = MSComm.Input
stx = Right(stx, 6)
txtMsg(2).Text = ""
For i = 1 To Len(stx)
txtMsg(2).Text = txtMsg(2).Text & Asc(Mid(stx, i, 1)) & ","
Next i
txtMsg(2).Text = Left(txtMsg(2).Text, Len(txtMsg(2).Text) - 1)
apt = Asc(Left(stx, 1))
B1 = Asc(Mid(stx, 2, 1))
B2 = Asc(Mid(stx, 3, 1))
Select Case apt
Case 1:
txtPVTemperature.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 2:
txtSPTemperature.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 3:
Case 4:
txtPVHumidity.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 5:
txtSPHumidity.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 6:
Case 7:
Case 8:
Case 9:
If Asc(Mid(stx, 2, 1)) > 0 Then
txtChamber.Text = "ON"
Else
txtChamber.Text = "OFF"
End If
Case 10:
txtEvents.Text = GetInt(B1, B2)
Case 11:
Case 12:
txtInputs.Text = "1..8 = " & B1 & " 9..16 = " & B2
Case 13:
txtAlarms1.Text = " 1.. 8 = " & B1 & " 9..16 = " & B2
Case 14:
txtAlarms2.Text = "17..24 = " & B1 & " 25..32 = " & B2
End Select
End If
End If
End Sub
And I'm trying many different things, the best I got (the one that led me to 1 and 63) is this:
{
String^ rec;
if (this->serialPort1->IsOpen)
{
this->textBox1->Text = String::Empty;
try
{
rec = this->serialPort1->ReadExisting();
}
catch (TimeoutException^)
{
this->textBox2->Text = "Timeout";
}
}
this->textBox1->Text = rec;
char aux[100];
if (rec == String::Empty)
this->textBox2->Text = "String Empty";
else
{
std::string rec1 = marshal_as<std::string>(rec);
strcpy(aux, rec1.c_str());
int a, b, c, d, e1, f;
printf("String received: ");
for (int i = 0; i < 6; i++)
{
if (aux[i] == 0)
break;
printf("%ld ", aux[i]);
if (i == 0)
a = aux[i];
if (i == 1)
b = aux[i];
if (i == 2)
c = aux[i];
if (i == 3)
d = aux[i];
if (i == 4)
e1 = aux[i];
if (i == 5)
f = aux[i];
}
}
}
I'm expecting to receive a 6 byte string and It's not happening.
I'm so sorry for the long post, but I think this way I can be more specific.
Again, thank you very much!

Nested IF AND OR in Excel

How do I write the formula for the following:
if B2 = "SF" and D2 = "1"
then H2 = E2 + .75
else if B2 = "SF" and D2 = ".25"
then H2 = E2 + .625
else if B2 = "CW" and D2 = "1"
then H2 = E2 + 1
I want my answers to be in H2, with data being entered into B2, D2 and E2.
=if(AND(B2="SF",D2=1)=TRUE,E2+0.75,if(AND(B2="SF",D2=0.25)=TRUE,E2+0.625,if(AND(B2="CW",D2=1)=TRUE,E2+1,0)))
Regards

Kotlin replace isEmpty()&last() with lastOrNull() within a Collection

I would like to use something like (code below), but I think that there must be a nicer solution with usage lastOrNull() instead of using isEmpty and last()
data class Entry(val x: Float, val y: Float)
var entries: MutableList<Entry> = ArrayList()
if(some) {
entries.add(Entry(100f, 200f)
}
val foo = (if (entries.isEmpty()) 0f else entries.last().y) + 100f
Is there some better way like entries.lastOrNull()?.y if null 0f?
you can using Kotlin elvis operator ?:, for example:
// if the expression `entries.lastOrNull()?.y` is null then return `0f`
// v
val lastY = entries.lastOrNull()?.y ?: 0f
for the expression in your code above, you can using safe-call ?.let/?.run to make your code more clearly, for example:
//val foo = if (entries.isEmpty()) 0f else entries.last().y + 100f else 100f
// if no Entry in List return `0F` ---v
val foo = entries.lastOrNull()?.run { y + 100 } ?: 0F
// ^--- otherwise make the last.y + 100
If I've managed to understand you correctly, this will do the job:
val foo = if (entries.isEmpty()) 0f else entries.lastOrNull()?.y ?: 0f + 100f