I just looking how to use LINQ for grouping a list.
Class Item
Public col1 As String
Public col2 As String
Public col3 As String
Public description As String
Public Sub New(ByVal col1 As String, ByVal col2 As String,
ByVal col3 As String, ByVal description As String)
Me.col1 = col1
Me.col2 = col2
Me.col3 = col3
Me.description = description
End Sub
End Class
Dim ItemList As New List(Of Item)
ItemList.Add(New Item("A", "A", "A", "1"))
ItemList.Add(New Item("A", "A", "A", "2"))
ItemList.Add(New Item("A", "B", "A", "3"))
ItemList.Add(New Item("A", "B", "A", "4"))
ItemList.Add(New Item("A", "B", "B", "5"))
ItemList.Add(New Item("A", "B", "C", "6"))
Result should be list of 4 items:
'[0] = "A", "A", "A", "1 2"
'[1] = "A", "B", "A", "3 4"
'[2] = "A", "B", "B", "5"
'[3] = "A", "B", "C", "6"
If I understand your requirement to group by the 3 columns and join their descriptions, the following LINQ statement should work:
var query = from item in ItemList
group item by
new { Col1 = item.col1, Col2 = item.col2, Col3 = item.col3 }
into g
select new
{
Col1 = g.Key.Col1,
Col2 = g.Key.Col2,
Col3 = g.Key.Col3,
Description = String.Join(" ", g.Select(xx => xx.description))
};
Class Item
...
Public ReadOnly Property id As String
Get
Return col1 & "_" & col2 & "_" & col3
End Get
End Property
End Class
Dim groups = ItemList.GroupBy(Function(item) item.id)
Dim result = From g in groups
Select New Item(g(0).col1, g(0).col2, g(0).col3,
String.Join(" "c, g.Select(Function(i) i.description)))
Related
I have the following binary string (actually a bit array)
"1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0"
which I want to convert to a byte array.
I need this for an embedded report code functions which only accepts a byte array. I've converted this function from powershell.
I found this but it's C#
string source = "1,1,1,0,0";
byte[] result = source
.Split(',')
.Select(item => byte.Parse(item))
.ToArray();
This does not work in report builder ('Select' is not a member of 'System.Array'.)
Dim source As String = "1,1,1,0,0"
Dim result As Byte() = source.Split(","c).[Select](Function(item) Byte.Parse(item)).ToArray()
If I add System.Linq as suggested I'm still getting an error:
'Select' is not a member of 'Linq'.
Dim source As String() = "1,1,1,0,0".Split(","c)
Dim result As Byte() = System.Linq.Select(Function(source) Byte.Parse(source)).ToArray()
After looking at the function more closely I realized that the input can be any array so I modified the type of the array to string and updated my input accordingly. Tank you guys for trying to help.
Function
Function GetSQLProductKey(ByVal astrBinaryKey As String(), ByVal intVersion As Integer) As String
Dim achrKeyChars As Char() = {"B", "C", "D", "F", "G", "H", "J", "K", "M", "P", "Q", "R", "T", "V", "W", "X", "Y", "2", "3", "4", "6", "7", "8", "9"}
Dim strSQLProductKey As String
Dim iastrBinaryKey As Long
Dim iachrKeyChars As Long
Dim iastrBinaryKeyOuterLoop As Long
Dim iastrBinaryKeyInnerLoop As Long
Try
If (intVersion >= 11) Then
iastrBinaryKey = 0
Else
iastrBinaryKey = 52
End If
For iastrBinaryKeyOuterLoop = 24 To 0 Step -1
iachrKeyChars = 0
For iastrBinaryKeyInnerLoop = 14 To 0 Step -1
iachrKeyChars = iachrKeyChars * 256 Xor astrBinaryKey(iastrBinaryKeyInnerLoop + iastrBinaryKey)
astrBinaryKey(iastrBinaryKeyInnerLoop + iastrBinaryKey) = Math.Truncate(iachrKeyChars / 24)
iachrKeyChars = iachrKeyChars Mod 24
Next iastrBinaryKeyInnerLoop
strSQLProductKey = achrKeyChars(iachrKeyChars) + strSQLProductKey
If (iastrBinaryKeyOuterLoop Mod 5) = 0 And iastrBinaryKeyOuterLoop <> 0 Then
strSQLProductKey = "-" + strSQLProductKey
End If
Next iastrBinaryKeyOuterLoop
Catch
strSQLProductKey = "Cannot decode product key."
End Try
GetSQLProductKey = strSQLProductKey
End Function
Function Call SSRS
Code.GetSQLProductKey(Split(Replace(Fields!ProductKey.Value, " ",""), ","), Left(Fields!Version.Value, 2))
I wish to identify if a cell in a worksheet is found within the merged_cells collection returned by openpyxl.
The merged_cells range looks like this (VSCode debugger):
I have the cell reference J31 - which is found in this collection. How would I write a function that returns true if that cell is found in the merged_cells.ranges collection?
for cell in ^^merged_range^^:
if cell==your_special_cell:
return True
^^merged_range^^ must be of type openpyxl.worksheet.cell_range
Further to D.Banakh's answer (+1), try something like this (building upon a previous example I wrote for someone else, since there is little context to your question):
for cell in ws.merged_cells.ranges:
#print(cellRef +' ==> '+ str(cell.min_row) +'/'+ str(cell.max_row) +'/'+ str(cell.min_col) +'/'+ str(cell.max_col))
if (int(cell.min_row) <= int(row) <= int(cell.max_row)) and (int(cell.min_col) <= int(col) <= int(cell.max_col)):
print('Cell ' +cellRef+ ' is a merged cell')
Example within a context:
import re
cellBorders = fnGetCellBorders(ws, cellRef)
if ('T' in cellBorders) or ('L' in cellBorders) or ('R' in cellBorders) or ('B' in cellBorders) or ('M' in cellBorders):
print('Cell has border *OR* is a merged cell and borders cannot be checked')
def getCellBorders(ws, cellRef):
tmp = ws[cellRef].border
brdrs = ''
if tmp.top.style is not None: brdrs += 'T'
if tmp.left.style is not None: brdrs += 'L'
if tmp.right.style is not None: brdrs += 'R'
if tmp.bottom.style is not None: brdrs += 'B'
if (brdrs == '') and ('condTableTopLeftCell' in refs):
if fnIsCellWithinMergedRange(ws, cellRef): brdrs = 'M'
return brdrs
def fnIsCellWithinMergedRange(ws, cellRef):
ans = False
col = fnAlphaToNum(re.sub('[^A-Z]', '', cellRef))
row = re.sub('[^0-9]', '', cellRef)
for cell in ws.merged_cells.ranges:
if (int(cell.min_row) <= int(row) <= int(cell.max_row)) and (int(cell.min_col) <= int(col) <= int(cell.max_col)):
ans = True
return ans
def fnAlphaToNum(ltr):
ab = ["MT", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
return ab.index(ltr)
References:
OpenPyXL - How to query cell borders?
How to detect merged cells in excel with openpyxl
https://bitbucket.org/openpyxl/openpyxl/issues/911/borders-on-merged-cells-are-not-preserved
I have coded a few programs in console applications for my college projects but now I need to use windows forms to display my results properly. I transferred my console code over, took out the console.writeline(s) and console.readline(s), instead I saved the input from a textbox using:
Dim SoundexString As String = StringInput.Text
Anyway, my code outputted fine in console and I have managed to get other things to output in windows forms using 'messagebox.show(xxx) and 'textbox.text = xxx'. For some reason my current code just won't output the final variable "SoundexCode"... I would be grateful for any advice. Code Below:
Public Class Form1
Public Sub ButtonConvert_Click(sender As Object, e As EventArgs) Handles ButtonConvert.Click
Dim SoundexString As String = StringInput.Text
Dim SoundexCode(5) As Char
For i = 1 To 5
SoundexCode(i) = "0"
Next
GetSoundex(SoundexString, SoundexCode)
MessageBox.Show(SoundexCode)
End Sub
Function GetSoundex(ByVal SoundexString As String, ByRef SoundexCode() As Char)
Dim StringLength As Integer = Len(SoundexString)
Dim LetterArray(StringLength) As Char
'Soundex arrays to check each rule before creating soundex code
Dim SoundexNumbers(StringLength) As Char
Dim DoubleLetters(StringLength) As Char
Dim ConsonantVowel(StringLength) As Char
'Assigning Number Locations to Individual Letters, Each Letter is in Correct Position
For i = 1 To StringLength
LetterArray(i) = CChar(SoundexString(i - 1))
Next
'1. Soundex Letters into Numbers
GetSoundexNumbers(LetterArray, SoundexNumbers, StringLength)
'2. Names with Double Letters / Double Soundex Numbers
RemoveDuplicateLetters(LetterArray, DoubleLetters, StringLength, SoundexNumbers)
'3. Consonant Vowel Seperation
SeperateConsonantVowel(LetterArray, StringLength, SoundexNumbers, ConsonantVowel)
'4. Creating Soundex
GetSoundexCode(LetterArray, StringLength, SoundexNumbers, DoubleLetters, ConsonantVowel, SoundexCode)
Return SoundexCode
End Function
Function GetSoundexNumbers(ByVal LetterArray() As Char, ByRef SoundexNumbers() As Char, ByVal StringLength As Integer)
For i = 1 To StringLength
SoundexNumbers(i) = LetterArray(i)
Next
For i = 1 To StringLength
Select Case SoundexNumbers(i)
Case "b", "f", "p", "v", "B", "F", "P", "V"
SoundexNumbers(i) = "1"
Case "c", "g", "j", "k", "q", "s", "x", "z", "C", "G", "J", "K", "Q", "S", "X", "Z"
SoundexNumbers(i) = "2"
Case "d", "t", "D", "T"
SoundexNumbers(i) = "3"
Case "l", "L"
SoundexNumbers(i) = "4"
Case "m", "M", "n", "N"
SoundexNumbers(i) = "5"
Case "r", "R"
SoundexNumbers(i) = "6"
'Unwanted Cases - Vowels / Phonetic Vowels
Case "a", "e", "i", "o", "u", "h", "w", "y", "A", "E", "I", "O", "U", "H", "W", "Y"
SoundexNumbers(i) = "!"
End Select
Next
Return SoundexNumbers
End Function
Function RemoveDuplicateLetters(ByVal LetterArray() As Char, ByRef DoubleLetters() As Char, ByVal StringLength As Integer, ByVal SoundexNumbers() As Char)
For i = 1 To StringLength
DoubleLetters(i) = LetterArray(i)
Next
'Checking Double Letters
For i = 1 To StringLength
If i < StringLength Then
If DoubleLetters(i) = DoubleLetters(i + 1) Then
DoubleLetters(i + 1) = "!"
End If
End If
Next
'Checking Double Soundex Numbers
For i = 1 To StringLength
If i < StringLength Then
If SoundexNumbers(i) = SoundexNumbers(i + 1) Then
DoubleLetters(i + 1) = "!"
End If
End If
Next
Return DoubleLetters
End Function
Function SeperateConsonantVowel(ByVal LetterArray() As Char, ByVal StringLength As Integer, ByVal SoundexNumbers() As Char, ByRef ConsonantVowel() As Char)
For i = 1 To StringLength
ConsonantVowel(i) = LetterArray(i)
Next
'Checking that a Vowel does not have Letters both Sides which Share same Soundex Number
For i = 1 To StringLength
If i > 1 And i < StringLength Then
If ConsonantVowel(i) = "a" Or ConsonantVowel(i) = "e" Or ConsonantVowel(i) = "i" Or ConsonantVowel(i) = "o" Or ConsonantVowel(i) = "u" Or ConsonantVowel(i) = "A" Or ConsonantVowel(i) = "E" Or ConsonantVowel(i) = "I" Or ConsonantVowel(i) = "O" Or ConsonantVowel(i) = "U" Then
If SoundexNumbers(i - 1) = SoundexNumbers(i + 1) Then
ConsonantVowel(i - 1) = "!"
End If
End If
End If
Next
Return ConsonantVowel
End Function
Function GetSoundexCode(ByVal LetterArray() As Char, ByVal StringLength As Integer, ByVal SoundexNumbers() As Char, ByVal DoubleLetters() As Char, ByVal ConsonantVowel() As Char, ByRef SoundexCode() As Char)
SoundexCode(1) = LetterArray(1)
SoundexCode(2) = "-"
Dim Counter As Integer
Dim CounterStore As Integer
For Counter = 2 To StringLength
If SoundexNumbers(Counter) <> "!" And DoubleLetters(Counter) <> "!" And ConsonantVowel(Counter) <> "!" Then
SoundexCode(3) = SoundexNumbers(Counter)
CounterStore = Counter
Exit For
End If
Next
If CounterStore < StringLength Then
For Counter = CounterStore + 1 To StringLength
If SoundexNumbers(Counter) <> "!" And DoubleLetters(Counter) <> "!" And ConsonantVowel(Counter) <> "!" Then
SoundexCode(4) = SoundexNumbers(Counter)
CounterStore = Counter
Exit For
End If
Next
ElseIf CounterStore = StringLength Then
SoundexCode(4) = "0"
SoundexCode(5) = "0"
End If
If CounterStore < StringLength Then
For Counter = CounterStore + 1 To StringLength
If SoundexNumbers(Counter) <> "!" And DoubleLetters(Counter) <> "!" And ConsonantVowel(Counter) <> "!" Then
SoundexCode(5) = SoundexNumbers(Counter)
CounterStore = Counter
Exit For
End If
Next
ElseIf CounterStore = StringLength Then
SoundexCode(5) = "0"
End If
Return SoundexCode
End Function
End Class
Strings are 0-based arrays (first letter starts at index 0) but SoundexCode is a 1-based array (ie. index 0 is null) so when it's converted to a string, SoundexCode appears as an empty string.
To fix your code, just use the String(value As Char(), startIndex As Integer, length As Integer) constructor instead of the regular one.
ie.
MessageBox.Show(New String(SoundexCode, 1, 5))
I have a matrix (5x5) with values in them for example:
Matrix (1,1) Value: 'a'
Matrix (1, 2) Value: 'b'
Matrix (2, 1) Value: 'c'
how would I be able to find the letter 'a' in that matrix and have it output the coordinates?
ie
user inputs 'b'
[searches for 'b' in table]
output (1,2)
thanks in advance
It's as simple as:
For i As Integer = 0 To LengthOfMatrix - 1
For y As Integer = 0 To HeightOfMatrix - 1
If Matrix(i, y) = "a" Then Console.Write(i & " " & y & vbCrLf)
Next
Next
Asuming that you declared Matrix as:
Dim Matrix As Char(,) = {{"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d", "e"}}
And LengthOfMatrix And HeightOfMatrix should be the dimentions of your matrix. They could be switched to something more dynamic like:
For i As Integer = 0 To Matrix.GetLength(0) - 1 'Get's the length of the first dimension
For y As Integer = 0 To Matrix.GetLength(1) - 1 'Get's the length of the second dimension
If Matrix(i, y) = "a" Then Console.Write(i & " " & y & vbCrLf)
Next
Next
In a short description, all that this loop does is it goes through all of the elements of the matrix and outputs the coordinates of every element that matches a certain criteria (In this case - equals to 'a').
Note: In most programming languages array's indexes begin from 0, so the first element in your matrix will be at coords (0,0).
I tried to incorporate the following into SSRS but failed.
If XXX = “A” then display “AT”
Else if XXX = “B” then display “BEE”
Else if XXX = “C” then display “CAR”
Else display “Other”
I tried
=Switch(
Fields!XXX.Value = "A", "AT",
Fields!XXX.Value = "B", "BEE",
Fields!XXX.Value = "C", "CAR", "Other")
You almost had it. For every output in the Switch function must be paired with a condition. Just make your last condition evaluate to True.
=Switch(
Fields!XXX.Value = "A", "AT",
Fields!XXX.Value = "B", "BEE",
Fields!XXX.Value = "C", "CAR",
True, "Other"
)
You want something like this:
=iif(Fields!XXX.Value = "A", "AT", iif(Fields!XXX.Value = "B", "BEE", iif(Fields!XXX.Value = "C", "CAR", "Other")))
[check the parens in the expression builder]