Sign in to follow this  
Followers 0
PDowning

More array looping

18 posts in this topic

#1 ·  Posted (edited)

You may recognize this from the "looping thru an array" thread from yesterday. Thanks to those that provided a working result. I didn't want to unmark the thread as answered because it technically was answered. Now I am improving the script. 

Now it will loop thru the array, compare the computer name to the array entries and if they MATCH, then strip the leading character, and run an msi with a new value tacked on the end for a variable.

Will this do what I am expecting? Thanks to Firefox and mikell for the code snippets.

For $i = 0 to UBound($RoomArray)-1
  If $RoomArray[$i] = $RoomNumber Then
        $sRoomNumb = StringTrimLeft($RoomArray[$i], 1)
        $RunMSI = "i:\Utilities\Lanschool\Student.msi"
        ShellExecuteWait($RunMSI, "/passive Channel=",$sRoomNumb)
   ExitLoop
Next
Edited by PDowning

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You understood the main thing, but there is some mistakes.

 

$RunMSI = "i:\Utilities\Lanschool\Student.msi"

For $i = 0 to UBound($RoomArray) -1
    If $RoomArray[$i] = $RoomNumber Then
        $sRoomNumb = StringTrimLeft($RoomArray[$i], 1)

        ShellExecuteWait($RunMSI, "/passive Channel=" & $sRoomNumb)

        ExitLoop ;comment this if you want to run all matching MSIs
    EndIf
Next
Br, FireFox. Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

You understood the main thing, but there is some mistakes.

 

$RunMSI = "i:\Utilities\Lanschool\Student.msi"

For $i = 0 to UBound($RoomArray) -1
    If $RoomArray[$i] = $RoomNumber Then
        $sRoomNumb = StringTrimLeft($RoomArray[$i], 1)

        ShellExecuteWait($RunMSI, "/passive Channel=" & $sRoomNumb)

        ExitLoop ;comment this if you want to run all matching MSIs
    EndIf
Next
Br, FireFox.

 

The & ... always forget that little piece of knowledge.  Thanks.   And why did you pull the msi declaration out of the loop? Just for readability or efficiency?

Paul

Share this post


Link to post
Share on other sites

Efficiency, no need to assign the same thing in your variable.

Furthermore, it would be worse if you would declare it in a loop, don't forget to declare all your variables (it's a good coding practice especially to AutoIt).

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here I am again with more of this array looping and whatnot.  This time I think I have it straight but wanted you wonder-coders to hit me with a hammer if it's not.

#include <Array.au3>

Local $RoomNumber = StringLeft(@Computername, 3)
Local $TeacherString = StringInStr(@ComputerName, "tw")
Global $RoomArray[4] = ["E16", "E14", "E25", "W55"]

Local $Index = _ArraySearch($RoomArray, $RoomNumber) ;searches the $sToFind in $aRoomArray
$RunStudentMSI = "i:\Utilities\Lanschool\Student.msi"
$RunTeacherMSI = "i:\Utilities\Lanschool\teacher.msi"

If FileExists("C:\Program Files\LanSchool\student.exe") Then  ;if file exists, exit script;program already installed
    Exit
Else
    If $Index = -1 Then                             ;if not equal to array entries, install Lanschool-Student on Channel 1
        ShellExecuteWait($RunStudentMSI, "/passive Channel=1")
    EndIf
EndIf

For $i = 0 to UBound($RoomArray)-1                      ;Loop thru the roomarray and compare to computername
   If $RoomArray[$i] = $RoomNumber Then                 ;if it matches, strip the 1st char, run the msi with the room number appended to
        $sRoomNumb = StringTrimLeft($RoomArray[$i], 1)  ;the channel option
        ShellExecuteWait($RunStudentMSI, "/passive Channel=" & $sRoomNumb)
        ExitLoop
    EndIf
Next

If FileExists("C:\Program Files\Lanschool\teacher.exe") Then                ;if file exists, exit script, program already installed
    Exit
Else
    If $TeacherString =  -1 Then                                            ;if not equal, then run the teacher install with the room number appended
        ShellExecuteWait($RunTeacherMSI, "/passive channel=" $sRoomNumb)
    EndIf
EndIf

It's the last section that I need affirmation on.  I want to take the computername, compare and see if the string "TW" is in it, if so, run that msi with the current roomnumber from the array.

Edit: I don't mind posting my whole code, nothing secret about it, and no clause in my employment that says anything I come up with is the employers property :-)

Edited by PDowning

Share this post


Link to post
Share on other sites

That's almost it. Read carrefuly the helpfile concerning the StringInStr function.

Failure:    Returns 0 if substring not found.
So your wrong line becomes :

If $TeacherString = 0 Then
Br, FireFox.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

That's almost it. Read carrefuly the helpfile concerning the StringInStr function.

Failure:    Returns 0 if substring not found.
So your wrong line becomes :

If $TeacherString = 0 Then
Br, FireFox.

 

 

So by reading the help, then "The count parameter must be longer than the substring being searched for." I need:  stringinstr(@computername, 4, 2). but there lies a problem. the Computename is going to be i.e.  e16-tw-<serial>.   the substring being searched for is "tw", so my count has to be 3?

Edit:  or  Stringinstr(@computername, "tw", 4, 2, 0, 1) ?

Edited by PDowning

Share this post


Link to post
Share on other sites

Nope. Read carefully the helpfile. The 3rd parameter is for the case sense, and the maximum value is two.

I'm almost sure you don't need the last two parameters for your script.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Oops, got the numbers in the wrong order ...

Stringinstr(@computername, "tw", 0, 1, 4, 2) - corrected string??

Share this post


Link to post
Share on other sites

Again your code is incorrect, the last parameter is not the length of your substring (which has no utility here) but the position in the string where the functions stops.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Then I'm not understanding fully for some reason, please bear with me.  According to the help, my parameters are correct.  0 = casesense(not sensitive), 1 = 1st occurence, 4 = position to start counting, 2 = 2 chars to search after position start.

How is that incorrect?

And no, I'm not asking for anyone to do it for me, just trying to understand why it's not right.

Share this post


Link to post
Share on other sites

Well for one thing, the substring you're looking for starts at position #5, not 4, so it will never be found.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Kind of things I never remember when I do a stringleft/right with stringinstr if I have to add a -1 or not :D


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Well for one thing, the substring you're looking for starts at position #5, not 4, so it will never be found.

 

Ahh. I thought it started couting at the position AFTER position 4. Gotcha.

Share this post


Link to post
Share on other sites

Hem, testing the presence or not of the string "tw" is not enough ?
Local $TeacherString = StringInStr(@ComputerName, "tw")

If not $TeacherString Then ...   ; means '= 0' , failure, doesn't match
If $TeacherString Then ...   ; means '<> 0'  , success, matches


Share this post


Link to post
Share on other sites
Hem, testing the presence or not of the string "tw" is not enough ?
Local $TeacherString = StringInStr(@ComputerName, "tw")

If not $TeacherString Then ...   ; means '= 0' , failure, doesn't match
If $TeacherString Then ...   ; means '<> 0'  , success, matches


I was going by what the others had said that my stringinstr was incorrect.

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

:blink:  I told you to replace this line :

If $TeacherString =  -1 Then
By this one :

If $TeacherString = 0 Then
On the reply #6. Then you gave an incorrect use of StringInStr, I never told that you were using it badly (at least the function itself). Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

:blink:  I told you to replace this line :

If $TeacherString =  -1 Then
By this one :

If $TeacherString = 0 Then
On the reply #6. Then you gave an incorrect use of StringInStr, I never told that you were using it badly (at least the function itself).

 

I'm still learning this ... I'll get it one day :thumbsup:

Edited by PDowning

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