Jump to content

Is this code bad?


notta
 Share

Recommended Posts

Arrays continue to haunt me, but especially dynamic arrays. The code here is basically an array(newarray) filled with comma delimited computer information. 1 computer per element. I then split each element(each computer) into another array and search(6th element) for the location. I increment the count of each building and then write out the entire line to an array of that building. I redim the array for the next element. This looks very klunky to me. It works, but is there a better way?

I just found this quote from Nutster

When you ReDim, the new array is created and then all the old data is copied, element by element, to the corresponding elements of the new array. Because of this, you do not want to be ReDim'ming arrays too often.

My script loops through 1000 elements in the array so their is quite a bit of redimming going on. If that's the case how would you guys do it? No need to supply the code, just the logic. Thanks.

Oh yea the numbers I'm dealing with are about 20 different buildings and about 50-100 computers per building.

Dim $aB1[1]                                                   ;Create the array with 1 element because I don't know the final size at this point.
        Dim $build1count = 0                                          ; building  1 number count

        for $k=1 To $newarray[0]                                ; array with 1000 computers each on one line
    $splitholder = StringSplit($newarray[$k],",")        ; split the current computer info into individual elements 
    
    if $splitholder[6] = "Building 1" Then                   ; Specific element I want to check for
        
        $aB1[0] += 1                                             ; Set the 0th element to 1 ????
        $build1count += 1                                     ; increment the building count to 1 so I write into the 1st element and not the 0th element
        ReDim $aB1[$build1count + 1]                    ; increase the size of the array for the next "building 1" machine
        $aB1[$build1count] = $newarray[$k]      ; copy the current array element into my array for that building
    EndIf
Next
Edited by notta
Link to comment
Share on other sites

It is often faster to work with a string, and then split it into an array in one shot at the end:

Global $aB1[1] 
Global $sB1 = ""
For $k = 1 To $newarray[0]  ; array with 1000 computers each on one line
    $splitholder = StringSplit($newarray[$k], ",") ; split the current computer info into individual elements
    If $splitholder[6] = "Building 1"  Then ; Specific element I want to check for
        $sB1 &= $newarray[$k] & Chr(1) ; copy the current array element into string for that building
    EndIf
Next
$sB1 = StringTrimRight($sB1, 1) ; Trim off training Chr(1)
$aB1 = StringSplit($sB1, Chr(1))

Note the use of Chr(1) as a delimiter (a handy SmOke_N trick). The character 0x01 is SOH, or Start Of Header, and occurs almost never in text streams since the days of the 100ma loop teletype. That makes it unique enough to be a good delimiter.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

$sB1 &= $newarray[$k] & Chr(1); copy the current array element into string for that building
    EndIf
Next
$sB1 = StringTrimRight($sB1, 1); Trim off training Chr(1)
$aB1 = StringSplit($sB1, Chr(1)

Thanks a ton Psalty. Works like a charm. You have no idea how many times this logic is going to be used in the future. Just about everything I work on is in need of dynamic arrays. Thanks again bud.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...