Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

deleting certain item in list



  • Please log in to reply
6 replies to this topic
bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

i have a list like this:

 

apples are good

bananas are yellow

oranges are sweet

grapes are fresh

...

 

in this list sometimes i get 2 duplicate attached strings with no space in between:

 

apples are good

bananas are yellow

oranges are sweetoranges are sweet

grapes are fresh

...

 

how can i detect and delete them so i get this:?

 

apples are good

bananas are yellow

oranges are sweet

grapes are fresh

...



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

Edit: Dang it, I totally overlooked the part where you said they strings were not delimited properly. A different approach is necessary. Probably being able to split the string in half some how and seeing if the values are equal then. I'll come up with a more appropriate script.

 

Edit 2: This should work:

 

^0::
var=
(
apples are good
bananas are yellow
oranges are sweetoranges are sweet
grapes are fresh
)
newvar=

MsgBox % var
Loop, parse, var, `n
{
length:=StrLen(A_LoopField)
If Mod(length,2) ; it's odd. If it's odd it can't be a duplicated string
{
newvar.=A_LoopField "`n"
Continue
}
first:=SubStr(A_LoopField,1,length/2)
second:=SubStr(A_LoopField,length/2+1)
If (first=second)
newvar.=first "`n"
else
newvar.=A_LoopField "`n"
}
StringTrimRight, newvar, newvar, 1
MsgBox % newvar
return

 

Spoiler below works if the string you're parsing is properly delimited:

 

Spoiler


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

Not 100% reliable, but it will do for your example:



list=
(
apples are good
 
bananas are yellow
 
oranges are sweetoranges are sweet
 
grapes are fresh
)
MsgBox %   RegExReplace(list,"(\V{5,})\K\1")
return


Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

Thanks for that sinkfaze. You demonstrated the power of RegEx. I might not have used an optimal non-regex approach, but seeing how 16 lines got reduced to a single line command in effectively a 33 character function (including the function name itself), you can see the benefits of learning regex. One day I will.

 

Why would you say this script isn't 100% reliable though? Any idea if my suggested script is any more or less reliable than yours?



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011
✓  Best Answer
H =
(
apples are good
bananas are yellow
oranges are sweetoranges are sweet
grapes are fresh
)

MsgBox % RegExReplace(H, "`am)^(.*?)(?=\1$)")

or to work off of sinkfaze's pattern

MsgBox % RegExReplace(H, "`am)^(.*)\K\1$")


bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

thanks for all the solutions, specially RegExReplace ones that were awesom! ;)



sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

Yeah I didn't have much time to work on it Exaskrys, just enough to get something working.  Glad to see that Alpha Bravo is still out there to clean up my mess!

The reason that my pattern isn't 100% reliable is because it's still possible (however remote) to have two matches of five or more characters side by side on a line.  Alpha Bravo's reworking of my solution solves that problem by anchoring the side by side matches to the beginning and end of each line (and also using the multiline option, which eliminates the need to accommodate potentially matching vertical whitespace characters).