• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Work With Files Number 2

I want to save an array with precisely length. For example:
  10,100    
  50,10    
  10,84.0  
  10,10    
If I want to have array with precisely length what about if I have "," .
If for example the lenght is 6 see how the array must be :

  10,100     10,100  
  5,10.0     10,100  
  10,84.     10,100  
  10,1.0     10,100

If you have some questions ask me.
Thanks

0
Nick1_bg
Asked:
Nick1_bg
  • 9
  • 8
1 Solution
 
AzraSoundCommented:
i'm trying to see what you want to do but cant quite make it out...can you try and give some more information?
0
 
Nick1_bgAuthor Commented:
If my output array is like this:
  10,100    
  50,10      
  10,84.0    
  10,10      
I want to convert it in :
  10,100     10,100  
  5,10.0     10,100  
  10,84.     10,100  
  10,1.0     10,100
But the problem is in "." how can I know if there is a comma or not?
And how can I add some "0" zeros?
Thanks


0
 
AzraSoundCommented:
ok i'm still a bit unclear, sorry, but let me try and give you some information about how to attack the problem I think you are having...

To know if there is a comma in a string then you use the Instr function, for example

Dim i as Integer

i = InStr(1,"this,comma",",")

this will return 5 into the variable 'i' since it found a comma in the 5th position...if it doesnt find a comma it will return 0. So to check if there is a comma,

Dim i As Integer
i = InStr(1, AString, ",")
If i = 0 Then
  Debug.Print "No comma found"
Else
  Debug.Print "Comma found at position " & i
End If


i hope this maybe gives you some direction...please keep me informed
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Nick1_bgAuthor Commented:
Adjusted points from 50 to 100
0
 
Nick1_bgAuthor Commented:
I want to output data with precisely length for example:
If there is no "." I want automatic to add "." and to add some "0". See the example:
Now I output this kind of data:

1   10,30
2   20.2,29.0
3   40.89,28.595

I want this data to be with precisely=6
see how can be :

1   10.000,30.000
2   20.200,29.000
3   40.890,28.595

That's all
Thanks for pasience AzraSound.
0
 
AzraSoundCommented:
ok i think i see what you are doing now...if you can just give me a couple more pieces of information I will write the code for you.  
1) is this information is stored in a file?  or is it in an array?

2)can you give me an example of how the data is stored.  for example, if its in a file I assume it is like this

10,100
20.2,29.0

but if its in an array how do you have it stored in the array"

3)  what version of VB do you have?
0
 
Nick1_bgAuthor Commented:
I store the date in file and I am using VB 5.0.
This is how the data must be stored /all fields to be with the same lenght here the precision is 6/
1   10.000,30.000
2   20.200,29.000
3   40.890,28.595
If there is no "." the program must put it auto. and to add "0"
Thanks

0
 
AzraSoundCommented:
ok here is quick sample, tell me if it is what you need, add a command button and a listbox to the form, and change the file's path to the one you need to reference...


Dim arr As Variant

Private Sub Command1_Click()
    Dim FileNumber As Integer
    Dim i As Integer
    Dim temp As String
    Dim str As String
    Dim pos As Long
    Dim A$
    FileNumber = FreeFile
    Open "C:\Windows\Desktop\test.txt" For Input As FileNumber
    Do While Not EOF(FileNumber)
        On Error GoTo Skip
        Line Input #FileNumber, A$
        arr = SplitFunc(A$, ",")
        For i = 0 To 1
            pos = InStr(1, arr(i), ".")
            If pos = 0 Then
                arr(i) = arr(i) & "."
                Do
                    If Len(arr(i)) = 6 Then Exit Do
                    arr(i) = arr(i) & "0"
                Loop
            Else
                Do
                    If Len(arr(i)) = 6 Then Exit Do
                    arr(i) = arr(i) & "0"
                Loop Until Len(arr(i)) = 6
            End If
        Next
        str = ""
        For i = 0 To UBound(arr)
            str = str & arr(i) & " "
        Next
        List1.AddItem str
    Loop
Skip:
    Close FileNumber
End Sub


Private Function SplitFunc(Expression As String, Delimiter As String) As Variant
    Dim i As Long
    Dim lNextPos As Long
    Dim sText As String
    Dim lCount As Long
    Dim varTemp() As String
    For i = 1 To Len(Expression)
         
        lNextPos = InStr(i + 1, Expression, Delimiter)
        If lNextPos = 0 Then
            lNextPos = Len(Expression)
        End If
        sText = Mid$(Expression, i, lNextPos - i)
        ReDim Preserve varTemp(lCount)
        varTemp(lCount) = sText
        lCount = lCount + 1
        i = lNextPos + Len(Delimiter) - 1
         
    Next
    SplitFunc = varTemp
End Function
0
 
Nick1_bgAuthor Commented:
AzraSound it is working but I want to output it in file and what about if I am having an array, for example:

1   10.000,30.000   40.890,28.595
2   20.200,29.000   20.200,29.000  
3   40.890,28.595   10.000,30.000  

Thanks
0
 
AzraSoundCommented:
how are the arrays spaced apart?
0
 
Nick1_bgAuthor Commented:
1            ,             0,0           ,             0,0           ,             0,0           ,             0,0
 2            ,             0.5,0.5       ,             1.5,0.5       ,             1.5,-0.5      ,             0.5,-0.5
 3            ,             1.5,0.5       ,             2.5,0.5       ,             2.5,-0.5      ,             1.5,-0.5
 4            ,             2.5,0.5       ,             3.5,0.5       ,             3.5,-0.5      ,             2.5,-0.5
 5            ,             3.5,0.5       ,             4.5,0.5       ,             4.5,-0.5      ,             3.5,-0.5
 6            ,             4.5,5         ,             14.5,5        ,             14.5,-5       ,             4.5,-5
 7            ,             14.5,5        ,             24.5,5        ,             24.5,-5       ,             14.5,-5
 8            ,             24.5,5        ,             34.5,5        ,             34.5,-5       ,             24.5,-5
 9            ,             34.5,5        ,             44.5,5        ,             44.5,-5       ,             34.5,-5
 10           ,             44.5,5        ,             54.5,5        ,             54.5,-5       ,             44.5,-5
 11           ,             54.5,5        ,             64.5,5        ,             64.5,-5       ,             54.5,-5
 12           ,             64.5,5        ,             74.5,5        ,             74.5,-5       ,             64.5,-5
 13           ,             74.5,5        ,             84.5,5        ,             84.5,-5       ,             74.5,-5
 14           ,             84.5,5        ,             94.5,5        ,             94.5,-5       ,             84.5,-5
 15           ,             94.5,5        ,             104.5,5       ,             104.5,-5      ,             94.5,-5
 16           ,             104.5,5       ,             114.5,5       ,             114.5,-5      ,             104.5,-5
0
 
Nick1_bgAuthor Commented:
1            ,             0,0           ,             0,0           ,             0,0           ,             0,0
 2            ,             0.5,0.5       ,             1.5,0.5       ,             1.5,-0.5      ,             0.5,-0.5
 3            ,             1.5,0.5       ,             2.5,0.5       ,             2.5,-0.5      ,             1.5,-0.5
 4            ,             2.5,0.5       ,             3.5,0.5       ,             3.5,-0.5      ,             2.5,-0.5
 5            ,             3.5,0.5       ,             4.5,0.5       ,             4.5,-0.5      ,             3.5,-0.5
 6            ,             4.5,5         ,             14.5,5        ,             14.5,-5       ,             4.5,-5
 7            ,             14.5,5        ,             24.5,5        ,             24.5,-5       ,             14.5,-5
 8            ,             24.5,5        ,             34.5,5        ,             34.5,-5       ,             24.5,-5
 9            ,             34.5,5        ,             44.5,5        ,             44.5,-5       ,             34.5,-5
 10           ,             44.5,5        ,             54.5,5        ,             54.5,-5       ,             44.5,-5
 11           ,             54.5,5        ,             64.5,5        ,             64.5,-5       ,             54.5,-5
 12           ,             64.5,5        ,             74.5,5        ,             74.5,-5       ,             64.5,-5
 13           ,             74.5,5        ,             84.5,5        ,             84.5,-5       ,             74.5,-5
 14           ,             84.5,5        ,             94.5,5        ,             94.5,-5       ,             84.5,-5
 15           ,             94.5,5        ,             104.5,5       ,             104.5,-5      ,             94.5,-5
 16           ,             104.5,5       ,             114.5,5       ,             114.5,-5      ,             104.5,-5
0
 
AzraSoundCommented:
well i hope this isnt super urgent for you...i am not going to have a chance to work on this until next weekend.  hopefully with the code i provided you it will give you some direction that you can get it to work yourself.  if not, you will hear from me in about a week.  cheers.
0
 
AzraSoundCommented:
btw...how far apart are these values placed?  they seem awfully far apart above...it needs to be a set space...is it a tab? can you reduce it to space-delimited?  let me know
0
 
Nick1_bgAuthor Commented:
you can place it without space:
1,234,234,345,353,345
2,234,234,345,353,345
3,234,234,345,353,345
4,234,234,345,353,345
5,234,234,345,353,345
6,234,234,345,353,345

0
 
AzraSoundCommented:
ok i just copied those values into a text file and it worked...what the code does is create a new text file to hold the values so you can specify where and what to call the file:

Option Explicit
Dim Arr As Variant
Dim filesys As Object
Dim newFile As Object

Private Sub Command1_Click()
    Dim FileNumber As Integer
    Dim i As Integer
    Dim temp As String
    Dim str As String
    Dim pos As Long
    Dim A$
    Set filesys = CreateObject("Scripting.FileSystemObject")
    Set newFile = filesys.CreateTextFile("C:\Windows\Desktop\test2.txt")
    FileNumber = FreeFile
    Open "C:\Windows\Desktop\test.txt" For Input As FileNumber
    Do While Not EOF(FileNumber)
        On Error GoTo Skip
        Line Input #FileNumber, A$
        Arr = SplitFunc(A$, ",")
        For i = 0 To UBound(Arr)
            pos = InStr(1, Arr(i), ".")
            If pos = 0 Then
                Arr(i) = Arr(i) & "."
                Do
                    If Len(Arr(i)) = 6 Then Exit Do
                    Arr(i) = Arr(i) & "0"
                Loop
            Else
                Do
                    If Len(Arr(i)) = 6 Then Exit Do
                    Arr(i) = Arr(i) & "0"
                Loop Until Len(Arr(i)) = 6
            End If
        Next
        str = ""
        str = JoinFunc(Arr, ",")
        newFile.WriteLine str
    Loop
Skip:
    Close FileNumber
    newFile.Close
End Sub


Private Function SplitFunc(Expression As String, Delimiter As String) As Variant
    Dim i As Long
    Dim lNextPos As Long
    Dim sText As String
    Dim lCount As Long
    Dim varTemp() As String
    For i = 1 To Len(Expression)
         
        lNextPos = InStr(i + 1, Expression, Delimiter)
        If lNextPos = 0 Then
            lNextPos = Len(Expression)
        End If
        sText = Mid$(Expression, i, lNextPos - i)
        ReDim Preserve varTemp(lCount)
        varTemp(lCount) = sText
        lCount = lCount + 1
        i = lNextPos + Len(Delimiter) - 1
    Next
    SplitFunc = varTemp
End Function

Private Function JoinFunc(Arr As Variant, Optional Delimiter As String) As String
    Dim i As Integer
    Dim tmpString As String
    For i = 0 To (UBound(Arr) - 1)
        tmpString = tmpString & Arr(i) & Delimiter
    Next
    JoinFunc = tmpString & Arr(UBound(Arr))
End Function

0
 
Nick1_bgAuthor Commented:
10x man it is working :-))))
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now