Sign in to follow this  
Followers 0
kor

Best way to compare a variable to 30+ strings?

10 posts in this topic

So I am writing a script that creates employees in AD. I receive a spreadsheet from our employee tracking system that gives me certain information such as first name last name, etc.

One of the items is Job Title. Now, our employee tracking system is VERY specific about job title. (Manager I, Manager II, Executive Manager, Helper IV, Helper III, etc) In AD though we don't much care about all the minutia and we only care if your a manager, a helper, a supervisor, etc.

So basically we want to take the 30+ possible job titles and condense them down into only a handful. We can't do StringRegEx looking for keywords because someone might be a Helper Manager so simply having the word "manager" doesn't automatically put them into a manager position.

So far the best way I've worked it out is to basically have a Case statement for EACH potential job title (30+) and then just condense them down to different titles within the case statements. I'm not sure if that is the best way to go about it though since that's going to be a lot of code for each and every possible job title. I figure it would look something like this.

Select
    Case $title = "IA II - ELL"
        $job = "Helper"
    Case $title = "IA III - Contained"
        $job = "Helper"
    Case $title = "Monitor"
        $job = "Helper"
    Case $title = "Manager III - Tech"
        $job = "Manager"
    Case $title = "Tech Mentor II - Contained"
        $job = "Teacher"
EndSelect

Just imagine that but with 30 or more case statements.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

As a quickie, to get the ball rolling, have you thought about something like the following:

$Title = StringTrimRight(StringReplace($title, "I", "")) ; Remove the 'I', 'II' information
$Title = StringTrimRight(StringReplace($title, "V", "")) ; Remove the 'V' information
  >> Other code to cleanup further, e.g. remove the info after " - " <<

Switch $Title ; We now have a nice clean variable to work with
Case "IA"
$job = "Helper"
Case "Monitor"
$job = "Helper"
...
EndSwitch

When you get down to it, it's not that big an issue if you need to consider a million and 1 items, it's a script and runs as such.

The next question though is: Why are you using AutoIT to do this? Why not use VBA since you're already working within an Excel Spreadsheet...?

Edited by Mallie99

Are you telling me something I need to know or something I want to know?

Share this post


Link to post
Share on other sites

Mallie99,

You are on the right track. Here are two different ways you could go about it.

Select
    Case StringInStr("|title1|title2|title3|", "|" & $title & "|")
        $job = "Category1"
    Case StringInStr("|title4|title5|title6|", "|" & $title & "|")
        $job = "Category2"
EndSelect

Switch $title
    Case "title1", "title2", "title3"
        $job = "Category1"
    Case "title4", "title5", "title6"
        $job = "Category2"
EndSwitch

Share this post


Link to post
Share on other sites

Switch $title
    Case "title1", "title2", "title3"
        $job = "Category1"
    Case "title4", "title5", "title6"
        $job = "Category2"
EndSwitch

I tried this method before, it didn't want to play... Which made me most aggrieved.

Are you telling me something I need to know or something I want to know?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi kor,

what about storing all matches in 2 arrays like:

$a1[1] = "IA II - ELL"

$a2[1] = "Helper"

$a1[2] = "IA III - Contained"

$a2[3] = "Helper"

...

And then running an _ArraySearch($a1, "Searchstring") to get the index?

Should be easier than running a Switch ... Case.

:)

Edit:

And well it still can be combined with what Mallie99 and willychan wrote. :)

Edited by Hannes123

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

We can't do StringRegEx looking for keywords because someone might be a Helper Manager so simply having the word "manager" doesn't automatically put them into a manager position.

Then when does it automatically put them into a manager position? If you can not answer that question, then you can't automate the task. Make sure to always be specific when making requirements.

For example, in what category would you assign a teacher manager? Is he principally a teacher or a manager? Does, for example, having the word "manager" in the name automatically assign someone to manager? If not manager and containing word "teacher", then teacher. If not manager or teacher, then helper.

You are not asking of us for help with your code, but how to code rules that will automatically do it. But if you want to code rules, then you need business rules. Rules that probably you are making. So make those, then automate!

Edited by Manadar

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I tried this method before, it didn't want to play... Which made me most aggrieved.

Your example above used select instead of switch, so it wouldn't have worked.

Both of the methods I gave you, I tested and worked perfectly here on my end. Did you try method 1?

Edited by willichan

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Your example above used select instead of switch, so it wouldn't have worked.

What about method 1?

:) You got me confused, my fault I think... I write a lot of PHP so I never really use SELECT I always use SWITCH. Edited by Mallie99

Are you telling me something I need to know or something I want to know?

Share this post


Link to post
Share on other sites

I did try Method1 willichan and that is pretty much exactly what we were looking for. There will be a few Case statements though that wont use StringInStr and will instead use StringRegEx because as manadar mentioned we will need to go to HR and say look, if a user has the word manager in their title then they MUST be a manager.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

:) You got me confused, my fault I think... I write a lot of PHP so I never really use SELECT I always use SWITCH.

AutoIt has two different Case structures.

Select puts a conditional statement with each case statement. In my example, the StrInStr() statement evaluates as true (>0) if found, and false (0) if not.

Switch puts a statement (value) on the switch line, and each case statement has a potential value or list of values to compare to the switch statement.

--- Edit ---

My apologies to Kor and Mallie99. I probably did cause some confusion. I confused your responses as if you were the same person.

Kor. Glad things are working. Good luck with HR.

Edited by willichan

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