leuce Posted October 27, 2011 Share Posted October 27, 2011 G'day everyone Let me first explain my situation. I have sentences like this: The [[ANT]] told the [[BEE]] that the [[COW]] took the [[DOG]] for a walk. After processing in the script, it must read: The {0/} told the {1/} that the {2/} took the {3/} for a walk. The text to change will always start and end with [[ and ]], and no other instances of [[ or ]] will ever occur in the input text, and the numbering should always start with 0 and be sequential. The problem is that someones bracketed items occur more than once, so this input text: The [[ANT]] told the [[BEE]] that the [[ANT]] and the [[COW]] took the [[DOG]] for a walk. Should be output after processing as: The {0/} told the {1/} that the {0/] and the {2/} took the {3/} for a walk. Accomplishing the first one (i.e. no repeating items) should be simple (just add them to an array and use the items number in the array). However, is there a way to ensure that only the unique items are added to the array (or alternatively to delete repeating items in the array)? I can think of really complex ways of accomplishing this, but is there a simple way to delete repeating items from an array so that only the first instance remains in the array? Thanks Samuel Link to comment Share on other sites More sharing options...
Realm Posted October 27, 2011 Share Posted October 27, 2011 (edited) Hello Leuce, Lookup _StringBetween() in your help file. It is perfect for your needs. #include <String.au3> #include <Array.au3> $string = 'The [[ANT]] told the [[BEE]] that the [[COW]] took the [[DOG]] for a walk.' _Main() Func _Main() Local $aArray1 = _StringBetween( $string, '[[', ']]') _ArrayDisplay($aArray1, 'Default Search') EndFunc ;==>_Main Realm Edit: I'm sorry I didn't read the last section of your post until after I had replied. You could Use _ArrayUnique(), also in your help file, to return only unique variables. Edit 2: Thought I'd include an example. Hope this is what your looking for. #include <String.au3> #include <Array.au3> Global $string = 'The [[ANT]] told the [[BEE]] that the [[ANT]] and the [[COW]] took the [[DOG]] for a walk.' Global $aArray1, $uniqueArray, $returnArray _Main() Func _Main() Local $aArray1 = _StringBetween( $string, '[[', ']]' ) _ArrayDisplay( $aArray1, 'Full Array' ) $uniqueArray = _ArrayUnique( $aArray1 ) _ArrayDisplay( $uniqueArray, 'Unique Array' ) Dim $returnArray[UBound($uniqueArray)-1] ;Since you need the array returned zero base, One extra small step: For $i = 1 To UBound( $uniqueArray ) - 1 $returnArray[$i-1] = $uniqueArray[$i] Next _ArrayDisplay( $returnArray, 'Zero Base Return array' ) EndFunc ;==>_Main Edited October 27, 2011 by Realm My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. Link to comment Share on other sites More sharing options...
leuce Posted October 27, 2011 Author Share Posted October 27, 2011 Lookup _StringBetween() in your help file. It is perfect for your needs. ...You could Use _ArrayUnique(), also in your help file, to return only unique variables. ...Edit 2: Thought I'd include an example. Hope this is what your looking for. Thanks for the code snippet -- it saves me a lot of time (I understand the principles and I make extensive use of the help file, but it would have taken me 2 hours to get the code snipped right myself). By the way: This script would be for a workaround when I translate stuff in a software localisation program. The source text would read something like "Unable to get SOME_CODE_HERE from SOME_MORE_CODE_HERE", but the poor human translator is expected to type "Unable to get {0/} from {1/}". My present non-scripted method is to simply use {0/} for all placeables and then afterwards fix the misnumbered ones manually, but if I can get a script to number them for me correctly, I could work a lot faster. Link to comment Share on other sites More sharing options...
leuce Posted October 27, 2011 Author Share Posted October 27, 2011 For the record, here is the completed script, attached. gtt_fix_placeables.zip Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 27, 2011 Moderators Share Posted October 27, 2011 leuce, This is my version of how to do the various replacements: #include <Array.au3> ; Original text $sText = "The [[ANT]] told the [[BEE]] that the [[COW]] took the [[DOG]] for a walk without the [[ANT]]." ConsoleWrite($sText & @CRLF) ; Get an array of unique [[items]] $aUnique_Items = _ArrayUnique(StringRegExp($sText, "(?U)\[\[(.*)\]\]", 3)) ; Replace each item in turn using the array index as a value For $i = 1 To $aUnique_Items[0] $sText = StringReplace($sText, "[[" & $aUnique_Items[$i] & "]]", "{" & $i - 1 & "/}") Next ; New text ConsoleWrite($sText & @CRLF) Does it do the trick for you? 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...
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