Jump to content

String Split


Recommended Posts

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
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...