Jump to content

Compare content of 2 inputs ...


Recommended Posts

The time has come to ask for help :)

I will try to offer a brief description of my problem.

I've made a script to configure network switches and I have to assign different ports (numbered 1 to 24) to different VLANs (virtual LAN) - 2 in this example because extending this would be easy.

Basically I have 2 inputs where numbers (or ranges: 4-7) to be entered separated by commas

ex:

Input1 content = 1, 2, 3-9, 10, 13-22 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 (after the range is converted)

Input2 content = 4, 11, 12, 20-24 = 4, 11, 12, 20, 21, 22, 23, 24

I would like those 2 contents to be compared and if there are common values - to be showed. (in the previous example the common ports are 4, 20, 21 and 22)

I will make a small basic script with 2 inputs in order to save time for anyone trying to help:

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("AForm1", 330, 94, 193, 115)
$Input1 = GUICtrlCreateInput("Input1", 16, 16, 121, 21)
$Input2 = GUICtrlCreateInput("Input2", 160, 16, 137, 21)
$Button1 = GUICtrlCreateButton("Run", 112, 56, 89, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            compare_inputs()
    EndSwitch
WEnd

Func compare_inputs()
    ;the compare takes place here
    MsgBox(0, "Common Values", "..common values displayed here")
EndFunc

In my script I have made a function doing this but it is a long one (more than 100 lines) and using StringSplit, converting to numbers ... and I'm sure it can be done more "elegant" and efficient but unfortunately I can't think of something else and this is the reason for asking for help.

Thank you,

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Hi,

I don't think that i understand what you mean by "common values"?

If you mean that they have to be founded some number of times, then how to know what that's number of times? :)

In general, i think this function (comparing procedure) must involve some 2D Arrays and comparing last values with currently checked in elements of bouth arrays (of inputs), and then store all results in one string that will be displayed at the end...

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • Moderators

#include <ARRAY.AU3>
Global $Input1content = "1, 2, 3-9, 10, 13-22 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22" 
Global $Input2content = "4, 11, 12, 20-24 = 4, 11, 12, 20, 21, 22, 23, 24"
Global $aIn1SRE = StringRegExp($Input1content, "(\d+)", 3)
_ArrayUnique($aIn1SRE, "", 0)
Global $aIn2SRE = StringRegExp($Input2content, "(\d+)", 3)
_ArrayUnique($aIn2SRE, "", 0)
Global $sHoldCompare, $iCC, $xCC

For $iCC = 0 To UBound($aIn1SRE) - 1
    For $xCC = 0 To UBound($aIn2SRE) - 1
        If StringInStr(Chr(1) & $sHoldCompare, Chr(1) & $aIn1SRE[$xCC] & Chr(1)) = 0 And _
            $aIn2SRE[$xCC] = $aIn1SRE[$iCC] Then
            $sHoldCompare &= $aIn2SRE[$xCC] & Chr(1)
        EndIf
    Next
Next

If $sHoldCompare <> "" Then
    $aLikeComparison = StringSplit(StringTrimRight($sHoldCompare, 1), Chr(1))
    _ArrayDisplay($aLikeComparison, "Found Comparable")
Else
    MsgBox(16, "Error", "No Comparison")
EndIf

Func _ArrayUnique(ByRef $aArray, $vDelim = '', $iBase = 1, $iCase = 1)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    If $vDelim = '' Then $vDelim = Chr(01)
    Local $sHold
    For $iCC = $iBase To UBound($aArray) - 1
        If Not StringInStr($vDelim & $sHold, $vDelim & $aArray[$iCC] & $vDelim, $iCase) Then _
            $sHold &= $aArray[$iCC] & $vDelim
    Next
    If $sHold And $iBase = 1 Then
        $aArray = StringSplit(StringTrimRight($sHold, StringLen($vDelim)), $vDelim)
        Return SetError(0, 0, 0)
    ElseIf $sHold And $iBase = 0 Then
        Local $avArray = StringSplit(StringTrimRight($sHold, StringLen($vDelim)), $vDelim)
        ReDim $aArray[UBound($avArray) - 1]
        For $iCC = 1 To UBound($avArray) - 1
            $aArray[$iCC - 1] = $avArray[$iCC]
        Next
        Return SetError(0, 0, 0)
    EndIf
    Return SetError(2, 0, 0)
EndFunc

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.

Link to comment
Share on other sites

I mean, show what the results should be in what conditions... just show the example as you show it in your post, but fill all inputs as they must to be shown, and then in function compare_inputs() in the msgbox instead of "..common values displayed here" show the result that function should calculate... then maybe i can help you to build this function.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

@SmOke_N and @MsCreatoR thanks for all the help.

Tried the code posted and it works - partially after replacing "" with "," in _ArrayUnique($aIn1SRE, "", 0) - comma is the separator.

Why it works partially - it returns 3 matches 4, 21 and 22 and it misses 20 which is common to both strings.

I'm trying to understand the code and I might be able to fix it myself - it will take me a while to figure out the code but it's OK :)

@MsCreatoR:

the inputs are:

$Input1content = "1, 2, 3-9, 10, 13-22"

$Input2content = "4, 11, 12, 20-24"

where 3-9 means everything between 3 and 9 (3, 4, 5, 6, 7, 8, 9) and so on

I want to find which values are present in both places.

the real meaning of "1, 2, 3-9, 10, 13-22" is "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22"

and "4, 11, 12, 20-24" is "4, 11, 12, 20, 21, 22, 23, 24"

you can see that the common values are 4, 20, 21 and 22

Practically the content of this inputbox (the string shown) can be anything. In order to be a valid string it needs to be comma separated (if the values are not separated by commas "," that is bad and it needs to give the user an error message). My script requires a more complicate error control but I can handle that. All I'm looking for is a way to identify common values within those 2 strings.

As I said - I did this but my approach looks like a too complicated one:

here is how my script is doing it:

- StringSplit the 1st string by commas ","

- takes the first element

- StringInStr "-" to see if it is a range or not

---- if not range (it is a number) then store it in an array

---- if range (3-9) get the numbers within and store them in an array

-repeat for all components returned and adding values in the same array

-repeat the same with the 2nd input

-compare the 2 arrays and return the matches

this took me like 100 lines or more and I know it is inefficient - I hoped that someone would be able to show me a shorter way to do it.

Thanks again for help ;)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • Moderators

@SmOke_N and @MsCreatoR thanks for all the help.

Tried the code posted and it works - partially after replacing "" with "," in _ArrayUnique($aIn1SRE, "", 0) - comma is the separator.

Why it works partially - it returns 3 matches 4, 21 and 22 and it misses 20 which is common to both strings.

I'm trying to understand the code and I might be able to fix it myself - it will take me a while to figure out the code but it's OK :)

@MsCreatoR:

the inputs are:

$Input1content = "1, 2, 3-9, 10, 13-22"

$Input2content = "4, 11, 12, 20-24"

where 3-9 means everything between 3 and 9 (3, 4, 5, 6, 7, 8, 9) and so on

I want to find which values are present in both places.

the real meaning of "1, 2, 3-9, 10, 13-22" is "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22"

and "4, 11, 12, 20-24" is "4, 11, 12, 20, 21, 22, 23, 24"

you can see that the common values are 4, 20, 21 and 22

Practically the content of this inputbox (the string shown) can be anything. In order to be a valid string it needs to be comma separated (if the values are not separated by commas "," that is bad and it needs to give the user an error message). My script requires a more complicate error control but I can handle that. All I'm looking for is a way to identify common values within those 2 strings.

As I said - I did this but my approach looks like a too complicated one:

here is how my script is doing it:

- StringSplit the 1st string by commas ","

- takes the first element

- StringInStr "-" to see if it is a range or not

---- if not range (it is a number) then store it in an array

---- if range (3-9) get the numbers within and store them in an array

-repeat for all components returned and adding values in the same array

-repeat the same with the 2nd input

-compare the 2 arrays and return the matches

this took me like 100 lines or more and I know it is inefficient - I hoped that someone would be able to show me a shorter way to do it.

Thanks again for help ;)

It got all 4 of those for me, just the way I posted it. I don't know why you are replacing "" with ",", it's just a delimiter, that can be preordained by the function itself.

*Edit:

What version of AutoIt are you running? Is it one that supports the revised StringRegExp?

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.

Link to comment
Share on other sites

You're right - considering how the strings look it works but once I change the strings to the proper form (either "1, 2, 3-9, 10, 13-22" OR "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22") it doesn't work any more.

If the string is "1, 2, 3-9, 10, 13-22" I get no match

if the string is "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22" I get 3 matches (number 20 is not there)

I apologize if my OP was not clear enough - I showed the "extended" form of this string in order to make the things clear but ... it looks like all I did was to confuse others.

Thanks,

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Ok, here is what i came up with:

$Input1content = "1, 2, 3-9, 10, 13-22" 
$Input2content = "4, 11, 12, 20-24"

$ComarationResults = Compare_Inputs($Input1content, $Input2content, ",")

MsgBox(64, "Compare Results", "The results are: " & $ComarationResults)

Func Compare_Inputs($Str1, $Str2, $Delim=",")
    Local $Nums_1_Arr = _ExpandNumsToArray($Str1, $Delim)
    Local $Nums_2_Arr = _ExpandNumsToArray($Str2, $Delim)
    
    Local $RetComparation = '', $SearchInStr2
    Local $BiggestArray = $Nums_1_Arr
    Local $SmallestArray = $Nums_2_Arr
    
    If UBound($BiggestArray) < UBound($SmallestArray) Then
        $BiggestArray = $Nums_2_Arr
        $SmallestArray = $Nums_1_Arr
    EndIf
    
    For $i = 1 To UBound($BiggestArray)-1
        $SearchInStr2 = _FindInArray($SmallestArray, $BiggestArray[$i])
        If $SearchInStr2 <> -1 Then $RetComparation &= $BiggestArray[$i] & $Delim & " "
    Next
    Return StringRegExpReplace($RetComparation, '[' & $Delim & '|\s]+\z', '')
EndFunc

Func _ExpandNumsToArray($Nums, $Delim)
    Local $NumsArr = StringSplit(StringStripWS($Nums, 8), $Delim)
    Local $RetArr[1], $ExpandedNumsArr, $SepPos
    
    For $i = 1 To UBound($NumsArr)-1
        $SepPos = StringInStr($NumsArr[$i], "-")
        If $SepPos > 1 And $SepPos < StringLen($NumsArr[$i]) Then
            $ExpandedNumsArr = StringSplit(_ExpandNumbers($NumsArr[$i], $Delim), $Delim)
            For $j = 1 To UBound($ExpandedNumsArr)-1
                ReDim $RetArr[UBound($RetArr)+1]
                $RetArr[UBound($RetArr)-1] = $ExpandedNumsArr[$j]
            Next
        Else
            ReDim $RetArr[UBound($RetArr)+1]
            $RetArr[UBound($RetArr)-1] = $NumsArr[$i]
        EndIf
    Next
    Return $RetArr
EndFunc

Func _ExpandNumbers($RangeNum, $Delim)
    Local $SplitNum = StringSplit($RangeNum, "-")
    Local $RetNumbers = ''
    
    For $i = Number($SplitNum[1]) To Number($SplitNum[2])
        $RetNumbers &= $i & $Delim
    Next
    Return StringRegExpReplace($RetNumbers, '[' & $Delim & '|\s]+\z', '')
EndFunc

Func _FindInArray(ByRef $avArray, $Str)
    For $i = 1 To UBound($avArray)-1
        If $avArray[$i] = $Str Then Return $i
    Next
    Return -1
EndFunc

50 lines of functions coding :)

EDIT: A litle correction to the functions and Local declaration added to some variables.

EDIT2: I have chenged the main function, so now it's no mather what inputs to compare, there is array size checking procedure - the biggest array is taken as main array to check in (for comparation values).

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...