Solved

# Work With Files Number 2

Posted on 2000-05-01
Medium Priority
156 Views
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
Question by:Nick1_bg
• 9
• 8

LVL 28

Expert Comment

ID: 2768022
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

LVL 1

Author Comment

ID: 2771189
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

LVL 28

Expert Comment

ID: 2771431
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

LVL 1

Author Comment

ID: 2778808
Adjusted points from 50 to 100
0

LVL 1

Author Comment

ID: 2778809
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

LVL 28

Expert Comment

ID: 2778856
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

LVL 1

Author Comment

ID: 2779047
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

LVL 28

Expert Comment

ID: 2779121
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
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

LVL 1

Author Comment

ID: 2788942
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

LVL 28

Expert Comment

ID: 2789024
how are the arrays spaced apart?
0

LVL 1

Author Comment

ID: 2789085
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

LVL 1

Author Comment

ID: 2789095
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

LVL 28

Expert Comment

ID: 2789212
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

LVL 28

Expert Comment

ID: 2791106
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

LVL 1

Author Comment

ID: 2794615
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

LVL 28

Accepted Solution

AzraSound earned 400 total points
ID: 2794763
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

LVL 1

Author Comment

ID: 2798393
10x man it is working :-))))
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.