Sign in to follow this  
Followers 0
Baku

How to do a loop like in php with foreach?

13 posts in this topic

Hi guys,

i just started doin a little tool, learning by doing ;D But now i just got a BIIIIIG problem.

Listen pls..

I split a string to an array with Stringplit. I splitted files who were chosen by FileOpenDialog.

--> I've got $output[0] with the number of the created arrays.

--> $output[1] is the path, $output[2] to $output[X] are the chosen files.

Now I have to do two things with the arrays. Firstly, i have to add $output[1] to each further array to get the complete folder. Aaaand i have to shortern their paths with "FileGetShortName", because i have to write down the shortern parts in a file.

I think there is a way with a loop to do this. If not, i would have to do it like that:

If $output[0] = 1 
 $output[2] = "BLABLA" & "BLUBB"
 $doneop[2] = FileGetShortName($output[2])
 EndIf
 
If $output[0] = 2
 $output[2] = "BLABLA" & "BLUBB"
 $doneop[2] = FileGetShortName($output[2])
 $output[3] = "BLABLA" & "BLUBB"
 $doneop[3] = FileGetShortName($output[3])
 EndIf

If $output[0] = 3
 $output[2] = "BLABLA" & "BLUBB"
 $doneop[2] = FileGetShortName($output[2])
 $output[3] = "BLABLA" & "BLUBB"
 $doneop[3] = FileGetShortName($output[3])
EndIf

If $output[0] = 4
 $output[2] = "BLABLA" & "BLUBB"
 $doneop[2] = FileGetShortName($output[2])
 $output[3] = "BLABLA" & "BLUBB"
 $doneop[3] = FileGetShortName($output[3])
 $output[4] = "BLABLA" & "BLUBB"
 $doneop[4] = FileGetShortName($output[4])
EndIf

Thats the wrong way i guess... So pleaaaaasee guys, pleaaaaaaaaaasssee, tell me how to do that :evil:

Thx in advanced :)

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

If I've understood correctly, try this.

for $Ctr = 2 to $output[0] -1
  $ShortenedFileName = FileGetShortName($Output[1] & $Output[$Ctr])
next
Edited by sandyd

----[ SandyD ]---

Share this post


Link to post
Share on other sites

thats what i searched for guys, thank you very much :)

Share this post


Link to post
Share on other sites

i've got another problem =(

For $i = 1 To $output[0]
        $output[$i] = $output[1] & $output[$i]
        $doneop[$i] = FileGetShortName($output[$i])
    Next

The script starts, if i choose a file with FileOpenDialog i get that error

==> You must DIM an array before you can assign to it.:

$doneop[$i]= FileGetShortName($output[$i])

^ ERROR

Is Dim $doneop[$i] right? or what shall i set? If i put that before the loop, i get an error like

WARNING: $i: possibly used before declaration.

When i Dim it after the loop i get an error too

You must DIM an array before you can assign to it.:

$doneop[$i] = FileGetShortName($output[$i])

If i Dim it after the Loopstart i get

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

I dont get it :)

Share this post


Link to post
Share on other sites

You've used PHP?

Okay, when you want to make an array in PHP, how do you do it? First you specify that you want the variable to be an array right?

$myArray = array()

Well in AutoIt, you have to do the same thing, except that you also have to specify the SIZE of the array.

Dim $myArray[10]

That would create an array with 10 items (0 to 9). Your best bet in this case, is to do something like this.

Dim $doneop[$output[0] + 1]
For $i = 1 To $output[0]
    $output[$i] = $output[1] & $output[$i]
    $doneop[$i] = FileGetShortName($output[$i])
Next

I added 1 to the Dim because if you don't you'll get an error that the array 'subscript dimension range has been exceeded' ie: you went too high.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If you do -1 on the loop, how are you going to get the last value in $output?

$strArray = stringsplit('this is a string', ' ')
Dim $strLens[$strArray[0] + 1]
for $i = 1 to $strArray[0] - 1
    $strLens[$i] = stringlen($strArray[$i])
    msgbox(0, $strLens[$i], $strArray[$i])
next

Returns: "this", "is", "a"

Unless you mean like this:

$strArray = stringsplit('this is a string', ' ')
Dim $strLens[$strArray[0]]
for $i = 1 to $strArray[0]
    $strLens[$i - 1] = stringlen($strArray[$i])
    msgbox(0, $strLens[$i - 1], $strArray[$i])
next

But I figured for someone confused about arrays, having the second array being the same size as the first would be the simplest solution.

Edited by Saunders

Share this post


Link to post
Share on other sites

$strArray = stringsplit('this is a string', ' ')

Dim $strLens[UBound($strArray)]

for $i = 1 to UBound($strArray) - 1
    $strLens[$i] = stringlen($strArray[$i])
    msgbox(0, $strLens[$i], $strArray[$i])
next


My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites

$strArray = stringsplit('this is a string', ' ')

Dim $strLens[UBound($strArray)]

for $i = 1 to UBound($strArray) - 1
    $strLens[$i] = stringlen($strArray[$i])
    msgbox(0, $strLens[$i], $strArray[$i])
next

<{POST_SNAPBACK}>

Okay sure, but really, as far as results, how is that any different from what I suggested?

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

The order???

;yours
$strArray = stringsplit('this is a string', ' ')
Dim $strLens[UBound($strArray)]
for $i = 1 to UBound($strArray) - 1
    $strLens[$i] = stringlen($strArray[$i])
    msgbox(0, $strLens[$i], $strArray[$i])
next


;mine
$strArray = stringsplit('this is a string', ' ')
Dim $strLens[$strArray[0] + 1]
for $i = 1 to $strArray[0]
    $strLens[$i] = stringlen($strArray[$i])
    msgbox(0, $strLens[$i], $strArray[$i])
next

Looks exactly the same to me.

Only difference I can see is that mine is slightly faster because yours has to call UBound twice (although it could easily be dropped to one call with an extra variable, but even then it's ever so slightly slower than the non-ubound calling one).

Edited by Saunders

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

u guys are confusing me =( but well, this isnt working rightly...

$selection = FileOpenDialog("Choose your music..", @DesktopCommonDir, "Frauenhofer MPEG-1 Audio Layer 3 (*.mp3)", 1 + 2 + 4)
    $output = StringSplit($selection, "|")
    
Dim $doneop[$output[0] + 1]

For $i = 1 To $output[0]
    $output[$i] = $output[1] & $output[$i]
    $doneop[$i] = FileGetShortName($output[$i]) 
    MsgBox(64, "lol", $doneop[$i])

If i put this into that loop...

MsgBox(64, "test", $doneop[$i])

... I get the path of my file twice, and not in the shorter form :evil: I also tried that UBoundthingy to cound the arrayinfos, i always get that overranged array text... Also, if i put that MsgBox outside the Loop it appears...

The problem now is: How do i get that $doneop[$i] out of the loop working, and how to fix that thing that it not drops the path twice without the short filepathname....

thanks :)

Edited by Baku

Share this post


Link to post
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
Sign in to follow this  
Followers 0