Jump to content

String Insert


SmOke_N
 Share

Recommended Posts

  • Moderators

I'm trying to insert a hyphen in my string every 3rd letter or number... but every attempt I've tried for the past couple of hours has failed.

$RESULT = String("1234567890")

$CHANGE[10]
$CHANGE[1] = "A"
$CHANGE[2] = "B"
$CHANGE[3] = "C"
$CHANGE[4] = "D"
$CHANGE[5] = "E"
$CHANGE[6] = "F"
$CHANGE[7] = "G"
$CHANGE[8] = "H"
$CHANGE[9] = "I"

$OUT_VAR = ""
For $i = 1 to StringLen($RESULT)
$a = Number(StringMid($RESULT,$i,1))
$OUT_VAR = $OUT_VAR & $CHANGE[$a]
Next

This returns the desired result.. But I want to place a Hyphen every 3 places...

I could show you the 200 screw ups, but that would be a waist of space.

Thanks...

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'm trying to insert a hyphen in my string every 3rd letter or number... but every attempt I've tried for the past couple of hours has failed.

$RESULT = String("1234567890")

$CHANGE[10]
$CHANGE[1] = "A"
$CHANGE[2] = "B"
$CHANGE[3] = "C"
$CHANGE[4] = "D"
$CHANGE[5] = "E"
$CHANGE[6] = "F"
$CHANGE[7] = "G"
$CHANGE[8] = "H"
$CHANGE[9] = "I"

$OUT_VAR = ""
For $i = 1 to StringLen($RESULT)
$a = Number(StringMid($RESULT,$i,1))
$OUT_VAR = $OUT_VAR & $CHANGE[$a]
Next

This returns the desired result.. But I want to place a Hyphen every 3 places... 

I could show you the 200 screw ups, but that would be a waist of space.

Thanks...

<{POST_SNAPBACK}>

Hey Ron

I'm a new newbie so forgive the question. I'm missing something but where is the hyphen coming from?

Have a great day

Phil

Link to comment
Share on other sites

  • Moderators

Not comming from anywhere yet:

$RESULT = String("1234567890")

Dim $CHANGE[10]
$CHANGE[1] = "A"
$CHANGE[2] = "B"
$CHANGE[3] = "C"
$CHANGE[4] = "D"
$CHANGE[5] = "E"
$CHANGE[6] = "F"
$CHANGE[7] = "G"
$CHANGE[8] = "H"
$CHANGE[9] = "I"

$OUT_VAR = ""
For $i = 1 to StringLen($RESULT)
$a = Number(StringMid($RESULT,$i,1))
$OUT_VAR = $OUT_VAR & $CHANGE[$a]
Next

$NEW_OUT_VAR = ""
For $i = 1 to StringLen($OUT_VAR) Step 3
    $a = StringMid($OUT_VAR,$i ,3)
    $NEW_OUT_VAR = $NEW_OUT_VAR & $a & "-"
Next

    MsgBox(0, "", StringTrimRight($NEW_OUT_VAR, 1))

This did the desired result... guess I just had to "Give In" to get it :whistle:

Edit: Only gave my find... I put it all together.

Edit_2: Added Dim, because I'm a DIMWITT!!

Edited by ronsrules

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ron

How come when I run it I gat the following error.

D:\AutoIT\test.au3(3,12) : ERROR: syntax error

$CHANGE[10]~~~~~~~~~~~^

D:\AutoIT\test.au3 - 1 error(s), 0 warning(s)

>AU3Check Ended with Error(s).

>Running: (3.1.1.68):C:\Program Files\AutoIt3\beta\autoit3.exe "D:\AutoIT\test.au3"   

D:\AutoIT\test.au3 (3) : ==> Expected a "=" operator in assignment statement.:

$CHANGE[10]

>AutoIT3.exe ended.

>Exit code: 0    Time: 3.626


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

  • Moderators

#include <String.au3>

#include <Array.au3>

??

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#include <String.au3>

#include <Array.au3>

??

<{POST_SNAPBACK}>

Still the same result but no problem as I am just being nosey and trying to learn from others problems.


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

  • Moderators

OMG...!!

I'm going to edit the above post... but

Dim CHANGE[10]

Forgot to add the Dim for ya'll

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

I'm using StringMid in my For/Next... Why would I need to split the string if there isn't a delimeter to be recognized yet?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'm using StringMid in my For/Next... Why would I need to split the string if there isn't a delimeter to be recognized yet?

<{POST_SNAPBACK}>

$RESULT_2 = StringSplit("1234567890", "") ; string slplit "nothings"

and this is the same output also

For $i = 1 to StringLen($RESULT)

$OUT_VAR = $OUT_VAR & $CHANGE[$i]

MsgBox(0,"",$i & " " & $out_var); ;;;;;for testing

Next

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

Although I always appreciate your help... I have absolutely no idea what that does...

Are you stating that the 2 instances you gave do exactly the same thing?...

Thanks

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

how about this

$RESULT = StringSplit("1234567890", "")

Dim $CHANGE[10]
$CHANGE[1] = "A"
$CHANGE[2] = "B"
$CHANGE[3] = "C"
$CHANGE[4] = "D"
$CHANGE[5] = "E"
$CHANGE[6] = "F"
$CHANGE[7] = "G"
$CHANGE[8] = "H"
$CHANGE[9] = "I"

$OUT_VAR = ""
For $i = 1 to $RESULT[0]-1
$OUT_VAR = $OUT_VAR & $CHANGE[$i]
If StringLen($OUT_VAR) = $RESULT[3] Or StringLen($OUT_VAR) = $RESULT[7] Then
    $OUT_VAR = $OUT_VAR & "-"
EndIf
;MsgBox(0,"",$i & " " & $out_var); for testing
Next
MsgBox(0,"your desired result",$out_var); for testing
Exit

8)

NEWHeader1.png

Link to comment
Share on other sites

This will do your hyphen with the complete alphabet

$text = ""
$count = ""
For $i = 65 to 90
    $count = $count +1
    $text = $text & Chr($i)
    If $count = 3 then 
        $text = $text & "-"
        $count = ""
    EndIf
Next
MsgBox(0, "Uppercase alphabet", $text)

not sure if this helps in your direction....I hope it can

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

That's cool... but what happens if you do this:

$Random = Random(1, 9999999999, 1)
$RESULT = StringSplit($Random, "")

Dim $CHANGE[10]
$CHANGE[1] = "A"
$CHANGE[2] = "B"
$CHANGE[3] = "C"
$CHANGE[4] = "D"
$CHANGE[5] = "E"
$CHANGE[6] = "F"
$CHANGE[7] = "G"
$CHANGE[8] = "H"
$CHANGE[9] = "I"

$OUT_VAR = ""
For $i = 1 to $RESULT[0]-1
$OUT_VAR = $OUT_VAR & $CHANGE[$i]
If StringLen($OUT_VAR) = $RESULT[3] Or StringLen($OUT_VAR) = $RESULT[7] Then
    $OUT_VAR = $OUT_VAR & "-"
EndIf
;MsgBox(0,"",$i & " " & $out_var); for testing
Next
MsgBox(0,"your desired result",$out_var); for testing
Exit

It always makes A - I show up in order and the hyphens are in different places ([3] & [7])... and that's not the desired affect if you look at the Number(StringMid()), I'm assigning each digit a Letter value.

Now try mine... Remember, I wanted a hyphen every 3 places...

$Random = Random(1, 9999999999, 1)
$RESULT = String($Random)

Dim $CHANGE[10]
$CHANGE[1] = "A"
$CHANGE[2] = "B"
$CHANGE[3] = "C"
$CHANGE[4] = "D"
$CHANGE[5] = "E"
$CHANGE[6] = "F"
$CHANGE[7] = "G"
$CHANGE[8] = "H"
$CHANGE[9] = "I"

$OUT_VAR = ""
For $i = 1 to StringLen($RESULT)
$a = Number(StringMid($RESULT,$i,1))
$OUT_VAR = $OUT_VAR & $CHANGE[$a]
Next

$NEW_OUT_VAR = ""
For $i = 1 to StringLen($OUT_VAR) Step 3
    $a = StringMid($OUT_VAR,$i ,3)
    $NEW_OUT_VAR = $NEW_OUT_VAR & $a & "-"
Next

    MsgBox(0, "", StringTrimRight($NEW_OUT_VAR, 1))
Exit

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

This will do your hyphen with the complete alphabet

$text = ""
$count = ""
For $i = 65 to 90
    $count = $count +1
    $text = $text & Chr($i)
    If $count = 3 then 
        $text = $text & "-"
        $count = ""
    EndIf
Next
MsgBox(0, "Uppercase alphabet", $text)

not sure if this helps in your direction....I hope it can

8)

<{POST_SNAPBACK}>

Yep... looking at this before, would of made my headache go away. But... I still got the desired affect... Your's would make sure the Hyphen wasn't on the end... but then the StringTrimRight does that too.

Nice..

Edit: Said StringRight / Not StringTrimRight

Edited by ronsrules

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

try regexping it evry 3 symbols

use a pattern like "(*{3})"

and fetch ALL the matches in 1 big array (option 3)

then paste the array together with a dash in between each element.

just a theory.

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

This is quite an interesting-looking thread but I'm afraid I'm pretty confused. From the very first post it appears that you are trying to do two things in order:

  • Convert certain characters in a string from one to another;
  • Subsequently insert hyphens at every three locations (not overwriting any characters I think).
If that's about right, then it would probably be better to accomplish these tasks separately.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...