Siryx Posted July 17, 2015 Posted July 17, 2015 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
Jfish Posted July 17, 2015 Posted July 17, 2015 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 ... Siryx 1 Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt
Siryx Posted July 17, 2015 Author Posted July 17, 2015 (edited) 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 Edited July 17, 2015 by Siryx
MuffinMan Posted July 17, 2015 Posted July 17, 2015 (edited) 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) NextOR change this line so that you only grab one columnGlobal $array = _Excel_RangeRead($oWorkbook, "Tabelle1", "A2:A704") Edited July 17, 2015 by MuffinMan alternatives Siryx 1
iamtheky Posted July 17, 2015 Posted July 17, 2015 #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) Siryx 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted July 17, 2015 Posted July 17, 2015 Simple logic$sname= "Karl Jochen Wuesthof" $aName = stringsplit($sname , " ") $lastname = $aName[$aName[0]] $firstname = StringReplace($sname, " " & $lastname, "") msgbox(0, '' , $firstname & @CRLF & $lastname) Siryx 1
iamtheky Posted July 17, 2015 Posted July 17, 2015 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) Siryx 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted July 17, 2015 Posted July 17, 2015 This one never errors;$sname= "" ;$sname= "Nameonly " $sname= "Karl Jochen Wuesthof" $lastname = stringregexp($sName , "(\w*)\s*$" , 3)[0] $firstname = stringreplace($sName , $lastname , "") msgbox (0, '' , $firstname & @CRLF & $lastname) Siryx 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now