xcaliber13 Posted November 16, 2018 Share Posted November 16, 2018 Can someone point me in the right direction. I have an array. Column 29 in the array is LastName, FirstName MiddleInitial I want to split that column in three separate columns. LastName FirstName MiddleInitial So here is the problem with how I am trying to do this. Not every row has data. Some rows LastName, FirstName, MiddleInitial is empty. Some rows are just missing MiddleInitial. So am I correct in saying that is why my script is giving this error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: $avArray[$i][18] = $aNames[3] $avArray[$i][18] = ^ ERROR _ArrayDisplay is showing that some of the splits have up to 4 rows, while the empty rows of course have no rows _ArrayDisplay($aNames). How would I fix this? For $i = 0 To UBound($avArray) - 1 $aNames = StringSplit($avArray[$i][29], ","" ") _ArrayDisplay($aNames) $avArray[$i][17] = $aNames[1] $avArray[$i][18] = $aNames[3] $avArray[$i][19] = $aNames[4] Next Thank you Link to comment Share on other sites More sharing options...
iamtheky Posted November 16, 2018 Share Posted November 16, 2018 many ways to sanitize your input, but you need to show what the source array looks like (or at least a few examples from col 29). How would you know which name is which if it can have a varied number of comma delimited fields? what are the rules? ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Nine Posted November 16, 2018 Share Posted November 16, 2018 $aNames[0] contains the number of strings returned, you should use it to ensure you do not exceed range. And what happened to $aNames[2] ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
dmob Posted November 16, 2018 Share Posted November 16, 2018 StringSplit returns no of returned elements in $aNames[0]. Check that value before operating on $aNames[1] to $aNames[3] Link to comment Share on other sites More sharing options...
xcaliber13 Posted November 16, 2018 Author Share Posted November 16, 2018 iamtheky thank you for the response. Here is an example of col 29 Smith, John BLANK or empty cell Jones, Dave L Smith, Nancy J Jones, Jane BLANK or empty cell Column 29 will never vary from this format Nine $aNames[2] is always empty from the split. Link to comment Share on other sites More sharing options...
Nine Posted November 16, 2018 Share Posted November 16, 2018 (edited) no chance [2] will be empty based on the example you gave. It should contain first name and mid initial. unless the cell is empty ofc wait your stringsplit looks strange: should that be ", " instead of ","" " Edited November 16, 2018 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted November 16, 2018 Share Posted November 16, 2018 (edited) @xcaliber13 First of all, try to change this $aNames = StringSplit($avArray[$i][29], ","" ") to this $aNames = StringSplit($avArray[$i][29], ",") By the way, since you could have blank lines in your source array, you should check for those, because when you split your blank string, the function doesn't return an array, so you can't access something that doesn't exist, and then the error is thrown Edited November 16, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
iamtheky Posted November 16, 2018 Share Posted November 16, 2018 (edited) just put some checks on the ubound, if the format will always be the same. you just dont want to attempt to access array elements that dont exist. ;~ $aNames = StringSplit('Jones, Dave L', ","" ") $aNames = StringSplit('Jones, Dave', ","" ") ;~ $aNames = StringSplit('', ","" ") If $aNames[0] = 4 Then msgbox(0, '' , $aNames[1] & @LF & $aNames[3] & @LF & $aNames[4]) ElseIf $aNames[0] = 3 Then msgbox(0, '' , $aNames[1] & @LF & $aNames[3]) EndIf Edited November 16, 2018 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Nine Posted November 16, 2018 Share Posted November 16, 2018 ok I get it why [2] is empty cause there is space after the comma... Like you said, if it is always the same format remove the space split and work with the first name and mid initial separately from stringsplit, imo “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
mikell Posted November 16, 2018 Share Posted November 16, 2018 You might extract this column #29 to a 1D array, then parse it and build a 2D array with 3 columns Something like this #Include <Array.au3> Local $a[6] = ["Smith, John", "", "Jones, Dave L", "Smith, Nancy J", " ", "Jones, Jane"] _ArrayDisplay($a) Local $b[6][3] For $i = 0 to UBound($a)-1 $tmp = StringRegExp($a[$i], '[^\s,]+', 3) For $k = 0 to UBound($tmp)-1 $b[$i][$k] = $tmp[$k] Next Next _ArrayDisplay($b) FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
Malkey Posted November 16, 2018 Share Posted November 16, 2018 Try this. #include <Array.au3> Local $avArray[9][32] = [["Smith, John"], [""], ["Jones, Dave L"], ["Jones"], [", Dave"], [",, L"], ["Smith"], ["Jones, Jane"], [" "]] For $i = 0 To UBound($avArray) - 1 ;$aNames = StringSplit($avArray[$i][29], ","" ") $aNames = StringRegExp($avArray[$i][0], "^(\w*)(?:\h*,?\h*)(\w*)(?:\h*,?\h*)(\w*)$", 3) _ArrayDisplay($aNames) $avArray[$i][17] = $aNames[0] $avArray[$i][18] = $aNames[1] $avArray[$i][19] = $aNames[2] Next _ArrayDisplay($avArray) Link to comment Share on other sites More sharing options...
Aether Posted November 16, 2018 Share Posted November 16, 2018 man, how do I hate regexp ? It is simply incomprehensible. They shouldn't have made language like Lisp or Perl. Link to comment Share on other sites More sharing options...
xcaliber13 Posted December 11, 2018 Author Share Posted December 11, 2018 Mikell, Sorry for the long delay on getting back to this. I used your script and I was getting an error. And then I was out of work for a little while. Eye surgery. But anyways I figured out why I was getting an error. Wouldn't you know it one name in the list was not like all of the others. Most were Lastname, FirstName, MiddleInt but this one was Lastname, FirstName, MiddleInt, III Once I figured this out I was able to adjust the script to account for the extra column and then just combine columns so that I would get just the three columns needed. Thank you to all who helped! Link to comment Share on other sites More sharing options...
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