Discussion:
Need a script that Reads each line of a file stores each line into two separate arrays based on the first character of each line.
(too old to reply)
Jamn .playit
2017-02-09 16:55:20 UTC
Permalink
I do not have to script very often and as such I was wondering anyone could provide some base syntax for a simple script I need to create.

I have a file that will either start with a '1' or a '2' as the first character of each line.

I need to read the file then put each line that starts with '1' in an array and each that starts with '2' into another. Finally simply write each array into a separate file.

I was using something similar in another script but it has been so long since I had to really code I have forgotten a lot and kind of need this in a hurry, any help would be appreciated. Is it as simple as just using a get left = x?

While expfile.AtEndOfStream = False


expreadline = expfile.readline
'make sure we don't read any blank lines into the array
If instr(expreadline,"") > 0 then
<this is where i need some syntax>



exparray(counter) = expreadline
'exparray2(counter) = expreadline2
'make sure we don't read past the end of our data
If expfile.AtEndOfStream = False then

counter = counter + 1
ReDim Preserve exparray(counter)
Redim Preserve exparray2(counter)

End If
R.Wieser
2017-02-09 17:22:28 UTC
Permalink
Jamn,
Post by Jamn .playit
I need to read the file then put each line that starts with '1' in an
array and each that starts with '2' into another. Finally simply
write each array into a separate file.
Why first in an array and at the end into files. Unless you need to do
something more with those lines (sorting ?) you can just open three files:
one to read from and two to write the results into.

Also, checking for empty lines is not needed: an empty line will not return
either a "1" or a "2". :-)

set Out1 = CreateTextFile("Out1.txt",true)
.. set Out2 = CreateTextFile("Out2.txt",true)
.... Do while not expfile.AtEndOfStream
...... expreadline = expfile.readline
...... if left(expreadline,1)="1" then
........ Out1.WriteLine expreadline
...... elseif left(expreadline,1)="2" then
........ Out2.WriteLine expreadline
...... end if
.... loop
.. set Out2 = nothing
set Out1 = nothing

(dots at the left are used to represent indentation)

Regards,
Rudy Wieser
Post by Jamn .playit
I do not have to script very often and as such I was wondering anyone
could provide some base syntax for a simple script I need to create.
Post by Jamn .playit
I have a file that will either start with a '1' or a '2' as the first character of each line.
I need to read the file then put each line that starts with '1' in an
array and each that starts with '2' into another. Finally simply write each
array into a separate file.
Post by Jamn .playit
I was using something similar in another script but it has been so long
since I had to really code I have forgotten a lot and kind of need this in a
hurry, any help would be appreciated. Is it as simple as just using a get
left = x?
Post by Jamn .playit
While expfile.AtEndOfStream = False
expreadline = expfile.readline
'make sure we don't read any blank lines into the array
If instr(expreadline,"") > 0 then
<this is where i need some syntax>
exparray(counter) = expreadline
'exparray2(counter) = expreadline2
'make sure we don't read past the end of our data
If expfile.AtEndOfStream = False then
counter = counter + 1
ReDim Preserve exparray(counter)
Redim Preserve exparray2(counter)
End If
Jamn .playit
2017-02-09 20:48:08 UTC
Permalink
Post by Jamn .playit
I do not have to script very often and as such I was wondering anyone could provide some base syntax for a simple script I need to create.
I have a file that will either start with a '1' or a '2' as the first character of each line.
I need to read the file then put each line that starts with '1' in an array and each that starts with '2' into another. Finally simply write each array into a separate file.
I was using something similar in another script but it has been so long since I had to really code I have forgotten a lot and kind of need this in a hurry, any help would be appreciated. Is it as simple as just using a get left = x?
While expfile.AtEndOfStream = False
expreadline = expfile.readline
'make sure we don't read any blank lines into the array
If instr(expreadline,"") > 0 then
<this is where i need some syntax>
exparray(counter) = expreadline
'exparray2(counter) = expreadline2
'make sure we don't read past the end of our data
If expfile.AtEndOfStream = False then
counter = counter + 1
ReDim Preserve exparray(counter)
Redim Preserve exparray2(counter)
End If
Thanks very much
R.Wieser
2017-02-10 07:53:41 UTC
Permalink
Jamn,
Post by Jamn .playit
Thanks very much
You're welcome. :-)

Regards,
Rudy Wieser

Loading...