Sign in to follow this  
Followers 0
Klexen

Removing elements from array depending on condition

10 posts in this topic

#1 ·  Posted (edited)

I'm trying to get an array from a string based off whether or not there is a space in the word...

For example...

If the user enters the word "student" in the input box, the array should output:

array[0] = s

array[1] = t

array[2] =u

array[3] =d

array[4] =e

array[5] =n

array[6] =t

This works fine.

When a space is found in the array, I want the element in the array that contains the space to be replaced by the string that was made from the letters that are between the two spaces. (I got this far) However, my issue is deleting the elements in the array that the string was made from.

This is my desired array output.

If the user enters the word "st ud en t" in the input box, the array should output:

array[0] = st

array[1] = ud

array[2] = en

array[3] = t

Here's my code:

#include <String.au3>
#include <Array.au3>

_Main()

Global $mainArray, $wordToDisplay, $output, $count

Func _Main()

$wordToDisplay = InputBox("Enter word", "Enter your word", "")

$array = StringSplit($wordToDisplay, '', 0)
_ArrayDelete($array, 0)

For $r = 0 To UBound($array, 1) - 1
If $array[$r] = " " Then
Local $tempArray = _StringBetween($wordToDisplay, " ", " ")
$array[$r] = _ArrayToString($tempArray)
EndIf
Next

_ArrayDisplay($array, 'Letters in word')

EndFunc   ;==>_Main

Any help would be much appreciated. Thanks!

Edited by Klexen

Share this post


Link to post
Share on other sites



If you just want to remove spaces from user input you could use StringStripWS.

You can set a flag to strip leading spaces, trailing spaces, double (or more) spaces between words or all spaces.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Klexen,

Just check if there are spaces in the string and then either StringSplit on a space or not: :oops:

#include <Array.au3>

Global $aList[2] = ["student", "st ud en t"]

For $i = 0 To UBound($aList) - 1

    If StringInStr($aList[$i], " ") Then
        ; StringSplit on spaces
        $aResult = StringSplit($aList[$i], " ")
    Else
        ; StringSplit every letter
        $aResult = StringSplit($aList[$i], "")
    EndIf

    _ArrayDisplay($aResult)

Next

All clear? :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

Klexen,

Just check if there are spaces in the string and then either StringSplit on a space or not: :rip:

#include <Array.au3>

Global $aList[2] = ["student", "st ud en t"]

For $i = 0 To UBound($aList) - 1

    If StringInStr($aList[$i], " ") Then
        ; StringSplit on spaces
        $aResult = StringSplit($aList[$i], " ")
    Else
        ; StringSplit every letter
        $aResult = StringSplit($aList[$i], "")
    EndIf

    _ArrayDisplay($aResult)

Next

All clear? :D

M23

Ah! Much more elegant. Thank you so much. Works perfectly. :oops:

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Okay, I realized this doesn't do what it should do...

If I have a word like...

washer... And I wanted the output to be:

array[0] = w

array[1] = a

array[2] = sh

array[3] = e

array[4] = r

I couldn't do that, because creating a space to separate the "sh" also separates the other letters. "wa sh er"

So I was thinking maybe I could do...

"wa_sh_er" ...Then I could make a condition that says: if there are letters between two underscores, make those letters one element in the array, else make each character an element.

However, if I had a word like: "chicken" ... and I only wanted the ch and ck to count as one element "_ch_i_ck_en", adding the underscores to do that would create an unintentional block of letter(s) that would not be separated individually. In this example, it doesn't really matter, because there is only one letter between ch and ck. But if there was more letters, it wouldn't work.

Desired array output:

array[0] = ch

array[1] = i

array[2] = ck

array[3] = e

array[4] = n

Do any of you have an idea on how I could make it so I could type out a word, and tell the script to do stuff like this?

Example word: lovingly

Typed: lov_ing_ly_

Desired array output:

array[0] = l

array[1] = o

array[2] = v

array[3] = ing

array[4] = ly

I hope this makes sense! Thanks guys!

Edited by Klexen

Share this post


Link to post
Share on other sites

Klexen,

I hope this makes sense!

What you want to do makes perfect sense - why you want to do it is a complete mystery! :rip:

If you put an underscore before AND after the section you want to keep intact you can do it this way: :oops:

#include <Array.au3>

$sString = "lov_ing__ly_"
$aArray = _SplitString($sString)
_ArrayDisplay($aArray)

$sString = "wa_sh_er"
$aArray = _SplitString($sString)
_ArrayDisplay($aArray)

Func _SplitString($sString)

    ; Create the array
    Local $aArray[StringLen($sString)] = [0]

    For $i = 1 To StringLen($sString)
        ; look at each char in turn
        $sChar = StringMid($sString, $i, 1)
        Switch $sChar
            ; We find an underscore
            Case "_"
                $sChars = ""
                $j = $i + 1
                ; So we look for the next one
                While 1
                    $sChar = StringMid($sString, $j, 1)
                    Switch $sChar
                        ; When we find it we add the string of chars to the array
                        Case "_"
                            $aArray[0] += 1
                            $aArray[$aArray[0]] = $sChars
                            ; And now we restart the external loop
                            $i = $j
                            ExitLoop
                        ; Until we find a second underscore we add the char to the string 
                        Case Else
                            $sChars &= $sChar
                    EndSwitch
                    $j += 1
                WEnd
            ; We do not find an underscore
            Case Else
                ; We add it to the array
                $aArray[0] += 1
                $aArray[$aArray[0]] = $sChar
        EndSwitch

    Next

    ; We resize the array to fit the data
    ReDim $aArray[$aArray[0] + 1]
    ; We remove the count
    _ArrayDelete($aArray, 0)
    ; And we send it back
    Return $aArray

EndFunc

Does that do what you want? :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

Klexen,

What you want to do makes perfect sense - why you want to do it is a complete mystery! :rip:

If you put an underscore before AND after the section you want to keep intact you can do it this way: :oops:

The why:

I'm developing an iPad application for tutors of a particular system that teaches people with Dyslexia how to read and spell. In the application, depending on the Book Level / Lesson Level you're on, prebuilt words are displayed as letter tiles on the screen. This saves tutors the time of actually building the words manually using physical tiles. (This is currently what they need to do to teach this system.) The letter tiles are broken down into consonants, vowels, digraphs, units, prefixes, suffixes, vowel teams... etc.

Screenshot: http://i.imgur.com/rWY55.png

There are a couple tutors of this system that are going through all the Book Levels and Lessons, to get me a list of words that they need to be prebuilt automatically. With this script, the tutors will be able to generate the code I need for the iPad application for each word. Here is a sample of the code the script generates, that can pasted directly into my project.

//chicken
NSArray *bookFourLessonEightWordOne = [NSArray arrayWithObjects:[[LetterTileStore defaulTileStore] letterTileForKey:@"ch"],[[LetterTileStore defaulTileStore] letterTileForKey:@"i"],[[LetterTileStore defaulTileStore] letterTileForKey:@"ck"],[[LetterTileStore defaulTileStore] letterTileForKey:@"e"],[[LetterTileStore defaulTileStore] letterTileForKey:@"n"], nil];

Does that do what you want? :D

M23

It does EXACTLY what I want. You're awesome man. Thank you so much for your generous help!

Share this post


Link to post
Share on other sites

Klexen,

I am delighted I could help with such a project. It makes a pleasant change from the usual stuff we get here. :D

If I can ever be of any help in the future with this work, please do not hesitate to let me know. Start a forum topic as usual and if you do not get a satisfactory solution within a day, feel free to send a PM. :oops:

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

Klexen,

I am delighted I could help with such a project. It makes a pleasant change from the usual stuff we get here. :D

If I can ever be of any help in the future with this work, please do not hesitate to let me know. Start a forum topic as usual and if you do not get a satisfactory solution within a day, feel free to send a PM. :rip:

M23

M23,

It's cool to see when people have a respect for this type of work. :oops:

I really appreciate your offer man. I'll definitely let you know if I need any further help.

Thanks again!

Share this post


Link to post
Share on other sites

Albeit a bit late, here's another way to achieve the result:

#include <Array.au3>
$sString = "lov_ing__ly_"
$aArray = _SplitString($sString)
_ArrayDisplay($aArray)
$sString = "wa_sh_er"
$aArray = _SplitString($sString)
_ArrayDisplay($aArray)
Func _SplitString($sString)
Return(StringRegExp($sString, "(?|([[:alpha:]])|_([[:alpha:]]+)_)", 3))
EndFunc

I claim it's simpler but others could think otherwise Posted Image


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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