Jump to content

Adding two digit numbers from 0-9


Recommended Posts

Hi Experts,

Good day! hope all have a great day:)...

I have this thread that generate two digit numbers from the input given (thanks to mikell) that provides four types of output. I separately post another thread because I don't want to mashup my concern and by doing a step by step posting it make's it easier to our experts to understand.

Anyways, having this output "89", "01", "81", "09" generated from the code given my @mikell. I would like to insert them in a range of 0-9, means like "089", "189", "289", "389".... until it reaches "989" and until these four numbers ("89", "01", "81", "09") were distributed.

The output would be:

089, 189, 289, 389, 489, 589, 689, 789, 889, 989
001, 101, 201, 301, 401, 501, 601, 701, 801, 901
081, 181, 281, 381, 481, 581, 681, 781, 881, 981
009, 109, 209, 309, 409, 509, 609, 709, 809, 909

As you can see above unique numbers, highlighted in bold were the range 0-9, the output was inserted and was distributed.

These unique numbers will be used for our tracking database filenaming so will have our clue what tracking number to find. For example, my production wants to check for tracking number 389 and the succeeding range, then we can easily given him/her the 389 up to 989 just to explain things for the purpose and if someone want's it in the future (maybe?).^_^:sweating:

Apology if I don't have the code yet, I'm still googling for possible coding that should be made. I can find something I'll post it here.

I have now the code but the problem is I don't know how to continue the loop until all were distributed to 0-9.

#include <Array.au3>
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

Local $sTimeInput = "190"

$aTimeInput = StringSplit($sTimeInput, "")
$a = $aTimeInput[$aTimeInput[0] - 1] +10
$b = $aTimeInput[$aTimeInput[0]] + 10

Local $aResults[4]
$aResults[0] = Mod($a-1, 10) & Mod($b-1, 10)
$aResults[1] = Mod($a+1, 10) & Mod($b+1, 10)
$aResults[2] = Mod($a-1, 10) & Mod($b+1, 10)
$aResults[3] = Mod($a+1, 10) & Mod($b-1, 10)

For $i = 0 to 9
   Local $dNumber = Number($i) & Mod($a-1, 10) & Mod($b-1, 10)
   MsgBox($MB_SYSTEMMODAL, "Testing", "The following string were added with numeric value:" & @CRLF & _
        $i & " = " & $dNumber & @CRLF)
   $path = @ScriptDir & "\Tracking.txt"
   $File = FileOpen($path, 1)
   FileWrite($path, $dNumber & @CRLF)
Next
FileClose($File)

 

Thanks in advance Experts! If you need more details, please let me know.

 

KS15

 

 

Edited by KickStarter15
Adding code for reference

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Hi Experts,

I have the below code to generate what I want but I think there's another way of doing this without declaring them back again and again.:sweating:

#include <Array.au3>
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

Local $sTimeInput = "190"

$aTimeInput = StringSplit($sTimeInput, "")
$a = $aTimeInput[$aTimeInput[0] - 1] +10
$b = $aTimeInput[$aTimeInput[0]] + 10

For $i = 0 to 9
   Local $aResults[4]
   $aResults[0] = Mod($a-1, 10) & Mod($b-1, 10)
   $aResults[1] = Mod($a+1, 10) & Mod($b+1, 10)
   $aResults[2] = Mod($a-1, 10) & Mod($b+1, 10)
   $aResults[3] = Mod($a+1, 10) & Mod($b-1, 10)
   Local $dNumber0 = Number($i) & Mod($a-1, 10) & Mod($b-1, 10)
   Local $dNumber1 = Number($i) & Mod($a+1, 10) & Mod($b+1, 10)
   Local $dNumber2 = Number($i) & Mod($a-1, 10) & Mod($b+1, 10)
   Local $dNumber3 = Number($i) & Mod($a+1, 10) & Mod($b-1, 10)

   $path = @ScriptDir & "\Tracking.txt"
   $File = FileOpen($path, 1)
   FileWrite($path, $dNumber0 & @CRLF)
   FileWrite($path, $dNumber1 & @CRLF)
   FileWrite($path, $dNumber2 & @CRLF)
   FileWrite($path, $dNumber3 & @CRLF)
Next
FileClose($File)

I think I know how but I can't recall or remember anything how.:>

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

You might directly go for a 2D array

#include <Array.au3>

Local $sTimeInput = "190"

$aTimeInput = StringSplit($sTimeInput, "")
$a = $aTimeInput[$aTimeInput[0] - 1] +10
$b = $aTimeInput[$aTimeInput[0]] + 10

Local $aResults[4][10]
For $j = 0 to 9
    $aResults[0][$j] = $j & Mod($a-1, 10) & Mod($b-1, 10)
    $aResults[1][$j] = $j & Mod($a+1, 10) & Mod($b+1, 10)
    $aResults[2][$j] = $j & Mod($a-1, 10) & Mod($b+1, 10)
    $aResults[3][$j] = $j & Mod($a+1, 10) & Mod($b-1, 10)
Next
_ArrayDisplay($aResults)
FileWrite(@ScriptDir & "\Tracking.txt", _ArrayToString($aResults, @crlf))

 

Link to comment
Share on other sites

@mikell, Thanks, it work as expected.:lol:

 

Instead of posting another thread, can I ask you some little question? I have another case in finding the duplicate line from the tracking.txt and I tried below code but it wasn't viewing the deleted duplicate line, I just need to get the duplicate line and should not delete the duplicate line.

#Include <File.au3>
#include <Array.au3>
Dim $oFile,$nFile

_FileReadToArray("Tracking.txt",$oFile)
$nFile = _ArrayUnique($oFile)
_ArrayDisplay($nFile)
; I need to display the duplicate line in message box.

This code will delete the line and I don't want to delete the line I just want to show what are the duplicates.:>

 

Thanks,

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Another example answering the 1st question this thread.

#include <Array.au3>

Local $sTimeInput = "190" ; "we90" ; "90" ; "123"
MsgBox(0, "Result", _NumCombine($sTimeInput))


Func _NumCombine($sInput)
    Local $aP_M[8] = [-1, -1, 1, 1, -1, 1, 1, -1] ; Plus or Minus Array
    Local $aTI, $sRet = ""
    If StringRegExp($sInput, "(\d\d)$") Then ; Check $sInput variable ends with 2 digits.
        $aTI = StringRegExp($sInput, "(.?)(\d)(\d)$", 3)
    Else
        Return ""
    EndIf
    For $j = 0 To UBound($aP_M) - 1 Step 2
        For $i = 0 To 9
            $sRet &= $i & Mod(10 + $aTI[1] + $aP_M[$j], 10) & Mod(10 + $aTI[2] + $aP_M[$j + 1], 10) & ", "
        Next
        $sRet = StringTrimRight($sRet, 2) & @CRLF
    Next
    Return StringTrimRight($sRet, 2)
EndFunc   ;==>_NumCombine

#cs ; For "123" returns:-
012, 112, 212, 312, 412, 512, 612, 712, 812, 912
034, 134, 234, 334, 434, 534, 634, 734, 834, 934
014, 114, 214, 314, 414, 514, 614, 714, 814, 914
032, 132, 232, 332, 432, 532, 632, 732, 832, 932

For "190" returns:-
089, 189, 289, 389, 489, 589, 689, 789, 889, 989
001, 101, 201, 301, 401, 501, 601, 701, 801, 901
081, 181, 281, 381, 481, 581, 681, 781, 881, 981
009, 109, 209, 309, 409, 509, 609, 709, 809, 909
#ce

 

An example for 2nd question this thread  (View duplicate lines).

#include <Array.au3>

Local $a = FileReadToArray("Tracking.txt")
_ArraySort($a)
Local $s = _ArrayToString($a, @CRLF)
Local $aDup = StringRegExp($s, "(?m)(^.+$)\v+\1", 3) ; Get an array of adjacent duplicate lines, that are separated by one or more vertical white spaces,"\v+".

If IsArray($aDup) Then
    MsgBox(0, "Duplicate Lines", _ArrayToString($aDup, @CRLF & @CRLF))
Else
    MsgBox(0, "Duplicate Lines", "No duplicate lines present.")
EndIf

 

Link to comment
Share on other sites

Another way, using Scripting.Dictionary

#include <Array.au3>

Global $aArray[10] = ["a","a","b","c","c","c","c","d","e","e"]
Global $aRes = Example($aArray)
_ArrayDisplay($aRes)

Func Example($aArray)
  Local $oTst = ObjCreate("Scripting.Dictionary")
  Local $oRes = ObjCreate("Scripting.Dictionary")
  For $i = 0 To UBound($aArray) - 1
    If Not $oTst.Exists($aArray[$i]) Then
      $oTst($aArray[$i]) = 1
    ElseIf Not $oRes.Exists($aArray[$i]) Then
      $oRes($aArray[$i]) = 1
    EndIf
  Next
;  Return $oTst.Keys()  ; = ArrayUnique
  Return $oRes.Keys()   ; duplicates only
EndFunc

 

Link to comment
Share on other sites

20 hours ago, Malkey said:

Another example answering the 1st question this thread.

#include <Array.au3>

Local $sTimeInput = "190" ; "we90" ; "90" ; "123"
MsgBox(0, "Result", _NumCombine($sTimeInput))


Func _NumCombine($sInput)
    Local $aP_M[8] = [-1, -1, 1, 1, -1, 1, 1, -1] ; Plus or Minus Array
    Local $aTI, $sRet = ""
    If StringRegExp($sInput, "(\d\d)$") Then ; Check $sInput variable ends with 2 digits.
        $aTI = StringRegExp($sInput, "(.?)(\d)(\d)$", 3)
    Else
        Return ""
    EndIf
    For $j = 0 To UBound($aP_M) - 1 Step 2
        For $i = 0 To 9
            $sRet &= $i & Mod(10 + $aTI[1] + $aP_M[$j], 10) & Mod(10 + $aTI[2] + $aP_M[$j + 1], 10) & ", "
        Next
        $sRet = StringTrimRight($sRet, 2) & @CRLF
    Next
    Return StringTrimRight($sRet, 2)
EndFunc   ;==>_NumCombine

#cs ; For "123" returns:-
012, 112, 212, 312, 412, 512, 612, 712, 812, 912
034, 134, 234, 334, 434, 534, 634, 734, 834, 934
014, 114, 214, 314, 414, 514, 614, 714, 814, 914
032, 132, 232, 332, 432, 532, 632, 732, 832, 932

For "190" returns:-
089, 189, 289, 389, 489, 589, 689, 789, 889, 989
001, 101, 201, 301, 401, 501, 601, 701, 801, 901
081, 181, 281, 381, 481, 581, 681, 781, 881, 981
009, 109, 209, 309, 409, 509, 609, 709, 809, 909
#ce

 

Wow, this one is awesome. Nice mikell, thanks. Thumbs up!;)

 

20 hours ago, Malkey said:

An example for 2nd question this thread  (View duplicate lines).

Yah it definitely show's the duplicate found from the database. Thank you so much @mikell.!!!

Now I can connect all the task given to me and I only have one last thing to ask if it's okay mikell....:sweating:. If I want to locate all the same numbers from the $input let's say the input is "190" and locate the duplicate numbers in our database using the input provide and show the line from the text file. Is the below code editable for that?


For example:
I have this database called "Tracking.txt" where all the tracking numbers were saved, then from the input given "190", i need to get the line with "190" in it.

sample line: 123, 456, 789, 110, 190

#include <Array.au3>

Local $a = FileReadToArray("Tracking.txt")
_ArraySort($a)
Local $s = _ArrayToString($a, @CRLF)
Local $aDup = StringRegExp($s, "(?m)(^.+$)\v+\1", 3) ; Get an array of adjacent duplicate lines, that are separated by one or more vertical white spaces,"\v+".

If IsArray($aDup) Then
    MsgBox(0, "Duplicate Lines", _ArrayToString($aDup, @CRLF & @CRLF))
Else
    MsgBox(0, "Duplicate Lines", "No duplicate lines present.")
EndIf

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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...