Jump to content

Splitting array values like stringsplit


Recommended Posts

Hello everybody,

I have a problem and I don't know how to work around it. I have a script reading names from an excel list. I want to split them whenever there's a space. Then I would like to use everything in front of the first space as $firstname and everything after the last space as $lastname.. 

$namen = StringSplit($array[$i], " ")
    $firstname = $namen[1]
    $lastname = $namen[$namen[0]]

full script

#include <Array.au3>
#include <Excel.au3>


HotKeySet("{PAUSE}", "Ende")

Func Ende()
    Exit
EndFunc   ;==>Ende

Local $oExcel = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "_Excel_Open Error", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Scriptfile.xlsx")
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Reading of Excel File Failed", "Error opening workbook" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
EndIf
Global $array = _Excel_RangeRead($oWorkbook, "Tabelle1", "A2:B704")







For $i = 0 To UBound($array) - 1
    $namen = StringSplit($array[$i], " ")
    $vorname = $namen[1]
    $nachname = $namen[$namen[0]]
    MsgBox(0, "",$vorname)
    MsgBox(0, "",$nachname)
Next

 

Link to comment
Share on other sites

Quick question, does the data look like this: space,first,space,last,space,first,space,last ... ?

I would be great to see what is in _arrayDisplay($array) if you could screen shot that ...

 

its sensitive data so I prefer not to post it... there are just letters and spaces, even though there are some ppl with more than one first name. i can think of some random names if you want though.

€: changed my mind

eUnbenannt.png

Edited by Siryx
Link to comment
Share on other sites

Try this...

For $i = 0 To UBound($array) - 1
    $namen = StringSplit($array[$i][0], " ")
    $vorname = $namen[1]
    $nachname = $namen[$namen[0]]
    MsgBox(0, "",$vorname)
    MsgBox(0, "",$nachname)
Next

OR change this line so that you only grab one column

Global $array = _Excel_RangeRead($oWorkbook, "Tabelle1", "A2:A704")

 

Edited by MuffinMan
alternatives
Link to comment
Share on other sites

#include <Array.au3>

;~ $sname= "Axel Volmer"
$sname= "Karl Jochen Wuesthof"
;~ $sname= "Karl Udo  Juentgen"
;~ $sname= "Hans Guenter Leuther"

$aName = stringsplit($sname , " " , 2)
$lastname = $aName[ubound($aName) - 1]
$firstname = _ArrayToString($aName , " " , default , ubound($aName) - 2)

msgbox(0, '' , $firstname & @CRLF & $lastname)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

one more, no split :)

;~ $sname= "Axel Volmer"
$sname= "Karl Jochen Wuesthof"

$firstname = stringregexp($sName , "\D+\s" , 3)[0]
$lastname = stringreplace($sName , $firstname , "")

msgbox (0, '' , $firstname & @CRLF & $lastname)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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