Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting phone# using loops and arrays 1

Status
Not open for further replies.

tknight

Technical User
Sep 29, 2002
9
US
I'm trying to find the best way to count occurrences of a string, which is telephone number, using two loops and a array. I'm wondering if using the example below can be improved upon. As you can guess, if raycnt is large can take some time. Raycnt is/was incremented before the looping starts earlier in the script following a FGETS.

string ddd1[10000]
for count1 = 0 upto raycnt
look4this = ddd1[count1]
for count2 = 0 upto raycnt
if strcmp look4this ddd1[count2]
index++
;termmsg " %4d %14s`r`n" count2 look4this
ddd1[count2] = ""
endif
endfor
if not nullstr look4this
termmsg "%15s %4d`r`n" look4this index
endif
index = 0
endfor

; Array ddd1[count1] will contain 5k numbers with several
; numbers repeating.
; 6625196074
; 6625196074
; 0115025156840
; 0115025158283
; 2703054201
; 2703054201

Thank you,
tknight
 
If I understand everything correctly, you have one large array that contains phone numbers and are looking for duplicates of those phone numbers elsewhere in the array. Is that correct? If so, one thing that may help is checking that ddd1[count2] is not null before your if strcmp look4this ddd1[count2] command. If this array member is null, then you don't want to run through the array looking for other null members since it's not a valid phone number (and could also give you an incorrect value for index). You could also change this line:

for count2 = 0 upto raycnt

to read:

for count2 = count1 upto raycnt

Since you have already gone through from ddd1[0] to ddd1[count1] in your previous loops, you can skip these members (they will either be unique or null). Actually, you might want to make that read:

for count2 = count1+1 upto raycnt

since I think your script would set a member null if it was compared to itself (i.e. if look4this = ddd1[count1] and if strcmp look4this ddd1[count2] in cases where count1 = count2).


aspect@aspectscripting.com
 
Thank you, using the value of count2+1 in the 2nd loop made a big difference. Thanks again.
 
Made another improvement by moving the "if not nullstr" after the 1st loop. This keeps the 2nd loop from running as often.

for count1 = 0 upto raycnt
look4this = ddd1[count1]
if not nullstr look4this
for count2 = count1+1 upto raycnt
if strcmp look4this ddd1[count2]
index++
;termmsg " %4d`r`n" index
ddd1[count2] = ""
endif
endfor
index++
termmsg "%17s %4d %4d %s`r`n" look4this index trk[count1] ccc[count1]
totalcnt = index+totalcnt
index = 0
endif
endfor
termmsg "total found = %d`r`n" totalcnt
 
If you know the number(s) you're looking for you could use capture and then open and read the capture file using loop counters.

string fname
string FileSpec = "S:\Client Services\XXX\.txt" ;location capture file
string sLine
string Output
string sCapFile
string sPath,sPathway

strfmt sPathway "S:\Client Services\XXXX\SCRIPTS\%02d%02d%d" szMonth szDay szYear
strdelete sPathway 39 2
mkdir sPathway
pause 2

sPath = sPathway

set capture path sPath
set capture file sCapFile
set capture overwrite ON

proc Phone

Integer A = 0
Integer B = 0

set aspect path "S:\Client Services\XXX\.txt" ;assign the aspect path
statmsg "%s" fname

fopen 1 fname read text ;open the file for read
While not feof 1
fgets 01 sLine
If strncmp sLine "(888) 888-XXXX" 15
A++
else
If strncmp sLine "(888) XXX-0986" 15
B++
endif
endif
Endwhile
fclose 1
clear




strfmt Output "(888) 888-XXXX: %d" A
Termwrites Output
Termwrites "`n`r"
Termwrites "`n`r"
strfmt Output "(888) XXX-0986: %d" B

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top