strword can be how can split used vbcrlf - vb.net

How can split that items? I don't have anything else to do, which is what the textbox results look like and I don't know how to extract the values correctly from it.
Dim lines() As String = TxtResultToString1.Lines
For i As Integer = 1 To lines.Length - 1
Dim strWords = lines(i).Split(vbCrLf)
MsgBox(strWords(0))
Next
Items: this looks like it in the textbox
{ item = , Count = 1 }
{ item = 13, Count = 1 }
{ item = 17, Count = 1 }
{ item = 31, Count = 1 }
{ item = 5, Count = 1 }
{ item = 8, Count = 1 }
{ item = 77, Count = 2 }
{ item = 68, Count = 1 }
{ item = 21, Count = 1 }
{ item = 71, Count = 1 }
{ item = 40, Count = 1 }
{ item = 14, Count = 1 }
{ item = 49, Count = 1 }
{ item = 45, Count = 1 }
{ item = 29, Count = 1 }
{ item = 36, Count = 1 }
{ item = 44, Count = 1 }
In another textbox I want to do the following:
Textbox2.Lines=
13 17 31
17 31 5
31 5 8
8 77 68
68 21 71
21 71 40
71 40 14
40 14 49
14 49 45
49 45 29
45 29 36
29 36 44

First I parsed the lines in the text box to extract the item value. I split the line by the comma and then element (0) is split by the equals sign and trimmed. The result is add to the list. Then looping through the list I use a string builder to build the new lines. The minus 3 avoides an index out of range.
Private Sub ItemArrangement()
Dim lst As New List(Of String)
Dim lines = TextBox1.Lines
For i = 1 To TextBox1.Lines.Count - 1
Dim num = lines(i).Split(","c)(0).Split("="c)(1).Trim
lst.Add(num)
Next
Dim sb As New StringBuilder
For i = 0 To lst.Count - 3
sb.AppendLine($"{lst(i)} {lst(i + 1)} {lst(i + 2)}")
Next
TextBox2.Text = sb.ToString
End Sub
What you showed as output missed the line starting with 5. If this was intentional you will have to adjust the code.
My output in TextBox2 looked like this.
13 17 31
17 31 5
31 5 8
5 8 77
8 77 68
77 68 21
68 21 71
21 71 40
71 40 14
40 14 49
14 49 45
49 45 29
45 29 36
29 36 44

Related

Reverse Number in VB.Net

I want Reverse Number: example:
Textbox1.Text = 2 14 21 22 34 44
a deployment algorithm to do this. make
Expected Output: All Combination possible Reverse.
2 41 21 22 34 44
2 14 12 22 34 44
2 14 21 22 43 44
2 14 21 22 34 44
2 41 12 22 34 44
2 41 12 22 43 44
and so on...
2 14 21 22 34 44
What I try: it works, but it does not carry all the possible combinations, as in the above model.
Dim r As Integer
Public Function Reverse(rn As Integer)
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TextBox1.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Next
Dim numbers = Val(TextBox1.Text)
Dim result As Integer
While numbers > 0
rn = numbers Mod 10
result = result * 10 + rn
numbers = numbers \ 10
End While
Reverse = result
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = Reverse(r & " ")
End Sub

Display commend in ampl

I have a 2 dimension variable in ampl and I want to display it. I want to change the order of the indices but I do not know how to do that! I put my code , data and out put I described what kind of out put I want to have.
Here is my code:
param n;
param t;
param w;
param p;
set Var, default{1..n};
set Ind, default{1..t};
set mode, default{1..w};
var E{mode, Ind};
var B{mode,Var};
var C{mode,Ind};
param X{mode,Var,Ind};
var H{Ind};
minimize obj: sum{m in mode,i in Ind}E[m,i];
s.t. a1{m in mode, i in Ind}: sum{j in Var} X[m,j,i]*B[m,j] -C[m,i] <=E[m,i];
solve;
display C;
data;
param w:=4;
param n:=9;
param t:=2;
param X:=
[*,*,1]: 1 2 3 4 5 6 7 8 9 :=
1 69 59 100 70 35 1 1 0 0
2 34 31 372 71 35 1 0 1 0
3 35 25 417 70 35 1 0 0 1
4 0 10 180 30 35 1 0 0 0
[*,*,2]: 1 2 3 4 5 6 7 8 9 :=
1 64 58 68 68 30 2 1 0 0
2 44 31 354 84 30 2 0 1 0
3 53 25 399 85 30 2 0 0 1
4 0 11 255 50 30 2 0 0 0
The output of this code using glpksol is like tis:
C[1,1].val = -1.11111111111111
C[1,2].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[2,2].val = -1.11111111111111
C[3,1].val = -0.915032679738562
C[3,2].val = -1.11111111111111
C[4,1].val = 0.141414141414141
C[4,2].val = 0.2003367003367
but I want the result to be like this:
C[1,1].val = -1.11111111111111
C[2,1].val = -0.858585858585859
C[3,1].val = -0.915032679738562
C[4,1].val = 0.141414141414141
C[1,2].val = -1.11111111111111
C[2,2].val = -1.11111111111111
C[3,2].val = -1.11111111111111
C[4,2].val = 0.2003367003367
any idea?
You can use for loops and printf commands in your .run file:
for {i in Ind}
for {m in mode}
printf "C[%d,%d] = %.4f\n", m, i, C[m,i];
or even:
printf {i in Ind, m in mode} "C[%d,%d] = %.4f\n", m, i, C[m,i];
I don't get the same numerical results as you, but anyway the output works:
C[1,1] = 0.0000
C[2,1] = 0.0000
C[3,1] = 0.0000
C[4,1] = 0.0000
C[1,2] = 0.0000
C[2,2] = 0.0000
C[3,2] = 0.0000
C[4,2] = 0.0000

Declare (automatically) variables in vba

Is it possible to declare (automatically) variables in vba and with increasing number?
I have to Dim 50 variables, like Var1 till Var50
Is it possible with vba code
All my efforts (for next with concatenate) didn't work
The result should be;
Dim Var1 as integer
Dim Var2 as integer
.
.
Dim Var50 as integer
Use
Dim Var(1 to 50) as integer
Then you can set values for each like so
Var(1) = 1
Var(2) = 2
Var(3) = 3
Var(4) = 4
Var(5) = 5
Var(6) = 6
Var(7) = 7
Var(8) = 8
Var(9) = 9
Var(10) = 10
Var(11) = 11
Var(12) = 12
Var(13) = 13
Var(14) = 14
Var(15) = 15
Var(16) = 16
Var(17) = 17
Var(18) = 18
Var(19) = 19
Var(20) = 20
Var(21) = 21
Var(22) = 22
Var(23) = 23
Var(24) = 24
Var(25) = 25
Var(26) = 26
Var(27) = 27
Var(28) = 28
Var(29) = 29
Var(30) = 30
Var(31) = 31
Var(32) = 32
Var(33) = 33
Var(34) = 34
Var(35) = 35
Var(36) = 36
Var(37) = 37
Var(38) = 38
Var(39) = 39
Var(40) = 40
Var(41) = 41
Var(42) = 42
Var(43) = 43
Var(44) = 44
Var(45) = 45
Var(46) = 46
Var(47) = 47
Var(48) = 48
Var(49) = 49
Var(50) = 50

Grouping identical rows and returning the id of the row with a maximum value

I have a datatable like below:
id desc amt value 1 value 2 value 3 value 4 count Consolidated ID
1 test 23 78 98 30 13 5
2 sample 14 25 45 36 12 24
3 test 23 78 98 30 13 30
4 sample 14 25 45 36 12 20
5 test 23 78 98 30 13 11
I need to
group by the columns desc, amt, value1, value2, value3 and value4, and
return the consolidated ID as the id which has maximum in the count column for each group. Result set should be like below:
id desc amt value 1 value 2 value 3 value 4 count Consolidated ID
1 test 23 78 98 30 13 5 3
3 test 23 78 98 30 13 30 3
5 test 23 78 98 30 13 11 3
2 sample 14 25 45 36 12 24 2
4 sample 14 25 45 36 12 20 2
I know this can be done looping through datatable. But is there a simpler way to do it with LINQ in 1 or 2 statements?
DataTable - dt
Result will be of type IEnumerable of anonymous type in the Select statement, which has same columns as your DataTable
var final = dt.AsEnumerable().GroupBy(x=>
new {
amt = x["amt"],
value1 = x["value1"],
value2 = x["value2"],
value3 = x["value3"],
value4 = x["value4"],
})
.ToDictionary(p=>p.Key,
p=>new {id = p.Select(s=>s["id"]),
cid = p.OrderByDescending(n=>Convert.ToInt32((n["count"]))).Select(s=>s["id"]).First()})
.SelectMany(a=>a.Value.id.Select(h=>new {
h,
a.Key.amt,
a.Key.value1,
a.Key.value2,
a.Key.value3,
a.Key.value4,
ConsolidatedID = a.Value.cid
}));
Also pasting the VB.Net version (using Telerik Code Converter) as that has been the point of contention, though needs verification, as I do not work in VB.Net
Dim final = dt.AsEnumerable().GroupBy(Function(x) New With { _
Key .amt = x("amt"), _
Key .value1 = x("value1"), _
Key .value2 = x("value2"), _
Key .value3 = x("value3"), _
Key .value4 = x("value4") _
}).ToDictionary(Function(p) p.Key, Function(p) New With { _
Key .id = p.[Select](Function(s) s("id")), _
Key .cid = p.OrderByDescending(Function(n) Convert.ToInt32((n("count")))).[Select](Function(s) s("id")).First() _
}).SelectMany(Function(a) a.Value.id.[Select](Function(h) New With { _
h, _
a.Key.amt, _
a.Key.value1, _
a.Key.value2, _
a.Key.value3, _
a.Key.value4, _
Key .ConsolidatedID = a.Value.cid _
}))
If you want to fill the consolidated id column in the datatable, I suggest you first define a local function that returns the "key" of the row -- the values important to grouping:
Dim keyGetter = Function(row As DataRow) New With {
Key .desc = row("desc"),
Key .amt = row("amt"),
Key .value1 = row("value 1"),
Key .value2 = row("value 2"),
Key .value3 = row("value 3"),
Key .value4 = row("value 4")
}
Then, for each group, you can get the id of the row with the maximum count:
Dim results = dt.AsEnumerable.GroupBy(keyGetter, Function(key, grp)
Dim maxCount = grp.Max(Function(row) x("count"))
Return grp.First(Function(row) row("count") = maxCount)
End Function).ToDictionary(Function(x) x.Key, Function(x) x.First)
Then you can iterate over the datatable and fill the column. For each row, generate the key using the keyGetter, and use that to get the consolidated id:
For Each row In dt.AsEnumerable
row("consolidated id") = results(keyGetter(row))
Next

Wrong sum calculation of objects in array

I received code with an object (we'll call it Obj) with a property named Number. An array of these objects is defined with 40 objects in indexes 0..39. I don't need object at index 0.
The code has a static variable saving the sum of all Number's of Obj's in the array. This variable is initialized only once. Right after the Number values themselves are defined.
It usually works, but sometimes I log an exception (which I haven't been able to reproduce) with values of different objects and I see there the value of NumberAll is wrong (I've recently logged 1722 and 2134. Not that it should matter - but the only common prime factor for them is 2. should be 1554, if you were wondering, you can check me below :) ).
EDIT: The exception is an Index was outside the bounds of the array on the loop brought in the code below - a direct effect of this issue.
I've tried two methods of summing, brought below.
Relevant code follows. There is nowhere else in the code that a value is assigned to NumberAll.
Static NotFirstTime As Boolean, NumberAll As Integer
If NotFirstTime = False Then GoTo Data
sPoint: 'Code and more code
' ...
' ...
breakValue = someValue Mod NumberAll
Sum = 0
I = 1
Try
Do
If Sum + myArray(I).Number >= breakValue Then
Exit Do
End If
Sum = Sum + myArray(I).Number
I = I + 1
Loop
Catch ex As Exception
'log error and variable data - breakValue is larger than Sum will ever get
'hence the loop reaches the point of trying myArray(40) which doesn't exist
End Try
' ...
Data:
NotFirstTime = True
myArray(1).Number = 37
myArray(2).Number = 34
myArray(3).Number = 44
myArray(4).Number = 31
myArray(5).Number = 59
myArray(6).Number = 26
myArray(7).Number = 33
myArray(8).Number = 28
myArray(9).Number = 20
myArray(10).Number = 13
myArray(11).Number = 92
myArray(12).Number = 65
myArray(13).Number = 71
myArray(14).Number = 22
myArray(15).Number = 22
myArray(16).Number = 42
myArray(17).Number = 26
myArray(18).Number = 26
myArray(19).Number = 33
myArray(20).Number = 34
myArray(21).Number = 22
myArray(22).Number = 19
myArray(23).Number = 85
myArray(24).Number = 72
myArray(25).Number = 47
myArray(26).Number = 40
myArray(27).Number = 47
myArray(28).Number = 54
myArray(29).Number = 48
myArray(30).Number = 44
myArray(31).Number = 37
myArray(32).Number = 34
myArray(33).Number = 44
myArray(34).Number = 9
myArray(35).Number = 57
myArray(36).Number = 37
myArray(37).Number = 19
myArray(38).Number = 13
myArray(39).Number = 68
NumberAll = 0
'Only one of the following methods is used. They seem to give the same result. The bug exists in both.
'Method one
For I = 1 To 39 'E מספר המסכתות
NumberAll = NumberAll + myArray(I).Number
Next I
'Method two
Dim myList As List(Of Obj) = New List(Of Obj)(myArray)
NumberAll = myList.Sum(Function(b) b.Number)
NumberAll -= myArray(0).Number
GoTo sPoint