civilcalc Posted March 9, 2010 Share Posted March 9, 2010 Ok so I have some data, in this instance colours. I want to be able to count how many letter A's there are, then B's and so on..... Amber Azure Beige Black Blond Brass Brown Is there a simple way of doing this? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 9, 2010 Moderators Share Posted March 9, 2010 civilcalc, This is one way to do it: #include <Array.au3> $sData = "Amber" & @CRLF & _ "Azure" & @CRLF & _ "Beige" & @CRLF & _ "Black" & @CRLF & _ "Blond" & @CRLF & _ "Brass" & @CRLF & _ "Brown" Global $aLetters[26][2] For $i = 0 To 25 $aLetters[$i][0] = Chr(65 + $i) StringRegExpReplace($sData, Chr(65 + $i), "") $aLetters[$i][1] = @extended StringRegExpReplace($sData, Chr(97 + $i), "") $aLetters[$i][1] += @extended Next _ArrayDisplay($aLetters) M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 9, 2010 Share Posted March 9, 2010 (edited) This is one way to do it: Faster: #include <Array.au3> $sData = "Amber" & @CRLF & _ "Azure" & @CRLF & _ "Beige" & @CRLF & _ "Black" & @CRLF & _ "Blond" & @CRLF & _ "Brass" & @CRLF & _ "Brown" Global $aLetters[26] $iTimer = TimerInit() $aData = StringSplit($sData, "") For $n = 1 To UBound($aData) - 1 $iChar = Asc($aData[$n]) - 65 If $iChar >= 32 Then $iChar -= 32 If ($iChar < 0) Or ($iChar > 25) Then ContinueLoop $aLetters[$iChar] += 1 Next $iTimer = TimerDiff($iTimer) _ArrayDisplay($aLetters, "Time = " & Round($iTimer / 1000, 3) & "sec") Edited March 9, 2010 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Fulano Posted March 9, 2010 Share Posted March 9, 2010 Not that much faster, but you can cut out one line:#include <Array.au3> $sData = "Amber" & @CRLF & _ "Azure" & @CRLF & _ "Beige" & @CRLF & _ "Black" & @CRLF & _ "Blond" & @CRLF & _ "Brass" & @CRLF & _ "Brown" Global $aLetters[26] $iTimer = TimerInit() $aData = StringSplit($sData, "") For $n = 1 To UBound($aData) - 1 $iChar = Asc($aData[$n]) - 65 If $iChar >= 32 Then $iChar -= 32 If $iChar >= 0 And $iChar < 26 Then $aLetters[$iChar] += 1 Next $iTimer = TimerDiff($iTimer) _ArrayDisplay($aLetters, "Time = " & Round($iTimer / 1000, 3) & "sec")Also, the majority of punctuation marks, and the numbers are below the range of letters, with the And the if statement can 'short circuit' and avoid having the check $iChar < 26, with the Or statement it will check both every time. Also shouldn't make that big of a difference, unless you are counting a couple of million lines of text #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja! Link to comment Share on other sites More sharing options...
dani Posted March 9, 2010 Share Posted March 9, 2010 (edited) Totally unrelated, forgive me In Ruby: words = %w[Amber Azure Beige Black Blond Brass Brown].map(&:downcase) # Downcase to prevent different counts for a and A, b and B etc. count = Hash[('a'..'z').map { |char| [char, 0] }] # Hash that holds a through z mapped to the value 0 words.each { |word| word.each_char { |char| count[char] += 1 } } # Counts every character and updates the Hash count.sort.each { |letter, count| puts "#{letter}: #{count}" } # Displays Hash Edited March 9, 2010 by dani Link to comment Share on other sites More sharing options...
Yoriz Posted March 9, 2010 Share Posted March 9, 2010 (edited) Couple more ways and also pretty fast p.s. Try the 2nd version on a lot of text and it is very fast $sData = "Amber" & @CRLF & _ "Azure" & @CRLF & _ "Beige" & @CRLF & _ "Black" & @CRLF & _ "Blond" & @CRLF & _ "Brass" & @CRLF & _ "Brown" Global $aLetters[26] $iTimer3 = TimerInit() $sData = StringStripWS($sData,8) $iStringLen = stringlen($sData) $iChar = 65 Do $sData = StringReplace($sData,Chr($iChar),"") $iNewStringLen = stringlen($sData) $aLetters[$iChar-65] = Chr($iChar) & " - " & $iStringLen-$iNewStringLen $iStringLen = $iNewStringLen $iChar +=1 until Not $iStringLen $iTimer3 = TimerDiff($iTimer3) _ArrayDisplay($aLetters, "Time = " & Round($iTimer3 / 1000, 5) & "sec") $sData = "Amber" & @CRLF & _ "Azure" & @CRLF & _ "Beige" & @CRLF & _ "Black" & @CRLF & _ "Blond" & @CRLF & _ "Brass" & @CRLF & _ "Brown" Global $aLetters[26] Global $aStrings[26] = ["e","t","a","o","i","n","s","r","h","l","d","c","u", _ "m","f","p","g","w","y","b","v","k","x","j","q","z"] ; supposed frequency order $iTimer4 = TimerInit() $sData = StringStripWS($sData,8) $iStringLen = stringlen($sData) $iChar = 0 Do $sData = StringReplace($sData,$aStrings[$iChar],"") $iNewStringLen = stringlen($sData) $aLetters[$iChar] = $aStrings[$iChar] & " - " & $iStringLen-$iNewStringLen $iStringLen = $iNewStringLen $iChar +=1 until Not $iStringLen $iTimer4 = TimerDiff($iTimer4) _ArrayDisplay($aLetters, "Time = " & Round($iTimer4 / 1000, 5) & "sec") Edited March 9, 2010 by Yoriz GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF. Link to comment Share on other sites More sharing options...
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