Hi below is the data sample for chart data which I read from URL
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
Now in this I want to save this data into a csv file as below
Time A B c
25 3 1 8
75 8 9 7
100 6 7 3
125 7 8 9
150 5 4 56
175 3 7 9
200 2 2 111
225 7 5 8
Actually I have to first read the data into an array in which each element will contain one line of the data file, Now I will have to split the each element by space and store it in a two dimensional array, now for each ith index of each array I have to create a coma separated line then put "\n" at the end of line , and at last save this to a csv file . I don't know much about the syntax of vb-script and classic asp that's why I am facing this problem . Please help me
To get you started:
Sub doReOrder(sFSpecI, sFSpecO, sCol1)
Dim oTS : Set oTS = goFS.OpenTextFile(sFSpecI)
Dim sData : sData = oTS.ReadLine() ' ARRAY ' ' 3 8
Dim aParts : aParts = Split(sData, " ")
Dim nCols : nCols = CByte(aParts(3)) ' Count vs UBound vs Data!
Dim nRows : nRows = CByte(aParts(4))
Dim nRows2 : nRows2 = nRows + 1
oTS.SkipLine ' Y
' get table in one string, prepend col1 name, clean '
sData = "'" & sCol1 & "' " & Replace(oTS.ReadAll(), vbCrLf, " ")
sData = Trim(Replace(sData, "'", """"))
' get table in flat array
aParts = Split(sData, " ")
oTS.Close
Set oTS = goFS.CreateTextFile(sFSpecO, True)
' WScript.Echo Join(aParts, "|")
ReDim aData(nCols) ' hold one (new) row to prep for Join
Dim nRow
For nRow = 0 To nRows
Dim nCol
For nCol = 0 To nCols
' magic column hopping
aData(nCol) = aParts(nRow + nCol * nRows2)
Next
oTS.WriteLine Join(aData, ",")
Next
oTS.Close
End Sub
Test:
Dim sFSpecI : sFSpecI = "..\Data\f1.txt"
Dim sFSpecO : sFSpecO = "..\Data\f1.csv"
Dim sCol1 : sCol1 = "Time" ' dangerous - possibly reserved in SQL
WScript.Echo goFS.OpenTextFile(sFSpecI).ReadAll()
doReOrder sFSpecI, sFSpecO, sCol1
WScript.Echo goFS.OpenTextFile(sFSpecO).ReadAll()
Output:
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
"Time","A","B","C"
25,3,1,8
75,8,9,7
100,6,7,3
125,7,8,6
150,5,4,56
175,3,7,9
200,2,2,111
225,7,5,8
Related
I'm trying to list all numbers with 3 digits where the individual digits sum to a given number.
So far I can return a list of all numbers using this Visual Basic code:
target = 17
i = 1
j = 1
k = 1
Do While i < 10
Do While j < 10
Do While k < 10
r = i + j + k
If r = target Then
If i <> j And j <> k And k <> i Then
lsNumbers.Add(i & j & k )
End If
End If
k += 1
Loop
If k = 10 Then k = 1
j += 1
Loop
If j = 10 Then j = 1
i += 1
Loop
But I want only unique, non repeating combinations.
For example for the target number 17:
179, 197, 269, 278, 287...
I want to be able to test the current number before I add it to the list, to check if it is a combination of a number already in the list - so 197 would fail because of 179, and 287 would fail because of 278
Observations
Just curious, is excluding the 0 digit on purpose?
To iterate through the possible digits, a well suited instruction pair is FOR NEXT. Definitely simpler than the DO WHILE that you used.
Loop
If k = 10 Then k = 1
Loop
If j = 10 Then j = 1
Upon loop completion, the iterator is sure to contain 10. The IF is redundant.
Solution
In order to check if a number, that obeys the condition, is unique in the sense that it is not composed of the same 3 digits as an already validated number, you could consult a 3-D array. If the new number corresponds to a non-zero element in this array, it means that the new number would be using the same digits as an earlier number. That's reason to reject it.
Next code runs in QBasic. You'll have no trouble rewriting it for Visual BASIC.
DIM r%(1 TO 9, 1 TO 9, 1 TO 9)
FOR i% = 1 TO 9
FOR j% = 1 TO 9
FOR k% = 1 TO 9
r%(i%, j%, k%) = 0
NEXT
NEXT
NEXT
target% = 17
FOR i% = 1 TO 9
FOR j% = 1 TO 9
FOR k% = 1 TO 9
IF i% + j% + k% = target% THEN
IF r%(i%, j%, k%) = 0 THEN
PRINT i% * 100 + j% * 10 + k%; " ";
r%(i%, j%, k%) = 1 ' Could do without this one because of the ascending order
r%(i%, k%, j%) = 1
r%(j%, i%, k%) = 1
r%(j%, k%, i%) = 1
r%(k%, i%, j%) = 1
r%(k%, j%, i%) = 1
END IF
END IF
NEXT
NEXT
NEXT
This is my output of valid numbers:
179 188 269 278 359 368 377 449 458 467 557 566
I have Textbox1.Lines (separated by a space)
5 7 10 12 12
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
1 1 7 14 19
2 4 7 8 10
How should I remove duplicates from each line and display something like this:
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
2 4 7 8 10
that is, to remove from the entire line that contains a duplicate number on the same line.
Private Sub BttGamblerInput_Click(sender As Object, e As EventArgs) Handles BttGamblerInput.Click
On Error Resume Next
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TxtGamblerImput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Not worked.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each line In TxtGamblerOutput.Text
Dim originalArray = TxtGamblerOutput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Dim checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length Then
TxtGamblerOutput.Text = checkArray
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
End Sub
Code 3:
Dim numbers() As Integer = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}
' get the duplicates
Dim dups As New Dictionary(Of Integer, Integer) ' number, count of number
For i = 0 To numbers.Length - 1
If dups.ContainsKey(numbers(i)) Then
dups(numbers(i)) += 1
Else
dups.Add(numbers(i), 1)
End If
Next
' display them
For Each dup In dups
If dup.Value > 1 Then
Console.WriteLine(dup.Key)
End If
Next
Console.ReadLine()
I haven't coded or tested anything, but you can use the Distinct method from List(of T) and compare the length of the output to the current List. Another approach is to use an ArrayList (c.f. https://stackoverflow.com/a/13428210/9101981 for the bones of getting unique elements).
Pseudo-ish Code using an Array:
for each line in the text input
originalArray = line.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length then
' Your code here to display the valid line
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
Some additional reading:
https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8
and, in particular, note the recommendation to use List.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8
noting that this is the Linq Distinct documentation
I have the following number strings:
Textbox1.text / or Textbox1.Line= 1,2,3,19,29,78,48,39,40,51,53,54,69,70,71,73
Textbox2.text= / or Textbox2.Line= 1,9,3,31,29,78,45,39,40,51,59,54,69,70,71,73
textbox3.text= / or TextBox3.Line= 11,4,3,31,29,78,45,39,40,53,59,54,6974,75,76
and Others ...
How can I make a Count that shows how many numbers from 1 to 10 are in the Textbox, how many numbers from 11-20, how many numbers from 31-40, and so on. Example: On line 1 - we will have 3 small numbers from 1 to 10 (1,2,3).
To do this You will have to split the string into an array or list then compare the values in the array or list to the numbers you want. like this
Dim arr1 As New List (Of String)
arr1.AddRange(Split (TextBox1.Text, ","))
Dim final As String
Dim count As Integer = 0
For Each item As String In arr1
If CInt(item) >= 1 And CInt(item) <= 10 Then
count+=1
'replace 10 with the maximum number you want and 1 with the minimum number.
final&=item & " "
End If
Next
Msgbox("There are " & count & "numbers" & final)
You can easily convert a string containing a comma-separated list of integers into a string array like this
Dim s = "1,2,3,19,29,78,48,39,40,51,53,54,69,70,71,73"
Dim parts = s.Split(","c)
Then convert the string array into a list of integers
Dim numbers = New List(Of Integer)
For Each p As String In parts
Dim i As Integer
If Integer.TryParse(p, i) Then
numbers.Add(i)
End If
Next
Now comes the counting part. With LINQ you can write
Dim tens = From n In numbers
Group n By Key = (n - 1) \ 10 Into Group
Order By Key
Select Text = $"{ 10 * Key + 1} - {10 * Key + 10}", Count = Group.Count()
This
For Each x In tens
Console.WriteLine($"{x.Text} --> {x.Count}")
Next
Prints
1 - 10 --> 3
11 - 20 --> 1
21 - 30 --> 1
31 - 40 --> 2
41 - 50 --> 1
51 - 60 --> 3
61 - 70 --> 2
71 - 80 --> 3
you will probably have to run the program to understand, but i want my loop:
For i = 1 To final
txttable.Text += vbCrLf + CStr(i) + " "
Next
to be in the same line as the rest.
so it looks something like this if i enter 5
5
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 8 12 15
... to 5
txttable.Text is a textbox
maybe i will have to rewrite some of my code to make it work, dont be shy if you have any suggestions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim final As Integer
Integer.TryParse(txtfinal.Text, final)
Select Case final
Case 2 To 20
txttable.Text = (final).ToString + vbCrLf
For i = 1 To final
txttable.Text += vbCrLf + CStr(i) + " "
Next
For i = 1 To final
txttable.Text += vbTab + CStr(i) + " "
Next
txttable.AppendText(Environment.NewLine)
txttable.AppendText(Environment.NewLine)
For ligne As Integer = 1 To final
For col As Integer = 1 To final
txttable.Text += vbTab + (ligne * col).ToString
Next
txttable.Text += vbCrLf
Next
Case Else
MessageBox.Show("veuillez entrez une valeur entre 2 et 20 inclusivement")
txtfinal.Clear()
txtfinal.Focus()
End Select
End Sub
End Class
Try something more like...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim final As Integer
If Integer.TryParse(txtfinal.Text, final) Then
If final >= 2 AndAlso final <= 20 Then
Dim sb As New System.Text.StringBuilder
sb.AppendLine(final.ToString)
For col As Integer = 1 To final
sb.Append(vbTab & col.ToString() & " ")
Next
sb.Append(vbCrLf & vbCrLf)
For row As Integer = 1 To final
sb.Append(row.ToString)
For col As Integer = 1 To final
sb.Append(vbTab & (row * col).ToString)
Next
sb.Append(vbCrLf)
Next
txttable.Text = sb.ToString
Exit Sub
End If
End If
MessageBox.Show("veuillez entrez une valeur entre 2 et 20 inclusivement")
txtfinal.Clear()
txtfinal.Focus()
End Sub
Which produced:
5
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
when 5 was entered.
I have a textbox that the user puts in sets of numbers(e.g. 32 45 98 56 52 1 23) and I need to copy these numbers into a listbox so that each number is its own item. So far I have this
For Each ch As Char In TextBox20.Text
If Char.IsDigit(ch) Then
ListBox1.Items.Add(ch)
End If
Next
but the problem is that it will copy each digit as an item so we will end up with
3
2
4
5
9
8
5
6
I need it to copy them like this
32
45
98
56
here is a sample of how to do that
Sub addToListBox()
Dim sample As String
Dim v As Variant
Dim i As Integer
sample = "32 45 98 56 52 1 23"
v = Split(sample, " ")
For i = 0 To UBound(v)
If IsNumeric(v(i)) Then
ListBox1.Items.Add(v(i))
End If
Next i
End Sub