Jump to content
Sign in to follow this  
Siryx

Splitting array values like stringsplit

Recommended Posts

Siryx

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

 

Share this post


Link to post
Share on other sites
Jfish

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

 

  • Like 1

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
Siryx

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

Share this post


Link to post
Share on other sites
MuffinMan

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
  • Like 1

Share this post


Link to post
Share on other sites
iamtheky
#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)

 

  • Like 1

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

Share this post


Link to post
Share on other sites
mikell

Simple logic

$sname= "Karl Jochen Wuesthof"

$aName = stringsplit($sname , " ")
$lastname = $aName[$aName[0]]
$firstname = StringReplace($sname, " " & $lastname, "")

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

 

  • Like 1

Share this post


Link to post
Share on other sites
iamtheky

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)

 

  • Like 1

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

Share this post


Link to post
Share on other sites
mikell

:D
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)

 

  • Like 1

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  

×