Jump to content
Sign in to follow this  
Ryuji5864

Split String off into subsections/ groups

Recommended Posts

Ryuji5864

Example: 234782349 firstname lastname email

I want to take the above string and spit it off into different sections. A section for the number, firstname, lastname, email. How would I go about doing that?

Share this post


Link to post
Share on other sites
Ryuji5864

Use the StringSplit() Function.

Just found that function >.> Sorry for the post.

Share this post


Link to post
Share on other sites
John117

Example: 234782349 firstname lastname email

I want to take the above string and spit it off into different sections. A section for the number, firstname, lastname, email. How would I go about doing that?

#Include <Array.au3>
$sString =  "234782349 firstname lastname email"
$OneSpace = " " ;If more than 1 space apart, add more spaces here to match.
See helpfile.
$array = StringSplit($sString, $OneSpace)
_ArrayDisplay($array)

Share this post


Link to post
Share on other sites
John117

Whats wrong with reading the help file first

StringSplit ( "string", "delimiters" [, flag ] )

What's wrong with being nicer about it. Especially with newer members . . .

^_^

Share this post


Link to post
Share on other sites
Ryuji5864

What's wrong with being nicer about it. Especially with newer members . . .

^_^

What if the amount of spacing is unknown?

Share this post


Link to post
Share on other sites
Authenticity

Then you can use StringRegExpReplace and something like:

#Include <Array.au3>
$sString =  "234782349      firstname              lastname      email" & @CRLF
$sString &=  "234782349 firstname          lastname    email"

$sString = StringRegExpReplace($sString, '\h+', ' ')
$aLines = StringSplit($sString, @CRLF, 1)

For $i = 1 To $aLines[0]
    $array = StringSplit($aLines[$i], ' ')
    _ArrayDisplay($array)
Next

Share this post


Link to post
Share on other sites
Ryuji5864

Then you can use StringRegExpReplace and something like:

#Include <Array.au3>
$sString =  "234782349      firstname              lastname      email" & @CRLF
$sString &=  "234782349 firstname          lastname    email"

$sString = StringRegExpReplace($sString, '\h+', ' ')
$aLines = StringSplit($sString, @CRLF, 1)

For $i = 1 To $aLines[0]
    $array = StringSplit($aLines[$i], ' ')
    _ArrayDisplay($array)
Next
Thanks a ton!! You were the biggest help. I have one more question for you. Is it possible to change the name of the row from [0] to anything else?

Share this post


Link to post
Share on other sites
Authenticity

Mind to explain some more? Do you mean, changing $aLines[0] to something else? If so, you'll need first to check if the new something else is not exceeding the last array's subscript.

Share this post


Link to post
Share on other sites
Ryuji5864

Mind to explain some more? Do you mean, changing $aLines[0] to something else? If so, you'll need first to check if the new something else is not exceeding the last array's subscript.

From This

[0]|General information

[1]|234890934867

[2]|laskdfjnkjn1

[3]|Doe

[4]|John

[5]|jdoe@somethinganother.com

To This

|General information

# |234890934867

ID |laskdfjnkjn1

Lastname|Doe

Firstname|John

Email |jdoe@somethinganother.com

If this is not possible, I could just throw it through a 2D array

Share this post


Link to post
Share on other sites
Authenticity

Use Enum. You can give a subscript index element a meaningful name so it's index is self-explaining:

Enum $GenInfo, $Num, $ID, $Lastname, $Firstname, $Email, $UpperBound

; ...
For $i = 1 To $aLines[0]
    $array = StringSplit($aLines[$i], ' ')
    If UBound($array) = $UpperBound Then
         ConsoleWrite('General Information: ' & $array[$GenInfo] & @LF)
         ConsoleWrite('Member number: ' & $array[$Num] & @LF)
         ;....
    EndIf
Next

Or as you mentioned, it's possible to implement it using a 2D array but the enumeration method is preferred in my opinion.

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  

×