Page 1 of 1 1
Topic Options
#125688 - 2004-08-26 08:49 AM Listing textfile
Transalp Offline
Lurker

Registered: 2004-08-26
Posts: 4
Hello,

I'm having a few files with textlines. Some of the lines are the same in these files. I need to have a new file with each line only 1 time.

A little example:

textfile1.txt
-------------
string1
string2
string3

textfile2.txt
-------------
string3
string4
string5

textfile3.txt
-------------
string2
string4

result.txt
----------
string1
string2
string3
string4
string5

Could someone please help me?


Edited by Transalp (2004-08-26 09:30 AM)

Top
#125689 - 2004-08-26 09:07 AM Re: Listing textfile
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Approximately how many unique lines in total will there be?

If it's no more than (say) 10,000 you can load each line into an array. Search the array for the string, if it's not present add it and write to your new file otherwise ignore it.

You can do exactly the same thing with ReadProfileString() and WriteProfileString(), and these are usually easier for starters to understand.

If you have a lot of uniques you may get into memory trouble with arrays.

In this case:
  1. Catenate all the files together using DOS COPY (the "+" switch)
  2. User DOS SORT to sort the catenated file.
  3. Read each line in your sorted file. If it is the same as the last line ignore it, otherwise write it to your new file.

Top
#125690 - 2004-08-26 09:25 AM Re: Listing textfile
Transalp Offline
Lurker

Registered: 2004-08-26
Posts: 4
Quote:

Approximately how many unique lines in total will there be?

If it's no more than (say) 10,000 you can load each line into an array. Search the array for the string, if it's not present add it and write to your new file otherwise ignore it.




Let's say 100 lines max.

Top
#125691 - 2004-08-26 09:42 AM Re: Listing textfile
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
That makes it simpler.

You know you have a pretty small data set, so
  1. Dimension your array up front, say 1000 elements.
  2. Read each file in turn, and for each line read...
  3. If the line exists in the array then ignore it.
  4. If the line does not exist, add it to the array and write it to your output file.


Job done, easy eh?

Look up information on the following commands in the manual:
DIM, Dir(), Open(), ReadLine(), WriteLine(), Ascan().

Have a go, and post again if you start to struggle.

Top
#125692 - 2004-08-26 10:36 AM Re: Listing textfile
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I struggled a bit with it and came out with a weird execute() construct ...
Advantage of this is that it doesn't matter how many files and lines you have ... Memory is the limit so to say


Code:
break on

$Out = @scriptdir + "\result.txt"
$FileArray = @scriptdir + "\file1.txt",@scriptdir + "\file2.txt",@scriptdir + "\file3.txt"
Global $OutStrArray[0]

for $i = 0 to ubound($FileArray)
$_ = execute("Global $$StringArray$i[0]")
next

for $i = 0 to ubound($FileArray)
$_ = open(1,$FileArray[$i])
$c = 0
$l = readline(1)
while not @error
$_ = execute("redim preserve $$StringArray$i[$$c]")
$_ = execute("$$StringArray$i[$$c] = $$l")
$c = $c + 1
$l = readline(1)
loop
$_ = close(1)
next

for $i = 0 to ubound($FileArray)
$_ = execute("for $$j = 0 to ubound($$StringArray$i)
if ascan($$OutStrArray,$$StringArray$i[$$j]) = -1
$$OutStrArray[ubound($$OutStrArray)] = $$StringArray$i[$$j]
redim preserve $$OutStrArray[ubound($$OutStrArray)+1]
endif
next")
next

if ubound($OutStrArray) redim preserve $OutStrArray[ubound($OutStrArray)-1] endif

if exist($out) del $out endif

$_ = open(1,$out,5)

for each $e in $OutStrArray
$_ = writeline(1,$e + @crlf)
next

$_ = close(1)

exit 1


_________________________



Top
#125693 - 2004-08-26 02:17 PM Re: Listing textfile
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
And here's a slightly simpler solution

Code:

break on

dim $Out, $FileArray, $file
dim $outArray, $line
$Out = @scriptdir + "\result.txt"
$FileArray = @scriptdir + "\file1.txt",@scriptdir + "\file2.txt",@scriptdir + "\file3.txt"

for each $file in $FileArray
if open(1,$file) = 0
$line = readline(1)
while @error = 0
if ubound($outArray) < 0
redim $outArray[0]
$outArray[0] = $line
else
if ascan($outArray,$line) < 0
redim preserve $outArray[ubound($outArray)+1]
$outArray[ubound($outArray)] = $line
endif
endif
$line = readline(1)
loop
$ = close(1)
endif
next

if open(1,$Out,5) = 0
for each $line in $outArray
$ = writeline(1, $line + @crlf)
next
$ = close(1)
endif

_________________________
Eric

Top
#125694 - 2004-08-26 03:01 PM Re: Listing textfile
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
macie's right ... was thinking too complex

here's mine slightly more condensed :

Code:
break on


$Out = @scriptdir + "\result.txt"
$FileArray = @scriptdir + "\file1.txt",@scriptdir + "\file2.txt",@scriptdir + "\file3.txt"
Global $OutStrArray[0]
for $i = 0 to ubound($FileArray)
$_ = open(1,$FileArray[$i])
$l = readline(1)
while not @error
if ascan($OutStrArray,$l) = -1
$OutStrArray[ubound($OutStrArray)] = $l
redim preserve $OutStrArray[ubound($OutStrArray)+1]
endif
$l = readline(1)
loop
$_ = close(1)
next

if ubound($OutStrArray) redim preserve $OutStrArray[ubound($OutStrArray)-1] endif

if exist($out) del $out endif

$_ = open(1,$out,5)

for each $e in $OutStrArray
$_ = writeline(1,$e + @crlf)
next

$_ = close(1)

exit 1



Edited by Jochen (2004-08-26 03:35 PM)
_________________________



Top
#125695 - 2004-08-26 03:11 PM Re: Listing textfile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, I was thinking more like this:
Code:

break on
dim $Out, $FileArray, $file
dim $outArray, $line
$Out = @scriptdir + "\result.txt"
$FileArray = @scriptdir + "\file1.txt",@scriptdir + "\file2.txt",@scriptdir + "\file3.txt"

if open(2,$Out,5) "failed to open output file, quitting" quit endif

for each $file in $FileArray
if open(1,$file) = 0
$line = readline(1)
while @error = 0
if ascan($outArray,$line) < 0
redim preserve $outArray[ubound($outArray)+1]
$outArray[ubound($outArray)] = $line
$ = writeline(2, $line + @crlf)
endif
endif
$line = readline(1)
loop
$ = close(1)
endif
next
$ = close(2)

_________________________
!

download KiXnet

Top
#125696 - 2004-08-26 03:15 PM Re: Listing textfile
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
hmm, i thought when i tried to ascan using a non-array it didn't work, but i guess it does. must have fat fingered something when it i tested it.

Nice code
_________________________
Eric

Top
#125697 - 2004-08-26 03:29 PM Re: Listing textfile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, it shouldn't work
but what it returns then?
I did not really test it.
you may need to add some "and" to that ascan line.
_________________________
!

download KiXnet

Top
#125698 - 2004-08-26 07:52 PM Re: Listing textfile
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Nice game of golf but there are UDFs already that will do all of this.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#125699 - 2004-08-26 08:04 PM Re: Listing textfile
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
like which one?
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.075 seconds in which 0.035 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org