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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have this code which doesn't seen to work

Status
Not open for further replies.

taval

Programmer
Jul 19, 2000
192
GB
I have written a piece of asp code that should put unique file extensions into an array. So for example if a db has the files:

workdocument.txt
myspread.xsl
game.exe
adobe.exe

then the following piece of code should only put .txt, .xsl, .exe into the array. In otherwords if there are 10 exe files, it will only put one instance of that extension into the array once.

my code looks like this:
-----------------------------------------------

rsFiles.MoveFirst

While not rsFiles.EOF

file = rsFiles("FileName")
rtn = InStr(1,file, ".")
if rtn > 0 then
extension = Right(file, (Len(file)-rtn+1))
end if

for l=0 to 49
if extArray(arrayinc) = extension then
exit for
else
extArray(arrayinc) = extension
Response.write extArray(arrayinc) & &quot;<br>&quot;
arrayinc= arrayinc+1
exit for
end if
next


rsFiles.MoveNext

wend

----------------------------------------------------

the problem is this code puts in all the extensions (even duplicates extensions )into the array. It looks as though it should work but I can't figure out whats wrong, grateful for any help. Thanks.
 
tval,

I don't use ASP but -

you need to just set a flag when/if you find a match in the loop of i = 0 to 49. If the flag is NOT set on exiting the loop, then the Extension has not been added, so add it then.

The way your code is set up, you will add the extension unless it matches EVERY existing element of the array.

 
I've tried adding a flag as you said but it still does not work , the flag is never raised but when I raise it on purpose by changing the code it works.

Heres my updated code, I think the probelm is with the comparing of the strings:



While not rsFiles.EOF

file = rsFiles(&quot;FileName&quot;)
rtn = InStr(1,file, &quot;.&quot;)
if rtn > 0 then
extension = Right(file, (Len(file)-rtn+1))
end if

for l=0 to 49
if extArray(arrayinc) = extension then
flag = true
else
end if
next

if flag = true then
extArray(arrayinc) = extension
Response.write extArray(arrayinc) & &quot;<br>&quot;
arrayinc= arrayinc+1
flag=false
else
end if



rsFiles.MoveNext
wend

Can anybody help me out? , Thanks.

 
tval,

Where is 'arrayinc being set? In the loop for i = 0 to 49, arrayinc is not set, so you are comparing extArray(SAMEVALUE) to extension 50 times. Try using I in place of arrayinc?
 
sorry about that my code looks like this:

rsFiles.MoveFirst

While not rsFiles.EOF

file = rsFiles(&quot;FileName&quot;)
rtn = InStr(1,file, &quot;.&quot;)
if rtn > 0 then
extension = Right(file, (Len(file)-rtn+1))
end if

for l=0 to 49
if extArray(l) = extension then
else
flag = true
end if
next

if flag = true then
extArray(arrayinc) = extension
Response.write extArray(arrayinc) & &quot;<br>&quot;
arrayinc= arrayinc+1
flag= false
else
end if



rsFiles.MoveNext
wend

But I still get the same problem, all the extensions are shown put into the array.
 
tval,

for l=0 to 49[tab]' is this I (letter) or 1 (number)?
[tab][tab]if extArray(l) = extension then
[tab]else[tab][tab]' What is this for? It APPEARS TO BE A PROBLEM
[tab][tab]flag = true
[tab][tab]end if
next

I think you are setting the flag improperly.

Try putting some breakpoints in the code. See what the strings look like at various points. Check sosme case when the strings are equal and step (trace) through the loop setting the flag to see which branch it takes.

Why is the ELSE in the loop?


 
let me try and explain this piece of code.

for l=0 to 49 ' is the letter l
if extArray(l) = extension then
else '
flag = true
end if
next

the loops goes through the whole array, if it founds an &quot;extension&quot; that is in the array, then it doesn't do anything. However if it finds an extension that is in the array then it sets the flag to true. if the flag is true then the extension is added.
 
I've got it to work , the problem was that I had to trim both the strings first, it was hard to find and I was just about to give up, anyway now it does work. Thank you for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top