Page 1 of 1 1
Topic Options
#130536 - 2004-12-03 04:15 AM Collate data
Kixfan Offline
Fresh Scripter

Registered: 2004-03-18
Posts: 17
I have a text file named ITEM and contents as follows:

Apple
Pear
Orange
Apple
Orange

Is there a way to collate the number of items, like this:

Apple - 2
Pear - 1
Orange - 2

Thank you.

Top
#130537 - 2004-12-03 04:55 AM Re: Collate data
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Yes, it could be done.

In the UDF library there are many UDFs you could use as inspiration. Several that count, several that remove dups, etc.

Start by using the ReadFile() UDF to read it into an array. Then modify one of the UDFs that remove dups from an array to also pass the number of elements removed.

How hard can it be?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#130538 - 2004-12-03 05:28 AM Re: Collate data
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You can get the results you are looking for by using the HASH UDFs from the library. Simply use ORange as the key and increment the value.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#130539 - 2004-12-03 05:51 PM Re: Collate data
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Code:

$file = "test.txt"

$List = readfile($file)

dim $array[0]

for each $A in $list
for each $b in $list
if $a = $b
$counter = $counter + 1
endif
next
if ascan($array,$a +" - " + $counter) = -1
$array[ubound($array)] = $a +" - " + $counter
redim preserve $array[ubound($array)+1]
endif
$counter = 0
next

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

? join($array,@crlf)


Top
#130540 - 2004-12-03 07:38 PM Re: Collate data
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Very nice solution there Bryce. Recommend to add an If $A to the code to remove blank or invalid entry in the return.

Slightly modified code.

Code:
Break On

Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('WrapAtEOL','On')

Dim $array[0],$List,$File,$A,$B,$Counter
$File = "c:\temp\test.txt"
$List = ReadFile($file)
For Each $A In $list
If $A
For Each $B In $List
If $A = $B
$Counter = $Counter + 1
EndIf
Next
If Ascan($Array,$A + " - " + $Counter) = -1
$Array[UBound($Array)] = $A + " - " + $Counter
ReDim PreServe $Array[UBound($Array)+1]
EndIf
$Counter = 0
EndIf
Next
If UBound($Array)
ReDim PreServe $Array[UBound($Array)-1]
EndIf
? Join($Array,@CRLF)

Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=chr(10)
$f=freefilehandle
$_=open($f,$file)
if @error exit @error endif
do $t=$t+$lf+readline($f) until @error
$_=close($f)
$ReadFile=split(substr($t,2),$lf)
EndFunction



Code:
Apple 

Pear
Orange
Apple
Orange
apple
apple
apple
apple
Orange
Orange
apple


Apple - 2
Pear - 1
Orange - 1
Orange - 3
apple - 5


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
0 registered and 484 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.057 seconds in which 0.022 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