A couple of BinaryHashRec Examples
Keywords:
Example 1:
BoxOpen("Initilizing",0)
infile = "C:\Windows\Desktop\wordlist.txt"
outfile = "C:\Windows\Desktop\newlist.txt"
start=TimeYmdHms()
BoxText("Reading data into buffer")
size=FileSize(infile)
buffer1 = BinaryAlloc(size)
BinaryRead(buffer1, infile)
BoxText("Moving data to variable")
biglist = binarypeekstr(buffer1,0,size)
biglist = strreplace(biglist,@crlf,@tab)
binaryfree(buffer1)
BoxText("Counting items")
count=ItemCount(biglist,@tab)-1
;Assume item is 256 bytes max
recsize=256
keyoffset=0
;allocate an output binrat bug 10x too large
bigsize=count*recsize
bigbb=BinaryAlloc(bigsize)
BinaryEODSet(bigbb,bigsize)
BoxTitle("In for loop")
For x = 1 to count
BoxText("%x% / %count%")
Item=ItemExtract(x,biglist,@tab)
if Item == ""
continue
endif
;compute offset for this item
offset=BinaryHashRec(bigbb,recsize,keyoffset,recsize,Item)
;look in binary buffer to see if anything there
while 1
existingitem=BinaryPeekStr(bigbb,offset,recsize)
if existingitem=="" ; nothing there use it
BinaryPokeStr(bigbb,offset,Item)
break ; next
endif
if existingitem==Item then break ; ignore it this removes the duplicates
;existing but different item exists try again
offset=offset+recsize
endwhile
next
;Drop biglist.
drop(biglist)
;All dem puppies in there now. And Dups have been eliminated.
;Fish data back out
BoxTitle("In sort")
;sort not required, but it makes it easier to fish stuff out
BinarySort(bigbb,recsize,keyoffset,recsize,@descending|@string)
where=0
BoxText("writing file")
handle=FileOpen(outfile,"WRITE")
outcount=0
while 1
newlist=BinaryPeekStr(bigbb,where,recsize)
if newlist=="" then break
FileWrite(handle,newlist)
outcount=outcount+1
where=where+recsize
if where>=bigsize then break
endwhile
FileClose(handle)
finish=TimeYmdHms()
elapsed=TimeDiff(finish,start)
Message("in %count% out %outcount%","Elapsed time = %elapsed%")
Example 2:
infile = "C:\Windows\Desktop\New Folder\wordlist.txt"
outfile = "C:\Windows\Desktop\New Folder\newlist.txt"
start=TimeYmdHms()
BoxText("Reading data into buffer")
size=FileSize(infile)
buffer1 = BinaryAlloc(size)
BinaryRead(buffer1, infile)
BoxText("Counting items")
count=BinaryStrCnt(buffer1,0,size-1,@crlf)
;Assume item is 256 bytes max
recsize=256
keyoffset=0
;allocate an output binrat bug 10x too large
bigsize=count*recsize
bigbb=BinaryAlloc(bigsize)
BinaryEODSet(bigbb,bigsize)
ptr1=0
BoxText("In for loop")
For x = 1 to count
BoxTitle("%x% / %count%")
ptr2=BinaryIndex(buffer1,ptr1,@crlf,@fwdscan)
Item=BinaryPeekStr(buffer1,ptr1,ptr2-ptr1)
ptr1=ptr2+2
if Item == "" then continue
offset=BinaryHashRec(bigbb,recsize,keyoffset,recsize,Item)
while 1
zx=BinaryPeekStr(bigbb,offset,recsize)
if zx==""
BinaryPokeStr(bigbb,offset,Item)
break
endif
if zx==Item then break
offset=offset+recsize
endwhile
next
;Drop biglist.
drop(biglist)
;All dem puppies in there now. And Dups have been eliminated.
;Fish data back out
BoxTitle("In sort")
;sort not required, but it makes it easier to fish stuff out
BinarySort(bigbb,recsize,keyoffset,recsize,@descending|@string)
where=0
BoxText("writing file")
handle=FileOpen(outfile,"WRITE")
outcount=0
while 1
newlist=BinaryPeekStr(bigbb,where,recsize)
if newlist=="" then break
FileWrite(handle,newlist)
outcount=outcount+1
where=where+recsize
if where>=bigsize then break
endwhile
FileClose(handle)
finish=TimeYmdHms()
elapsed=TimeDiff(finish,start)
Message("in %count% out %outcount%","Elapsed time = %elapsed%")
Article ID: W14228
Filename: BinaryHashRec Examples.txt