Sign in to follow this  
Followers 0
sharrakor

Simple array error... Anyone know why?

19 posts in this topic

#1 ·  Posted (edited)

I'm writing something simple to run my au3 files from notepad++ when i press a certain key. Here is my code:

opt("wintitlematchmode",2)

hotkeyset("`","runcurrentproj")

while 1
sleep(10)
wend

func runcurrentproj()
local $fulltitle[256];i think this is the largest name possible
$fulltitle=stringsplit(wingettitle("- Notepad++"),"",2)
$count=0
while $fulltitle[$count]<>""
$count+=1
wend
$count-=1

for $i=0 to 11
$fulltitle[$count]=""
$count-=1
next

run($fulltitle)

sleep(500)

endfunc

I get this error when I attempt to run it, however:

---------------------------

AutoIt Error

---------------------------

Line 15 (File "L:\programming\au3executer\executer.au3"):

wend

wend^ ERROR

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

---------------------------

OK

---------------------------

Anyone know how to fix this? I'm having this same problem in another script I'm writing. I've tried not defining the size of $fulltitle as well.

Edited by sharrakor

Share this post


Link to post
Share on other sites



It's breaking here, since you're incrementing $count beyond the boundaries of your array.

$count=0
 while $fulltitle[$count]<>""
 $count+=1
 wend
The first element of an array returned by StringSplit is populated with the index of the last element.

Therefore, it would be better to do a For Loop.

For $count = 1 to $fulltitle[0]
    ...
NextoÝ÷ Øò¢íý­ç^jX§­©À®¶²¶­Á«'ßÛ`zw«j×o&ºw-í«i¢¥j׬¶»-zW¦z{n±å¢éݶ!yÉ"µø«²ÑèÁëÞ®+e¢,)Ê·ö·¬jeÊÚòx-¢·¦¢÷ªºM¢×©iß¾ªê-~º&¶­Ù^!ûajÝý²Ø^q«ë-v+Ø^jºÚÊëæ§vÚò¶¬jëh×6$fulltitle = StringReplace(WinGetTitle(" - Notepad++"), " - Notepad++", "")

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

You are doing quite a few things wrong here.

First, you are assigning an array scope to one of your variables and then redeclaring it again when you perform the Stringsplit. I have no idea what you are really trying to do with this script but try looking at this first of all:

local $fulltitle[256]; You don't need this...

$fulltitle[0] is going to hold the number of strings returned.

There are a few more issues with your script but start with that first.


My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

Opt("WinTitleMatchMpde", 2)

HotKeySet("`", "RunCurrentProj")

While 1
    Sleep(15)
WEnd

Func RunCurrentProj()
    Local $fulltitle ; No need, you can check the array size of the returning array using UBound.
    
    $fulltitle = StringSplit(WinGetTitle("- Notepad++"), "", 2)

    ;For $i = 0 To 11   ; ==> At this line you're making your script much specific than you might want to.
        ;$fulltitle[$count] = ""
        ;$count -= 1
    ;Next
    
    If Not IsArray($fulltitle) Then Return
    For $i = 0 To UBound($fulltitle)-1 ; To process the whole array
        ;Do something here
    Next
    
    Run($fulltitle) ; What is the purpose? to launch a new Notepad++ process?
    Sleep(500)
    

EndFunc   ;==>runcurrentproj

Please answer the commented questions.

Share this post


Link to post
Share on other sites

Opt("WinTitleMatchMpde", 2)

HotKeySet("`", "RunCurrentProj")

While 1
    Sleep(15)
WEnd

Func RunCurrentProj()
    Local $fulltitle ; No need, you can check the array size of the returning array using UBound.
    
    $fulltitle = StringSplit(WinGetTitle("- Notepad++"), "", 2)

    ;For $i = 0 To 11   ; ==> At this line you're making your script much specific than you might want to.
        ;$fulltitle[$count] = ""
        ;$count -= 1
    ;Next
    
    If Not IsArray($fulltitle) Then Return
    For $i = 0 To UBound($fulltitle)-1 ; To process the whole array
        ;Do something here
    Next
    
    Run($fulltitle) ; What is the purpose? to launch a new Notepad++ process?
    Sleep(500)
    

EndFunc   ;==>runcurrentproj

Please answer the commented questions.

Why are you using ubound when StringSplit already returns the number of strings stored in the array in the first element [0].

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

Because I can read and see that he's calling this function with the last parameter set to 2?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Opt("WinTitleMatchMpde", 2)

HotKeySet("`", "RunCurrentProj")

While 1
    Sleep(15)
WEnd

Func RunCurrentProj()
    Local $fulltitle ; No need, you can check the array size of the returning array using UBound.
    
    $fulltitle = StringSplit(WinGetTitle("- Notepad++"), "", 2)

    ;For $i = 0 To 11   ; ==> At this line you're making your script much specific than you might want to.
        ;$fulltitle[$count] = ""
        ;$count -= 1
    ;Next
    
    If Not IsArray($fulltitle) Then Return
    For $i = 0 To UBound($fulltitle)-1 ; To process the whole array
        ;Do something here
    Next
    
    Run($fulltitle) ; What is the purpose? to launch a new Notepad++ process?
    Sleep(500)
    

EndFunc   ;==>runcurrentproj

Please answer the commented questions.

Just a note Authenticity, StringSplit always returns an array so your code is a little faulty. It mighyt be better to check that wingettitle doesn't return 0. Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Thanks for all the replies. I'm trying to fix up my code right now. By the way I see I'm doing a lot of stuff longhand, but thats because I'm used to C which I'm currently learning in school, so these things like Ubound are a luxury (in classes we have to use a loop to calculate that value).

To your question Authenticity:

The purpose is to run something like: "L:\programming\thecurrentfile.au3". Running an au3 complies and runs it on my system.

I'll post what I come up with next.

Share this post


Link to post
Share on other sites

Search the help file for functions that begin with String or _String, you don't need array for this task. Get the whole title, check to see if it's equal to 0 (failure) and use string manipulation functions to get the path or the what you want.

Share this post


Link to post
Share on other sites

Search the help file for functions that begin with String or _String, you don't need array for this task. Get the whole title, check to see if it's equal to 0 (failure) and use string manipulation functions to get the path or the what you want.

This is what I did, and I came up with exactly what Skruge suggested originally. However, I'm having problems with wingettitle's return value. Even though the window is active, and it reads as a string, it says it is equal to 0.

#include <string.au3>
#include <array.au3>
opt("wintitlematchmode",2)
opt("trayicondebug",1)

hotkeyset("`","runcurrent")

while 1
sleep(10)
wend


func runcurrent()
$title=stringreplace(wingettitle(" - Notepad++")," - Notepad++","")
$title=stringreplace($title,"*","")
msgbox(0,"test",$title)

if wingettitle(" - Notepad++")=0 then
msgbox(0,"failure","")
endif

endfunc

What am I doing wrong?

Share this post


Link to post
Share on other sites

You're trying to get the title and replace things before you even confirmed that the returned string is not actually 0.

Example:

Opt('WinTitleMatchMode', 2)
HotKeySet("'", 'Apostrophe')
HotKeySet('{ESC}', '_EXIT')

While 1
    Sleep(15)
WEnd

Func _EXIT()
    Exit
EndFunc

Func Apostrophe()
    Local $sNew, $sTitle = WinGetTitle('Notepad')
    If $sTitle = 0 Then ConsoleWrite('===>  ' & @ScriptLineNumber & @LF)
    
    $sNew = StringRegExpReplace($sTitle, ' - Notepad$', '', 1)
    If $sNew <> $sTitle Then Run('Notepad.exe "' & $sNew & '"')
EndFunc

Share this post


Link to post
Share on other sites

You're trying to get the title and replace things before you even confirmed that the returned string is not actually 0.

Example:

Opt('WinTitleMatchMode', 2)
HotKeySet("'", 'Apostrophe')
HotKeySet('{ESC}', '_EXIT')

While 1
    Sleep(15)
WEnd

Func _EXIT()
    Exit
EndFunc

Func Apostrophe()
    Local $sNew, $sTitle = WinGetTitle('Notepad')
    If $sTitle = 0 Then ConsoleWrite('===>  ' & @ScriptLineNumber & @LF)
    
    $sNew = StringRegExpReplace($sTitle, ' - Notepad$', '', 1)
    If $sNew <> $sTitle Then Run('Notepad.exe "' & $sNew & '"')
EndFunc

I'm well aware the order is wrong, I was just debugging it.

I just don't see why my if statement thought that wingettitle was returning a zero when it wasn't.

Share this post


Link to post
Share on other sites

You need to preform another check because comparing string with decimal value might come up with a true evaluation.

Check if StringLen($Something) <> 0

Share this post


Link to post
Share on other sites

I'm well aware the order is wrong, I was just debugging it.

I just don't see why my if statement thought that wingettitle was returning a zero when it wasn't.

The problem is, WinGetTitle returns either a string like "File name - Notepad++" or it returns 0.

AutoIt will convert the string to a number when doing a numerical comparison.

A string like "123" will be properly converted to the integer 123, but since the window title doesn't represent a number, it will always get converted to zero.

So using such a comparison, it will always equal zero.

What you really want to do is see if the window exists, right?

Try this:

#include <string.au3>
#include <array.au3>
Opt("wintitlematchmode", 2)
Opt("trayicondebug", 1)

HotKeySet("`", "runcurrent")

While 1
    Sleep(10)
WEnd


Func runcurrent()
    If Not WinExists(" - Notepad++") Then
        MsgBox(0, "failure", "")
    Else
        $title = StringReplace(WinGetTitle(" - Notepad++"), " - Notepad++", "")
        $title = StringReplace($title, "*", "")
        MsgBox(0, "test", $title)

    EndIf
EndFunc   ;==>runcurrent

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

The problem is, WinGetTitle returns either a string like "File name - Notepad++" or it returns 0.

AutoIt will convert the string to a number when doing a numerical comparison.

A string like "123" will be properly converted to the integer 123, but since the window title doesn't represent a number, it will always get converted to zero.

So using such a comparison, it will always equal zero.

What you really want to do is see if the window exists, right?

Try this:

#include <string.au3>
#include <array.au3>
Opt("wintitlematchmode", 2)
Opt("trayicondebug", 1)

HotKeySet("`", "runcurrent")

While 1
    Sleep(10)
WEnd


Func runcurrent()
    If Not WinExists(" - Notepad++") Then
        MsgBox(0, "failure", "")
    Else
        $title = StringReplace(WinGetTitle(" - Notepad++"), " - Notepad++", "")
        $title = StringReplace($title, "*", "")
        MsgBox(0, "test", $title)

    EndIf
EndFunc   ;==>runcurrent
Thanks, that makes a lot of sense. I wonder why the developers made a failure equal zero if we can't easily use it, though.

Also, can you point me to a place where I can find some info on If Not? I haven't seen that before, is it basically: "If $test<>5" is the same as "if not $test=5"?

This is my code:

#include <string.au3>
#include <array.au3>
opt("wintitlematchmode",2)
opt("trayicondebug",1)

hotkeyset("`","runcurrent")

while 1
sleep(10)
wend

func runcurrent()
if winactive(" - Notepad++") then
    $title=stringreplace(wingettitle(" - Notepad++")," - Notepad++","")
    $title=stringreplace($title,"*","")
    run($title)
    msgbox(0,"test",$title)
endif
endfunc

However, it doesn't actually run my program like it does if I open the run box and type in "C:\programming\test.au3". Anyone know why, or how I can get around this?

Share this post


Link to post
Share on other sites

First compile this script to see what happens next. One AutoIt script need to be closed for the other to get executed by the wrapper.

Share this post


Link to post
Share on other sites

Thanks, that makes a lot of sense. I wonder why the developers made a failure equal zero if we can't easily use it, though.

Also, can you point me to a place where I can find some info on If Not? I haven't seen that before, is it basically: "If $test<>5" is the same as "if not $test=5"?

I agree about the return values. I would prefer if it returned a blank string "" and set @error rather than return 0.

As for your question about Not and <> , a good place to start is in the help file, under the topic Operators.

Because of operator precedence and how Not works, "if not $test=5" is not evaluated the way you'd expect.

Let's say for the sake of argument that $test is equal to 5...

"if not $test = 5"

"if (not $test) = 5"

"if (not 5) = 5"

"if 0 = 5"

False

However, it doesn't actually run my program like it does if I open the run box and type in "C:\programming\test.au3". Anyone know why, or how I can get around this?

The Run box is closer in functionality to ShellExecute() than to Run().

I prefer not to link my own posts, but we just discussed this here: http://www.autoitscript.com/forum/index.php?showtopic=90146

See my reply for a solution that might work for you, and read herewasplato's reply for an excellent explanation.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

#include <string.au3>
#include <array.au3>
Opt("wintitlematchmode", 2)
Opt("trayicondebug", 1)

HotKeySet("`", "runcurrent")

While 1
    Sleep(10)
WEnd


Func runcurrent()
    If WinExists("[CLASS:SciTEWindow]", "") Then
        $title = WinGetTitle("[CLASS:SciTEWindow]")
        $aTitle = StringSplit($title, ".au3", 1)
        If Not IsArray($aTitle) Then Return
        Run($aTitle[1] & ".au3")
        MsgBox(0, "test", $aTitle[1] & ".au3")
    EndIf
EndFunc   ;==>runcurrent
Basically you are trying to create a program that when you press the ` key it automatically runs whichever open script you are working on in notepad++? If I'm mistaken about this then I apologize. If so, then do it by class and you can use the autoit window finder program that's included with autoit.

This works with the Scite editor as an example:


My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Share this post


Link to post
Share on other sites

First compile this script to see what happens next. One AutoIt script need to be closed for the other to get executed by the wrapper.

Not so :)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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