Sign in to follow this  
Followers 0
ravenfyre

Q1) How to merge strings, each a part of the whole

5 posts in this topic

#1 ·  Posted (edited)

Hey fellas, I got a couple questions. Hopefully someone can help me out a bit :">

Question #1:

How would I go about merging several strings? Normally the answer would be easy, but what if each of these strings is only a part of the whole and if one part of it already exists in another it should be overwritten. One could start at the beginning and cut off in the middle, another in the middle cut off at the end and one could just be the middle of the whole string. Take the following as an example.

$string1 = "1,2,3"

$string2 = "3,4,5"

$string3 = "2,3,4,5"

$string4 = "5,6,7"

therefore, the complete string is: "1,2,3,4,5,6,7"

The numbers in the string are ALWAYS in the same order, they just start and end at different places, therefore I need to merge them to get the complete string. I am puzzled at how to go about doing this :ph34r:.

Question #2

I thought I might aswell stick in another quicky question. Is there any other way than using "return" to exit a function, so the script will continue? Is using "return" the only method? I know it will work but just wanted to know if there is another way.

Edited by ravenfyre

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

A2: EndFunc :">

Edited by ezzetabi

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

$STRING1 = "1,2,3"
$STRING2 = "3,4,5"
$STRING3 = "2,3,4,5"
$STRING4 = "5,6,7"

$TOTAL = $STRING1
$TOTAL = Merge($TOTAL, $STRING2)
$TOTAL = Merge($TOTAL, $STRING3)
$TOTAL = Merge($TOTAL, $STRING4)

MsgBox(0, 'test', $TOTAL)

Func Merge($TSTRING, $ISTRING)
   $START = StringInStr($TSTRING, StringLeft($ISTRING, 1)) - 1
   If $START = -1 Then $START = StringLen($TSTRING)
   $TSTRING = StringLeft($TSTRING, $START) & $ISTRING
   Return $TSTRING  
EndFunc  ;==>Merge

A "Func" ends with "Return" or when it reaches the "EndFunc" statement...

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

A1) You should use stringsplit to split up the items by a comma and then parse each one to see if it has been used already.

$var1 = "5,2,4,7,9"
$var2 = "7,8,6,5,2"
$var3 = "9,6,5,8,3"
$str = $var1 & "," & $var2 $ "," & $var3
$arr = StringSplit($str, ",")
$newstr = ""
for $i = 0 to $arr[0]
   if not StringInStr($newstr, $arr[$i]) Then
      $newstr = $newstr & "," & $arr[$i]
   EndIf
next

EDIT: I see I have been preceeded.

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

Ah, good info, thanks guys.

A2: EndFunc  :">

<{POST_SNAPBACK}>

Doh! Didn't even consider using EndFunc. All those endless hours starring at my monitor trying to finish my script has fried my brain lol :ph34r:

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