Sign in to follow this  
Followers 0
FinalVersion

String Split

5 posts in this topic

I've read the documentation and it didn't really help me that much. Any basically say if I have..

fistname:joe
lastname:smith

firstname:john
lastname:howard

I know how to get the information from a textfile, the hard part it splitting it up. Basically I'd like it in an array ( if possible ) like $name[1] would containt joe smith and $name[2] would contain john howard, anyway thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You need something like:

#include <Array.au3>
#include <File.au3>

Global $asInFile
Global $asOutArray[1] = [0]
Global $iOutCount = 0

_FileReadToArray("C:\temp\test.txt",$asInFile)

For $i = 1 To $asInFile[0]
    $asSplit = StringSplit($asInFile[$i],":")
    If @error <> 1 Then
        If $asSplit[1] = "firstname" Then 
            ReDim $asOutArray[UBound($asOutArray)+1]
            $iOutCount = $iOutCount + 1
            $asOutArray[$iOutCount] = $asSplit[2]
        EndIf
        If $asSplit[1] = "lastname" Then 
            $asOutArray[$iOutCount] = $asOutArray[$iOutCount] & " " & $asSplit[2]
        EndIf
    EndIf
Next
$asOutArray[0] = UBound($asOutArray)-1
_ArrayDisplay($asOutArray)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You need to show a sample of the format because otherwise it can be in any form such as lastname comes before firstname or there is something between them and what not.

#include <Array.au3>

Local $avMatch
Local $sText = "firstname:joe" & @CRLF & _
               "lastname:smith" & @CRLF & @CRLF & _
               "firstname:john" & @CRLF & _
               "lastname:howard"


$avMatch = StringRegExp($sText, "(?i)firstname:([^\r\n]*)\r\nlastname:([^\r\n]*)", 3)
If IsArray($avMatch) Then
    Local $avNames[UBound($avMatch)/2][2]
    
    For $i = 0 To (UBound($avMatch)/2) Step 2
        $avNames[$i/2][0] = $avMatch[$i]
        $avNames[$i/2][1] = $avMatch[$i+1]
    Next
    _ArrayDisplay($avNames)
EndIf

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