asiawatcher Posted January 31, 2016 Posted January 31, 2016 i got a text file with this number list (see below) as you can see the number 5 appears twice in this list is there a way we can detect this with any number ? any number appearing more than once in a list to detect it and display a messagebox with that number that appears twice ? cheers 3 2 5 ** 7 9 8 1 5 ** 2
InunoTaishou Posted January 31, 2016 Posted January 31, 2016 What is the list? Is it a file? Try creating an array with the size of the largest number available and increment the count of that array for the corresponding number #include <Array.au3> Local $numbers[] = [3, 2, 5, 7, 9, 8, 1, 5, 2, 3, 2, 9, 1, 33, 22, 1, 4, 33, 8, 0] Local $counter[1] Local $repeating_numbers = "" For $i = 0 to UBound($numbers) - 1 If ($numbers[$i] > UBound($counter)) Then ReDim $counter[$numbers[$i] + 1] $counter[$numbers[$i]] += 1 If ($counter[$numbers[$i]] = 2) Then $repeating_numbers &= $numbers[$i] & @CRLF Next MsgBox("", "Repeating numbers", "These numbers appear more than twice:" & @CRLF & $repeating_numbers) _ArrayDisplay($counter)
asiawatcher Posted January 31, 2016 Author Posted January 31, 2016 (edited) the numbers are located inside a txt file, the numbers are one under the other will this work for this case ? Edited January 31, 2016 by asiawatcher
mikell Posted January 31, 2016 Posted January 31, 2016 ;$txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3" &@crlf& "4" &@crlf& "41" &@crlf& "42" &@crlf& "5" &@crlf& "6" &@crlf& "42" ;Msgbox(0,"", $txt) $txt = FileRead("test.txt") $ret = StringRegExpReplace($txt, '(?s)(\b\d+\b)(?!.*\b\1\b)\R?', "") Msgbox(0,"", "duplicates :" & @crlf & $ret ) asiawatcher 1
asiawatcher Posted January 31, 2016 Author Posted January 31, 2016 bravo !!! mikell thanks for once more
mikell Posted January 31, 2016 Posted January 31, 2016 The previous code is basic You might want to check if there are no duplicates, or if some numbers appear 2 times, 3 times etc In this case the array way is better #Include <Array.au3> $txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3 " &@crlf& "4" &@crlf& "41" &@crlf& "42" &@crlf& "3" &@crlf& "5" &@crlf& "6" &@crlf& "42" ;Msgbox(0,"", $txt) ;$txt = FileRead("test.txt") $all = StringRegExp($txt, '(?m)^(\d+)\s*$', 3) _ArrayDisplay($all) Local $u = UBound($all), $nb[$u][2], $n For $i = 0 to $u-1 $txt = StringRegExpReplace($txt, '\b' & $all[$i] & '\b', "") If @extended > 1 Then $nb[$n][0] = $all[$i] $nb[$n][1] = @extended & " times" $n += 1 EndIf Next If $n = 0 Then Msgbox(0,"", "no duplicates") Else Redim $nb[$n][2] _ArrayDisplay($nb) EndIf
jguinch Posted January 31, 2016 Posted January 31, 2016 Or same thing for each value : #Include <Array.au3> Local $txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3 " &@crlf& "4" &@crlf& "41" &@crlf& "42" &@crlf& "3" &@crlf& "5" &@crlf& "6" &@crlf& "42" Local $uniq = StringRegExp($txt, "(?s)\b(\d+)\b(?!.*\b\1\b)", 3) Local $aResult[UBound($uniq)][2] For $i = 0 To UBound($uniq) - 1 StringRegExpReplace($txt, "\b" & $uniq[$i] & "\b", "") $aResult[$i][0] = $uniq[$i] $aResult[$i][1] = @extended & " times" Next _ArraySort($aResult, 1, 0, 0, 1) _ArrayDisplay($aResult) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
asiawatcher Posted February 1, 2016 Author Posted February 1, 2016 hi all thanks for your answers is there a way to do it like mihell's style without arrays for numbers appearing twice but NOT 3 times (ignore those numbers) cheers
iamtheky Posted February 1, 2016 Posted February 1, 2016 only returns the items that appear the number of times specified $nCount = 2 Local $txt = "3" &@crlf& "1" &@crlf& "2" &@crlf& "3" &@crlf& "4" &@crlf& "6" &@crlf& "42" &@crlf& "3" &@crlf& "5" &@crlf& "6" &@crlf& "42" $aList = stringsplit($txt , @CRLF , 3) _ArraySort($aList) $k = 1 For $i = ubound($aList) - 1 to 0 step -1 If $i > 0 AND $aList[$i]= $aList[$i - 1] Then _ArrayDelete($aList , $i) $k += 1 Else If $k <> $nCount Then _ArrayDelete($aList, $i) $k = 1 EndIf Next _ArrayDisplay($aList , $nCount & " Time(s)") ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
AutoBert Posted February 1, 2016 Posted February 1, 2016 and here's a solution with a func i written 2009: #include <Array.au3> #include <File.au3> $txt = "" For $i=0 to 100 $txt &= Random(1,30,1)&@CRLF Next ;$txt = FileRead('test.txt') ;if txt is ia in a file ;startpoint ;ConsoleWrite($txt & @CRLF) _ArrayDisplay(_countUniqueNumbers($txt), '_countUniqueNumbers') Func _countUniqueNumbers($sText='Test', $sDelim=@CRLF) ;ConsoleWrite($sText & @CRLF) ;returns the count of each unique number in a String ;autor: autobert (autoit.de) 11/2009 $sText = @CRLF&StringReplace($sText, ' ', '') $sText = StringReplace($sText, $sDelim, $sDelim & ' ') $aSource = StringSplit($sText, $sDelim, 3) _ArrayDelete($aSource, 0) ;_ArrayDisplay($aSource,'Original') $aUnique = _ArrayUnique($aSource) _ArrayDelete($aUnique, 0) Dim $aUnique2D[UBound($aUnique)][2] For $x = UBound($aUnique) - 1 To 0 Step -1 $aUnique2D[$x][0] = $aUnique[$x] StringReplace($sText, $aUnique[$x] & $sDelim, 'a') $aUnique2D[$x][1] = @extended If StringStripWS($aUnique2D[$x][0],8)='' Then _ArrayDelete($aUnique2D,$x) Next Return $aUnique2D EndFunc ;==>_countUniqueNumbers EmilyLove 1
EmilyLove Posted February 2, 2016 Posted February 2, 2016 I was going to suggest _ArrayUnique but AutoBert beat me to the punch.
AutoBert Posted February 2, 2016 Posted February 2, 2016 (edited) Meanwhile i saw same method using @extended from StringReplace from Melba23. His solution is shorter (so some ms faster?) and better to read. Edited February 2, 2016 by AutoBert
EmilyLove Posted February 2, 2016 Posted February 2, 2016 2 minutes ago, AutoBert said: Meanwhile i saw same method using @extended from StringReplace from Melba23 it's shorter (so some ms faster?) and better to read. Link?
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now