Jump to content

Array Help Needed AGAIN(Solved)


Recommended Posts

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

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

Link to comment
Share on other sites

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 by Nine
Link to comment
Share on other sites

@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 by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

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 by iamtheky

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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

  • xcaliber13 changed the title to Array Help Needed AGAIN(Solved)

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

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