Sign in to follow this  
Followers 0
leuce

How to add only unique items to array

5 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 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.  

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

leuce,

This is my version of how to do the various replacements: :oops:

#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? :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0