Need help with variable and loop

Author Message
lmktx

  • Total Posts : 4
  • Scores: 0
  • Reward points : 0
  • Joined: 12/2/2011
  • Status: offline
Need help with variable and loop Friday, December 02, 2011 8:54 PM (permalink)
0
I was wondering if VBScript had the capability to create a variable via a loop?

For example:
I have the following code which has to cycle through 4 different filecollections and therefor four different counters (EstaNumber?Counter, BetaNumber?Counter, CosaNumber?Counter, DeltaNumber?Counter). Each one checks for files starting with the letters A - Z, then assigns the values to buttons on the HTML page of the HTA program.

 For Each fileList In filecollection 
 If left(fileList.Name,1) = "a" Then EstaNumberACounter =  EstaNumberACounter + 1 
 If left(fileList.Name,1) = "b" Then EstaNumberBCounter =  EstaNumberBCounter + 1 ... and it continues on down thru "z" Next EstaNumberA.value = "A " & EstaNumberACounter 
 EstaNumberB.value = "B " & EstaNumberBCounter ... and it continues on down thru "z" 


Is there a way to create the variable name via a loop to minimize the amount of lines I'm having to code and to make it less complicated when new filecollections are added?  Something like this... (which obviously doesn't work *lol* or I wouldn't be on here asking darnit!)
 For Each fileList In filecollection 
 If left(fileList.Name,1) = "a" Then ACounter =  ACounter + 1 
 If left(fileList.Name,1) = "b" Then BCounter =  BCounter + 1 ... and it continues on down thru "z" Next LetterArray = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z") 
 For i = 0 to 1 
 ThisVar = "EstaNumber" & LetterArray(i) 
 ThisVar.value = LetterArray(i) & " " & LetterArray(i)&"Counter" Next 
 


This is a long shot... but hopefully someone has an idea of how to do this so I don't have several hundred lines of code for a simple directory count, and so I won't have to add 60 odd lines of code each time a new file collection is added.
<message edited by lmktx on Friday, December 02, 2011 8:55 PM>
 
#1
    59cobalt

    • Total Posts : 977
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Need help with variable and loop Saturday, December 03, 2011 12:39 PM (permalink)
    0
    Normally you'd use a dictionary if you need to generate variables on the fly.
    Set var = CreateObject("Scripting.Dictionary")
    var.Add "A", "foo"
    var.Add "B", 23
    '...
    var("B") = 42

    In your particular case you'd probably use it like this:
    Set counter = CreateObject("Scripting.Dictionary")
    
    For Each fileList In filecollection
     key = LCase(Left(fileList.Name, 1))
     If counter.Exists(key) Then
     counter(key) = counter(key) + 1
     Else
     counter.Add key, 1
     End If
    Next

    <message edited by 59cobalt on Saturday, December 03, 2011 12:45 PM>
     
    #2
      lmktx

      • Total Posts : 4
      • Scores: 0
      • Reward points : 0
      • Joined: 12/2/2011
      • Status: offline
      Re:Need help with variable and loop Monday, December 05, 2011 5:48 PM (permalink)
      0
      A scripting dictionary hmmm?
      Okay so I've played with this just a tish... and I can see where this would be useful.  A couple of questions come to mind as I cannot seem to find the answers elsewhere.  I understand the loop you made and how the counter works... but I'm not sure if I understand the all of the functionality so bear with me for a second.

      1) Counter is the variable?
      2) The "KEY", in my case, would be one of the 26 letters of the alphabet and would be assigned an incremental value each time a file was found with the initial letter matching the key?
      3) If I wanted to assign one of the values it to a HTML form field how would I go about doing this?

      For example... I have a static HTA form (abbreviated below just for space issues) that looks like this:
      <input id=alphaA value=><input id=alphaB value=><input id=alphaC value=>...<input id=alphaZ value=>
      <input id=betaA value=><input id=betaB value=><input id=betaC value=>...<input id=betaZ value=>
      <input id=gammaA value=><input id=gammaB value=><input id=gammaC value=>...<input id=gammaZ value=>
      <input id=deltaA value=><input id=deltaB value=><input id=deltaC value=>...<input id=deltaZ value=>

      How do assign the value of Counter(A) to HTML form field ID "alphaA":
      By saying:  alphaA.value = Counter(a).

      Which begs the next question:
      Is there a way thought to set the have the form field ID dynamic somehow?  Right now there are
      For instance by setting the Counter with two variables?
      IE: saying:  Counter(a,a).value = Counter(a,b).

      <message edited by lmktx on Monday, December 05, 2011 5:50 PM>
       
      #3
        59cobalt

        • Total Posts : 977
        • Scores: 91
        • Reward points : 0
        • Joined: 7/17/2011
        • Status: offline
        Re:Need help with variable and loop Tuesday, December 06, 2011 1:32 AM (permalink)
        0
        lmktx
        1) Counter is the variable?
        The variable that holds the dictionary, yes.
        lmktx
        2) The "KEY", in my case, would be one of the 26 letters of the alphabet and would be assigned an incremental value each time a file was found with the initial letter matching the key?
        Correct.
        lmktx
        3) If I wanted to assign one of the values it to a HTML form field how would I go about doing this?
        [...]
        How do assign the value of Counter(A) to HTML form field ID "alphaA":
        By saying:  alphaA.value = Counter(a).
        The key is a literal string "a", not a variable a, so you need double quotes around the character:
        alphaA.value = Counter("a")
        Other than that it looks okay to me. I'm not too familiar with HTA, though, so you may want to get a second opinion on that one.
        lmktx
        Which begs the next question:
        Is there a way thought to set the have the form field ID dynamic somehow?  Right now there are
        For instance by setting the Counter with two variables?
        IE: saying:  Counter(a,a).value = Counter(a,b).
        Dictionaries do not support multiple dimensions. To achieve something like this, you must build a dictionary of dictionaries. Accessing the values would go like this:
        alphaAB.Value = Counter("a")("b")

         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9