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!

Lengthy code for somewhat simple process, need tips on shortning

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I have this code which takes all the data from a Submited form (which was a treeview of documents and options), now that I have it doing the basic task of just creating the new URLs with the filters, I need some tips on how to shorten this code. it involves lots of For Loops, as well as numerous arrays.<br><br><FONT FACE=monospace><br>&lt;%@ Language=VBScript %&gt;<br>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;/HEAD&gt;<br>&lt;BODY&gt;<br><br>&lt;%<br>Dim FormStr<br>Dim FrmCol<br>Dim Number<br>Dim TripleAry<br>Dim HeadAry<br>Dim ValueAry<br>Dim AryData<br>Dim URLList<br><br>' FormStr has 1 fields, &quot;Variable=Value&quot; from the Form Posting<br>'&lt;!-- Grab Parameters --&gt;<br>FormStr = Split(Request.Form, &quot;&&quot;)<br>ReDim FrmCol(UBound(FormStr),2)<br><br>'FormCol now has 2 feilds &quot;Variable&quot;, &quot;Value&quot;<br>'&lt;!-- Grab Variable and Value --&gt;<br>for i = LBound(FrmCol) to UBound(FrmCol)<br> TmpAry = Split(FormStr(i),&quot;=&quot;)<br> FrmCol(i,0) = TmpAry(0)<br> FrmCol(i,1) = TmpAry(1)<br>next<br><br>'&lt;!-- Grab number of Root Catagories(Heads) --&gt;<br>Number = 0<br>for j = LBound(FrmCol) to UBound(FrmCol)<br> if IsNumeric(FrmCol(j,0)) then<br> Number = Number + 1<br> end if<br>next<br><br>ReDim HeadAry(Number)<br>Redim ValueAry((UBound(FrmCol)-Number)-1, 3)<br><br>'HeadAry now has a single field, for the HeadIDs selected<br>'&lt;!-- break down into Head-Variable-Value as well as create a Head Table --&gt;<br>Looper = 0<br>Looper2= 0<br>For j = LBound(FrmCol) to UBound(FrmCol)<br> if IsNumeric(FrmCol(j,0)) then<br> HeadAry(Looper) = FrmCol(j,0)<br> Looper = Looper + 1<br> else<br> rtn = Instr(1, FrmCol(j,0), &quot;_&quot;)<br> if rtn &lt;&gt; 0 then<br> ValueAry(Looper2, 0) = Left(FrmCol(j,0), Rtn-1)<br> ValueAry(Looper2, 1) = Mid(FrmCol(j,0), rtn+1)<br> ValueAry(Looper2, 2) = FrmCol(j,1)<br> Looper2 = Looper2 + 1<br> end if<br> end if<br>next<br><br>'ValueAry now has 3 fields populated with all the HeadID, Variable, Value from the posting<br>'&lt;!-- Check the number of variables that actually reside under a selected head --&gt;<br>'&lt;!-- an option may have been chosen, but the root head hasnt been chosen --&gt;<br>Number = 0<br>for j = LBound(ValueAry) to UBound(ValueAry)<br> if checkhead(ValueAry(j,0)) then<br> Number = Number + 1<br> end if<br>next<br><br>Redim TripleAry(Number, 3)<br><br>'TripleAry will contain same format as VauleAry w/o the invalid rows<br>'&lt;!-- Load new array with Variables that match the selected Heads --&gt;<br>Looper = 0<br>For j = LBound(ValueAry) to UBound(ValueAry)<br> if checkhead(ValueAry(j,0)) then<br> TripleAry(Looper,0) = ValueAry(j,0)<br> TripleAry(Looper,1) = ValueAry(j,1)<br> TripleAry(Looper,2) = ValueAry(j,2)<br> Looper = Looper + 1<br> end if<br>next<br><br>'&lt;!-- Load URL Table into an Array --&gt;<br>Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)<br>Set RS = Server.CreateObject(&quot;ADODB.Recordset&quot;)<br>Conn.Open &quot;Tree&quot;, &quot;sa&quot;, &quot;&quot;<br>RS.Open &quot;Select * from HeadURL Order by ID, TOrder&quot;,Conn, 0,1<br>If not RS.EOF then<br> RS.MoveFirst<br> AryData = RS.GetRows<br> RS.Close<br> Set RS = nothing<br>end if<br>Conn.Close<br>Set Conn = nothing<br><br>Number = 0<br><br>'AryData now contains 2 fields (Field#, Row#)<br>'&lt;!-- check to see how many URLs match the Head to get a proper count for new array --&gt;<br>for j = LBound(AryData,2) to UBound(AryData, 2)<br> for k = LBound(HeadAry) to UBound(HeadAry)<br> if HeadAry(k) = Cstr(AryData(0,j)) then<br> Number = Number + 1<br> end if<br> next<br>next<br><br>ReDim URLList(Number-1, 3)<br>Looper = 0<br><br>'&lt;!-- Load Selected URLS into the New Array --&gt;<br>for j = LBound(AryData,2) to UBound(AryData,2)<br> for k = LBound(HeadAry) to UBound(HeadAry)<br> if HeadAry(k) = CStr(AryData(0,j)) then<br> URLList(Looper,0) = AryData(0,j)<br> URLList(Looper,1) = AryData(1,j)<br> URLList(Looper,2) = AryData(2,j)<br> Looper = Looper + 1<br> end if<br> next<br>next<br><br>'URLList now contains the HeadID-URL-Order<br>'&lt;!-- Append the filters onto the selected URLs --&gt;<br>For j = LBound(URLList) to UBound(URLList)<br> URLList(j,1) = URLList(j,1) & ReturnTags(URLList(j,0))<br>next<br><br>'URLList now been modifed with a proper URL<br>'&lt;!-- show final table, wit Head #, URL, Order --&gt;<br>Response.Write &quot;&lt;TABLE Border=1 align=center&gt;&quot; & vbcrlf<br>For j = Lbound(URLList) to UBound(URLList)<br> Response.Write vbtab & &quot;&lt;TR&gt;&quot; & vbcrlf<br> Response.Write vbtab & vbtab & &quot;&lt;TD&gt;&quot; & URLList(j,0) & &quot;&lt;/TD&gt;&quot; & &quot;&lt;TD&gt;&quot; & URLList(j,1) & &quot;&lt;/TD&gt;&quot; & &quot;&lt;TD&gt;&quot; & URLList(j,2) & &quot;&lt;/TD&gt;&quot; & vbcrlf<br> Response.Write vbtab & &quot;&lt;/TR&gt;&quot; & vbcrlf<br>next<br>Response.Write &quot;&lt;/TABLE&gt;&quot; & vbcrlf<br><br>'&lt;!-- Function to check to see if Filter has a chosen Head --&gt;<br>'&lt;!-- this also helps filter out stuff like the SubMit button --&gt;<br>function checkhead(headpos)<br> checkhead = False<br> for i = LBound(HeadAry) to UBound(HeadAry)<br> if HeadAry(i) = headpos then<br> checkhead = True<br> exit for<br> end if<br> next<br>end function<br><br>'&lt;!-- Returns the Filter Strings to be appended --&gt;<br>function ReturnTags(HeadID)<br> ReturnTags = &quot;&quot;<br> for i = LBound(TripleAry) to UBound(TripleAry)<br> if TripleAry(i,0) = Cstr(HeadID) then<br> if ReturnTags &lt;&gt; &quot;&quot; then<br> ReturnTags = ReturnTags & &quot;&&quot; & Server.URLEncode(TripleAry(i,1)) & &quot;=&quot; & Server.URLEncode(TripleAry(i,2))<br> else<br> ReturnTags = ReturnTags & &quot;?&quot; & Server.URLEncode(TripleAry(i,1)) & &quot;=&quot; & Server.URLEncode(TripleAry(i,2))<br> end if<br> end if<br> next<br>end function<br><br>Set FormStr&nbsp;&nbsp;&nbsp;= nothing<br>Set FrmCol&nbsp;&nbsp;&nbsp;&nbsp;= nothing<br>Set Looper2&nbsp;&nbsp;&nbsp;= nothing<br>Set Looper&nbsp;&nbsp;&nbsp;&nbsp;= nothing<br>Set Number&nbsp;&nbsp;&nbsp;&nbsp;= nothing<br>Set TripleAry = nothing<br>Set HeadAry&nbsp;&nbsp;&nbsp;= nothing<br>Set ValueAry&nbsp;&nbsp;= nothing<br>Set AryData&nbsp;&nbsp;&nbsp;= nothing<br>Set URLList&nbsp;&nbsp;&nbsp;= nothing<br>%&gt;<br><br>&lt;/BODY&gt;<br>&lt;/HTML&gt;<br></font> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Karl,<br><br>Since you are a student let me ask you some questions. Is your major in CS? Have you taken anything like 'Programming logic and design'? Have you ever done any pseudo coding?<br><br>If you could describe in english the problem you are solving then display the pseudo code for you current implementation rather than the language specific code then others not familiar with VBScript (that would be me! hehe)might be able to help out.<br><br>-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top