Sign in to follow this  
Followers 0
Shyke

Array Pain!

4 posts in this topic

$a = StringSplit($input, "")
For $i = 1 to $a[0]
    If $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "/" And $a[$i + 3] = "|" Then
        $output = $output & "m"
        $i = $i + 4
    EndIf
Next

That's a clippet of my code. It runs into errors when it tries to test $a[$i + 1] if it is already at the end of $a[0] as expected.

How do I work around this so I don't hit this terminal error?

Share this post


Link to post
Share on other sites



$a = StringSplit($input, "")
For $i = 1 to $a[0]
    If $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "/" And $a[$i + 3] = "|" Then
        $output = $output & "m"
        $i = $i + 4
    EndIf
Next

That's a clippet of my code. It runs into errors when it tries to test $a[$i + 1] if it is already at the end of $a[0] as expected.

How do I work around this so I don't hit this terminal error?

Don't ever modify a loop variable inside the loop itself. It will bring you really bad karma. How about:

$a = StringSplit($input, "")
$i=1
while $i <= $a[0] - 3
  If $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "/" And $a[$i + 3] = "|" Then
    $output = $output & "m"
  EndIf
  $i = $i + 4
wend

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Err...

Here's a bigger piece of my code:

$a = StringSplit($input, "")
For $i = 1 To $a[0]
    If $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "/" And $a[$i + 3] = "|" Then
        $output = $output & "m"
        $i = $i + 4
    ElseIf $a[$i] = "|" And $a[$i + 1] = "-" And $a[$i + 2] = "|" Then
        $output = $output & "h"
        $i = $i + 3
    ElseIf $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "|" Then
        $output = $output & "n"
        $i = $i + 3
    EndIf
Next
Edited by Shyke

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$a = StringSplit($input, "")
$i = 1
while $i < $a[0]
  if $i < $a[0] - 4 then
    If $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "/" And $a[$i + 3] = "|" Then
        $output = $output & "m"
        $i = $i + 4
    endif
  endif
  if $i < $a[0] - 3 then
    If $a[$i] = "|" And $a[$i + 1] = "-" And $a[$i + 2] = "|" Then
        $output = $output & "h"
        $i = $i + 3
    ElseIf $a[$i] = "|" And $a[$i + 1] = "\" And $a[$i + 2] = "|" Then
        $output = $output & "n"
        $i = $i + 3
    EndIf
  endif
Next

Same principal, just different checks.

Edit: Misplaced an endif

Edited by PaulIA

Auto3Lib: A library of over 1200 functions for AutoIt

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