Sign in to follow this  
Followers 0
Legacy99

StringRegExp and computer name matches

12 posts in this topic

Hey All,

I'm really new at this and maybe I'm going about this the wrong way, I'm hoping someone can help. I'm trying match part of a computers name from an array to the actual computer name and do some task if it matches the department. For example if computer name is dept1-joeblow then reset local password to this, if computername is Site2-johndoe then reset local password to that (the event is irrelevant, it's just an example)

Here is what I got

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")
For $i = $arrDeptCodes to $arrDeptCodes
    $n = StringRegExp(@ComputerName, $i)
    If $n = 1 Then;Gets a Match
        MsgBox (1, "Match", "Match = " & $i & @ComputerName)
    Else; No Match
        MsgBox (1, "No Match", "Not = " & $i & @ComputerName)
    EndIf
Next

The problem is that the first dept does not display and it $n gets set to 1 when it should be 0. The rest of the values are always 0. I really can't wrap my head around this one for some reason.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You need to have an understanding of arrays first.

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")oÝ÷ Øæï¬'+y«^±©Ú®¶²¶¬¢w¡jÎrدyé^éí³Ka® ®¶­sdf÷"b33c¶Òb33c¶'$FWD6öFW2Fòb33c¶'$FWD6öFWoÝ÷ Øò¢êeiÇjg¬±¨npØcºËtߦ«¬7©´*zȧܨ»
.ÛzØZ¶+b±¹ZK(Ê­æÚ²'¬k(§Z+Ó~¢oÝ÷ ÛMúÊ'µ¨§²Ø^rêëz{gºfÞ®íéh¢­Ê'jëh×6$arrDeptCodes[6] = ["Dept1-","Dept2-","Site1-","Site2-","core1-","core2-"]
For $i = 0 To UBound($arrDeptCodes) - 1 ; UBound contains the number of elements in the 1 dimensional array
    If StringRegExp(@ComputerName, $arrDeptCodes[$i] & '.+?') Then ; If @ComputerName Contains $arrDeptCodes[ElementNumber] First, and more characters to follow Then
        MsgBox (1, "Match", "Match = " & $i & @ComputerName)
    Else; No Match
        MsgBox (1, "No Match", "Not = " & $i & @ComputerName)
    EndIf
Next

I typed this in the reply box, so don't know if there are any mistakes :rolleyes:

Edit:

Damn forum makes nearly impossible to edit CODE!!! :rambo:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Legacy was pretty close with his own code.

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")
For $i in $arrDeptCodes

Share this post


Link to post
Share on other sites

Legacy was pretty close with his own code.

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")
For $i in $arrDeptCodes
No he wasn't... might want to re-look at the "whole" picture...

Changing the method of the loop doesn't make it "close".


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

What I posted was close to his for loop declaration. I just prefer to use a For In on a one dimensional array.

Share this post


Link to post
Share on other sites

Hi guys,

Thanks for the input. This is a huge learning curve. I tried both suggestions and came up with this that works, execpt, the pattern is case senstive. The computer name must match the case of the array. For example, if the computer is named dept1- it does not match. How do I overcome that hurdle?

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")

$n = 0
For $i = 0 to UBound($arrDeptCodes) -1  
    $display_dept = $arrDeptCodes[$i]
    $n = StringRegExp(@ComputerName, $display_dept, 0)

    If $n = 1 Then
        MsgBox (1, "Match", "MATCH: N= " & $n & ' computer= ' & @ComputerName & " i= " & $i);This output is for my sanity
    Else
        MsgBox (1, "No Match", "NO MATCH: N= " & $n & ' computer= ' & @ComputerName & " i= " & $i);Same here.
    EndIf
Next

Share this post


Link to post
Share on other sites

#include <Array.au3>

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")

For $i in $arrDeptCodes
    $n = StringRegExp(@ComputerName, "(?i)" & $arrDeptCodes[$i], 0)

    If $n Then
        MsgBox (1, "Match", "MATCH: N= " & $n & ' computer= ' & @ComputerName & " i= " & $i);This output is for my sanity
        ExitLoop
    EndIf
Next

If NOT $n Then MsgBox (1, "No Match", "NO MATCH: N= " & $n & ' computer= ' & @ComputerName & " i= " & $i);Same here.

Share this post


Link to post
Share on other sites

After talking with a friend about this (Thanks JR)

he suggested:

$pcname = StringUpper(@ComputerName)
$n = StringRegExp($pcname, $display_dept, 0)

and replace all the @ComputerName with $pcname. It works for my needs.

Thanks guys.

Share this post


Link to post
Share on other sites

@weaponx.

the [$i] in line $n = StringRegExp(@ComputerName, "(?i)" & $arrDeptCodes[$i], 0)

is passing the dept name not the row number so I always get a fail.

Tell me if I'm wrong but it would litererally read "test string"=%computername% in the array "arrDeptCodes" @ row Dept1 with pattern Dept1, @ row Dept2 with pattern Dept2, etc. In the msgbox, $i = core2- every time since it's the last in the array. Am I crazy or is that what it's doing?

Regardless if I'm crazy, very interesting stuff.

Thanks

Share this post


Link to post
Share on other sites

oops sorry

#include <Array.au3>

$arrDeptCodes = _ArrayCreate("Dept1-", "Dept2-", "Site1-", "Site2-", "core1-", "core2-")

For $i in $arrDeptCodes
    $n = StringRegExp(@ComputerName, "(?i)" & $i, 0)
    
    If $n Then
        MsgBox (1, "Match", "MATCH: N= " & $n & ' computer= ' & @ComputerName & " i= " & $i);This output is for my sanity
        ExitLoop
    EndIf
Next

If NOT $n Then MsgBox (1, "No Match", "NO MATCH: N= " & $n & ' computer= ' & @ComputerName & " i= " & $i);Same here.

Share this post


Link to post
Share on other sites

Hey no worries man, I was just trying to understand what you did, not beat up on you. Thanks for the input. After I seen the edit I should have just looked at it closer and figured it out myself, it makes total sense now so all is not lost. I really want to understand it, not just blindly copy and paste.

PS- it does work your way too :rolleyes:

Share this post


Link to post
Share on other sites

No problem. Your previous method was just continuing the loop even if a match was found and I don't think thats what you wanted. If you need any other help aside from the topic I can probably help.

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