Jump to content

splitting a string


Recommended Posts

I am trying to split a 12 character string into a array with earch array containing 3 letters

here's my code:

func _ss($s,$n)
    dim $return[1]
    if IsInt(Stringlen($s)/$n) then
        redim $return[Stringlen($s)/$n + 1]
        $return[0] = Stringlen($s)/$n
    Else
        MsgBox(0,IsInt(Stringlen($s)/$n),"hi")
        redim $return[int(Stringlen($s)/$n) + 2]
        $return[0] = int(Stringlen($s)/$n) + 1
    EndIf
    msgbox(0,"",Stringlen($s)/$n)
    $c = 0
    $num = 1
    MsgBox(0,"",stringlen($s))
    for $i =  1 to Stringlen($s)
        $c = $c + 1
        if IsInt($i/$n) and $i <> Stringlen($s) then
            $num = $num + 1
            ConsoleWrite($num & @lf)
            $return[$num] = $return[$num] & StringMid($s,$i,1)
            $c = 0
        Else
            $return[$num] = $return[$num] & StringMid($s,$i,1)
        EndIf
    Next
    return $return
EndFunc

This is what it does

say you have this string

"123456789123"

and you split it into and array with each array having 3 characters

$a[0] would equal 4 like expected

$a[1] would only have 2 characters "12"

$a[2] and $a[3] would have 3 characters like expected

$a[4] would have 4 letters

Can anybody help me please?

Thanks in advanced.

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

  • Moderators

Can you show what:

$a[0]

$a[1]

$a[2]

$a[3]

$a[4]

Literally would contain? Because it seems you are jumping around to me.

Edit:

This is how I understand you:

#include <array.au3>
$sString = "123456789123"
$a = StringRegExp($sString, "(..)(...)(...)(....)", 3)
_ArrayDisplay($a)
Edited by SmOke_N

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

$a[0] = "4"

$a[1] = "12"

$a[2] = "345"

$a[3] = "678"

$a[4] = "9123"

array 1 and array 4 are messed up

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

  • Moderators

$a[0] = "4"

$a[1] = "12"

$a[2] = "345"

$a[3] = "678"

$a[4] = "9123"

array 1 and array 4 are messed up

#include <array.au3>
$sString = "123456789123"
$a = _myStringFunc($sString)
_ArrayDisplay($a)

Func _myStringFunc($sString, $sPattern = "(..)(...)(...)(....)")
    Local $aSRE = StringRegExp($sString, $sPattern, 3)
    Local $nUBound = UBound($aSRE), $avArray[$nUBound + 1], $iCC
    $avArray[0] = $nUBound
    For $iCC = 0 To $nUBound - 1
        $avArray[$iCC + 1] = $aSRE[$iCC]
    Next
    Return $avArray
EndFunc

Edit: Added #include

Edited by SmOke_N

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

that code does the exact same thing as SS which is not that i want

i want

$a[0] = 4

$a[1] = 123

$a[2] = 456

$a[3] = 789

$a[4] = 123

and if the string was 123456789123456789123(21 characters long) I would want it to do

$a[0] = 7

$a[1] = 123

$a[2] = 456

$a[3] = 789

$a[4] = 123

$a[5] = 456

$a[6] = 789

$a[7] = 123

[size="10"]Pure Au3 crypt funcs(I'm currently also working on making a dll from this)[/size][Y] Be more active in the community[Y] Get 200 posts[N] Get 300 posts[N] Make a Topic in the example scripts forum with at least 50 replies.People who currently hate me:ValikSmOke_N

Link to comment
Share on other sites

  • Moderators

$a[0] = "4"

$a[1] = "12"

$a[2] = "345"

$a[3] = "678"

$a[4] = "9123"

array 1 and array 4 are messed up

that code does the exact same thing as SS which is not that i want

i want

$a[0] = 4

$a[1] = 123

$a[2] = 456

$a[3] = 789

$a[4] = 123

and if the string was 123456789123456789123(21 characters long) I would want it to do

$a[0] = 7

$a[1] = 123

$a[2] = 456

$a[3] = 789

$a[4] = 123

$a[5] = 456

$a[6] = 789

$a[7] = 123

look at the output you told me you wanted, mine outputs exactly what you said.

I asked what you wanted it to look like... Easily remedied though:

$sString = "123456789123"
$a = _myStringFunc($sString)
_ArrayDisplay($a)

Func _myStringFunc($sString, $sPattern = "(...)")
    Local $aSRE = StringRegExp($sString, $sPattern, 3)
    Local $nUBound = UBound($aSRE), $avArray[$nUBound + 1], $iCC
    $avArray[0] = $nUBound
    For $iCC = 0 To $nUBound - 1
        $avArray[$iCC + 1] = $aSRE[$iCC]
    Next
    Return $avArray
EndFunc

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

Stringchop created by Siao

#include <Array.au3>

_ArrayDisplay(_StringChop("123456789123456789123",3))

Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFunc

http://www.autoitscript.com/forum/index.ph...;hl=_StringChop

Edited by Paulie
Link to comment
Share on other sites

  • Moderators

Stringchop created by Siao

#include <Array.au3>

_ArrayDisplay(_StringChop("123456789123456789123",3))

Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFuncoÝ÷ Úêå
:)

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

Lol @ Smoke_N

apparently, some people just don't accept responses containing a StringRegExp() :);)

Yeah, those are the ones that will always continue to defend DOS as the only proper OS 20 years from now as well.

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

  • 6 years later...
  • Moderators

fxg4758, please don't necro old posts, especially after 6 years! The language has changed a lot since 2008. If you have a question, please post a new topic, include a detailed description of what you want to do, and what you have tried for yourself thus far. I guarantee you'll get much faster help.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

fxg4758, please don't necro old posts, especially after 6 years! The language has changed a lot since 2008. If you have a question, please post a new topic, include a detailed description of what you want to do, and what you have tried for yourself thus far. I guarantee you'll get much faster help.

Thanks. I create a new post. Regards

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...