Jump to content

Having trouble with "non-Array variables"


Recommended Posts

I created a simple loop to store values in an array. The filename would be stored into the first dimension and the filesize (per se) into the second dimension. However, it's not that simple (at lest for me): the loop is allowed to be run again and add new files to the array therefore making the array's size unknowingly variable.

I thought I set up the variables that would allow me to resize the array with each input correctly... but somethings not clicking right.

Dim $pngcount = 0
Dim $pngcounttotal = $pngcount + 1
Dim $pnglist[$pngcounttotal][3]

;some script skipped

$pngssplit = StringSplit($pngs, "|")
For $count = 2 To $pngssplit[0]
    $pnglist[$pngcount][0] = $pngssplit[1] & "\" & $pngssplit[$count]
    $pnglist[$pngcount][1] = FileGetSize($pngssplit[1] & "\" & $pngssplit[$count])
    $pngcount += 1
    ReDim $pnglist[$pngcounttotal][3]
Next
Link to comment
Share on other sites

I created a simple loop to store values in an array. The filename would be stored into the first dimension and the filesize (per se) into the second dimension. However, it's not that simple (at lest for me): the loop is allowed to be run again and add new files to the array therefore making the array's size unknowingly variable.

I thought I set up the variables that would allow me to resize the array with each input correctly... but somethings not clicking right.

Dim $pngcount = 0
Dim $pngcounttotal = $pngcount + 1
Dim $pnglist[$pngcounttotal][3]

;some script skipped

$pngssplit = StringSplit($pngs, "|")
For $count = 2 To $pngssplit[0]
    $pnglist[$pngcount][0] = $pngssplit[1] & "\" & $pngssplit[$count]
    $pnglist[$pngcount][1] = FileGetSize($pngssplit[1] & "\" & $pngssplit[$count])
    $pngcount += 1
    ReDim $pnglist[$pngcounttotal][3]
NextoÝ÷ Ûú®¢×µ&ë-ºÇ­çâ®Ë^éÝ7êà+-ÓKh究¢éíØ:-ä^)+{*.i×mçZµ«hëmêâZ®¶²²,ÞÜ
i+azf­Ú zÛayø¥zv¦yúèMú¦x,²bµ©Ý¸­z+m£Mú¦x%ËZ¶Ø^®(!¶Êh¶Æ®¶­sdvÆö&Âb33c·ævÆ7E³Õ³5ÒÒ³ÂgV÷C²gV÷C²ÂgV÷C²gV÷CµÐ £·6öÖR67&B6¶V@ ¢b33c·æw77ÆBÒ7G&æu7ÆBb33c·æw2ÂgV÷C·ÂgV÷C²¤bW'&÷"FVà ×6t&÷bÂgV÷C´W'&÷"gV÷C²ÂgV÷C´W'&÷"7ÆGFærb33c·æw2gV÷C²¤VÇ6P ²6Æ7VÆFRæWr6¦Rf÷"'& b33c¶æWt6÷VçBÒV&÷VæBb33c·ævÆ7B²V&÷VæBb33c·æw77ÆBÒ  &TFÒb33c·ævÆ7E²b33c¶æWt6÷VçEÕ³5Ð  ²FBFFFò'& f÷"b33c¶âÒ"Fòb33c·æw77ÆE³Ð b33c·ævÆ7E²b33c·ævÆ7E³Ò²b33c¶âÒÕ³ÒÒb33c·æw77ÆE³ÒfײgV÷C²b3#²gV÷C²fײb33c·æw77ÆE²b33c¶6÷VçEÐ b33c·ævÆ7E²b33c·ævÆ7E³Ò²b33c¶âÒÕ³ÒÒfÆTvWE6¦Rb33c·æw77ÆE³ÒfײgV÷C²b3#²gV÷C²fײb33c·æw77ÆE²b33c¶6÷VçEÒ æW@  ²6fRæWr'&6¦RFòf'7BVÆVÖVç@ b33c·ævÆ7E³Õ³ÒÒb33c¶æWt6÷VçBÒ¤VæD

:D

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

Thanks for the help! ...and sorry for the late reply. I tried implementing your changes to my script but when I tried it I kept getting "ReDim" used without an array variable. Could anybody explain to me why this is so?

Here's my updated script:

Func GetPNGsfiles()
    Local $pngs
    Local $pnglist
    Local $pngssplit
    
    $pngs = FileOpenDialog("PNGs to be crushed", "", "PNG image (*.png)", 1 + 2 + 4)
    
    ;by this time, there could already be many images stored in $pnglist... ie $pnglist[0][0] could be 18 at the moment
    If @error <> 1 Then
        If StringInStr($pngs, "|") = 0 Then
            
            ;this course would be taken should only one image be selected
            
            ReDim $pnglist[$pnglist[0] + 1][3];enlarging the array
            sleep(1000)
            
            $pnglist[$pnglist[0]][0] = $pngs
            $pnglist[$pnglist[0]][1] = FileGetSize($pngs)
            
            $pnglist[0][0] += 1;updating the total amount of images
            
        Else
            
            ;course of action for multiple images
            
            $pngssplit = StringSplit($pngs, "|")
            
            $nNewCount = UBound($pnglist) + UBound($pngssplit) - 2
            ReDim $pnglist[$nNewCount][3];enlarging the array
            
            For $n = 2 To $pngssplit[0]
                
                $pnglist[$nNewCount - UBound($pngssplit) + $n - 1][0] = $pngssplit[1] & "\" & $pngssplit[$n];array to store image path and filename
                $pnglist[$nNewCount - UBound($pngssplit) + $n - 1][1] = FileGetSize($pngssplit[1] & "\" & $pngssplit[$n]);array to store filesize
                
            Next
            $pnglist[0][0] = $nNewCount;updating the total amount of images
            
        EndIf
    EndIf
EndFunc   ;==>GetPNGsfiles
Edited by Zhelkus
Link to comment
Share on other sites

Thanks for the help! ...and sorry for the late reply. I tried implementing your changes to my script but when I tried it I kept getting "ReDim" used without an array variable. Could anybody explain to me why this is so?

Each time you run the function, you are re-declaring $pnglist as a non-array variable with "Local $pnglist". If you want the values in that array to persist between calls to the function, declare it outside the function with "Global" instead.

:D

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

Quite an ugly function my friend, despair not.

#include <array.au3>

$result = GetPNGsfiles()
_ArrayDisplay($result)

Func GetPNGsfiles()
    $pngs = FileOpenDialog("PNGs to be crushed", "", "PNG image (*.jpg)", 1 + 2 + 4)
    If Not @ERROR Then
        ;Split file list on pipe char
        $pngssplit = StringSplit($pngs, "|")

        ;If more than one image is found
        If $pngssplit[0] > 1 Then
            ;Create new array with one less element than previous
            Dim $pnglist[$pngssplit[0]][2]
            
            ;Number of elements is previous minus 1 since we stripped out the folder
            $pnglist[0][0] = $pngssplit[0]-1
            
            ;Skip folder name (Who wrote FileOpenDialog anyways?)
            For $X = 2 to $pngssplit[0]
                ;Append full path
                $pnglist[$X-1][0] = $pngssplit[1] & "\" & $pngssplit[$X]
            Next
            
        Else
            ;Create new array with same number of elements as previous
            Dim $pnglist[$pngssplit[0]+1][2]
            ;Only 1 element
            $pnglist[0][0] = 1
            ;Full path already included
            $pnglist[1][0] = $pngssplit[1]
        EndIf
    
        ;Loop through final array and add file sizes
        For $X = 1 to $pnglist[0][0]
            $pnglist[$X][1] = FileGetSize($pnglist[$X][0])
        Next
        
        Return $pnglist
    EndIf    
EndFunc   ;==>GetPNGsfiles
Link to comment
Share on other sites

Quite an ugly function my friend, despair not.

I got the impression he wanted to be able to call it more than once and add to the list, so I was trying to nudge him towards this version:

#include <array.au3>

Global $pnglist[1][3] = [[0, "", ""]]

GetPNGsfiles()
_ArrayDisplay($pnglist, "First Pass")
GetPNGsfiles()
_ArrayDisplay($pnglist, "Second Pass")

Func GetPNGsfiles()
    $pngs = FileOpenDialog("PNGs to be crushed", "", "PNG image (*.jpg)", 1 + 2 + 4)
    If Not @error Then
        ;Split file list on pipe char
        $pngssplit = StringSplit($pngs, "|")

        ;If more than one image is found
        If $pngssplit[0] > 1 Then
            ;Create new array with one less element than previous
            ReDim $pnglist[$pnglist[0][0] + $pngssplit[0]][3]

            ;Skip folder name (Who wrote FileOpenDialog anyways?)
            For $X = 2 To $pngssplit[0]
                ;Append full path
                $pnglist[$pnglist[0][0] + $X - 1][0] = $pngssplit[1] & "\" & $pngssplit[$X]
            Next
        ElseIf $pngssplit[0] = 1 Then
            ;Create new array with same number of elements as previous
            ReDim $pnglist[$pnglist[0][0] + 2][3]
            ;Full path already included
            $pnglist[$pnglist[0][0] + 1][0] = $pngssplit[1]
        EndIf

        ;Loop through final array and add file sizes
        For $X = $pnglist[0][0] + 1 To UBound($pnglist) - 1
            $pnglist[$X][1] = FileGetSize($pnglist[$X][0])
        Next

        ;Number of elements is previous minus 1 since we stripped out the folder
        $pnglist[0][0] += $pngssplit[0] - 1
    EndIf
EndFunc   ;==>GetPNGsfiles

Also, there were originally 3 columns in the second dimension, so I preserved that, even though the last one is not used here (maybe he had a future use planned).

:D

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

Oh my god! Thank you guys so much! I didn't realize I was making such a clumsy mistake :D The script looks a LOT nicer and much more tidier than mine. All these hours of tinkering with my computer and creating random purpose scripts really paid out cuz I understood what you wrote in a breeze. Thanks a lot, you two.

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...