Sign in to follow this  
Followers 0
tsolrm

remove element from array

18 posts in this topic

#1 ·  Posted (edited)

Hi, i've got a list in the form of:

nick 20

nick2 20

nick3 20

nick25 20

etc

It's a very large list. I need to delete a certain entry, say "nick2". I need this to be done as quick as possible.

This is what i've come up with. As far a i can see it will do the job but is there an easier way to do this?

Thanks

$Bans = FileRead ("Statistics.txt")
$Bans = StringSplit ($Bans, @LF)
$Bans = _ArrayToString ($Bans, "*")
$Bans = StringReplace ($Bans, "*nick2 ", "*")
$Bans = StringSplit ($Bans, "*", 2)
$Bans[0]=UBound ($Bans)-1
For $i = 1 to $Bans [0]
$Return = StringIsDigit (StringStripCR($Bans[$i]))
If $Return = 1 Then $Bans [$i] = ""
Next
$Bans = _ArrayToString ($Bans, "*")
$Bans = StringReplace ($Bans, "**", "*")
$Bans = StringSplit ($Bans, "*", 2)
$Bans[0]=UBound ($Bans)-1

_ArrayDisplay ($Bans)
Edited by tsolrm

Share this post


Link to post
Share on other sites



Is it always "name-to-be-deleted 20" or can the rest of the record change?

How big is your file (MB, GB)?


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

tsolrm,

I would do it like this: :)

#include <Array.au3>
#include <File.au3>

; Declare initial variables
Global $aBans
$sFile = "Statistics.txt"
$sDelete = "nick2"

; Read file
_FileReadToArray($sFile, $aBans)

; Just to show what you read
_ArrayDisplay($aBans)

; Find the offending name - note you need to use the partial search parameter
$iIndex = _ArraySearch($aBans, $sDelete, 1, 0, 0, 1)

; And if found - delete
If Not @error Then
    _ArrayDelete($aBans, $iIndex)
EndIf

; Just to show you it has been deleted
_ArrayDisplay($aBans)

; And rewrite the file - note how to avoid the count in the [0] element
_FileWriteFromArray($sFile, $aBans, 1)

All clear? ;)

M23

1 person likes this

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

Is it always "name-to-be-deleted 20" or can the rest of the record change?

How big is your file (MB, GB)?

its always in the form "nick number"

Share this post


Link to post
Share on other sites

As speed matters I would avoid the _Array* functions.

If the size of the file is < 2GB (I think this is the limit for a string, but I will check) you can read the whole file to a variable and do a stringreplace and then rewrite the file.


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

its always in the form "nick number"

Does this mean the number changes or is it always "20"?

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

tsolrm,

I would do it like this: :)

#include <Array.au3>
#include <File.au3>

; Declare initial variables
Global $aBans
$sFile = "Statistics.txt"
$sDelete = "nick2"

; Read file
_FileReadToArray($sFile, $aBans)

; Just to show what you read
_ArrayDisplay($aBans)

; Find the offending name - note you need to use the partial search parameter
$iIndex = _ArraySearch($aBans, $sDelete, 1, 0, 0, 1)

; And if found - delete
If Not @error Then
    _ArrayDelete($aBans, $iIndex)
EndIf

; Just to show you it has been deleted
_ArrayDisplay($aBans)

; And rewrite the file - note how to avoid the count in the [0] element
_FileWriteFromArray($sFile, $aBans, 1)

All clear? ;)

M23

clear indeed. Is _ArraySearch quicker then doing what i did?

Share this post


Link to post
Share on other sites

Does this mean the number changes or is it always "20"?

Yes it changes

Share this post


Link to post
Share on other sites

I would suggest to test Melba's code with your data. If you are satisfied with the speed stay with this solution. If not I'm sure speed can be enhanced.


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

Thank you guys

Share this post


Link to post
Share on other sites

There is a possibility that there would be two "nick2" or more.

And I want to delete all of them. My code accounts for this but ArraySearch stops the search as soon as it finds the first nick2 doesn't it?

Share this post


Link to post
Share on other sites

tsolrm,

Put the _ArraySearch/Delete section in a loop until the search returns an error. :)

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

Right. You would have to do _ArraySearch until you get -1 as return value.

Make sure to set parameter $iStart to the current record (= the return value of the previous _ArraySearch call)


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

Partial search wouldn't work properly here because if you are searching for "nick" for example then all of the elements in this case have "nick".

In reality one nickname could be contained within another.

Share this post


Link to post
Share on other sites

If you are familiar with regular expressions you can read the file into a string, replace all occurences of "nick2 number@CRLF" using StringRegExpReplace and then write the file to disk.

1 person likes this

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

tsolrm,

Then please give us a valid set of data to work with. We cannot be expected to offer sensible help if you keep altering your requriements. :)

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

tsolrm,

Then please give us a valid set of data to work with. We cannot be expected to offer sensible help if you keep altering your requriements. :)

M23

Well I suppose if I do a partial search for "nick " (with a space) than this will only find the entries that contain "nick 20".

Share this post


Link to post
Share on other sites

If you'll be doing multiple deletes, then using the _Array functions wouldn't be the way to go.

As you'll pass through your data multiple times and execute multiple Redims.

Maybe do it manually like here:

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