Jump to content

Size of an array


Recommended Posts

This may be a stupid quesiton, but I couldn't find this in the helpfile or searching the forums. How do I determine the length of an array. Basically I want to split up a string to determine the first, middle, and last names. However sometimes there may not be a middle name. This way the size of array temp would change. Any ideas?

$FullName = "First Middle Last"

$temp = StringSplit($FullName," ")

$FirstName = $temp[1]

$MiddleName = $temp[2]

$LastName = $temp[3]

This would break if $FullName = "First Last"

Any help would be greatly appreciated. Thanks.

-- If the apocalypse comes... beep me.

Link to comment
Share on other sites

Directly from the helpfile :

the first element ($array[0]) contains the number of strings returned

You should also check out Ubound as well, which you most likely will find useful

at a later time. When using Ubound though, you must note this :

Remember that the value returned by UBound is one greater than the index of

an array'slast element!

Edit : Test-script :
$array = StringSplit("Well|How|Yes|Remote|Cool", "|")
MsgBox(64, "", "$array[0] says : " & $array[0])
MsgBox(64, "", "Ubound says : " & UBound($array))
Edited by Helge
Link to comment
Share on other sites

$name = InputBox ("name", "enter your first and last (and maybe middle) name.")
$array = StringSplit ($name, " ")
$num = $array[0]
If $num = 2 Then
    $first = $array[1]
    $middle = ""
    $last = $array[2]
Else
    $first = $array[1]
    $middle = $array[2]
    $last = $array[3]
EndIf

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

Directly from the helpfile :

You should also check out Ubound as well, which you most likely will find useful

at a later time. When using Ubound though, you must note this :

Edit : Test-script :

$array = StringSplit("Well|How|Yes|Remote|Cool", "|")
MsgBox(64, "", "$array[0] says : " & $array[0])
MsgBox(64, "", "Ubound says : " & UBound($array))

Duh. Thanks, of course the first element contains the number of elements. I'll also have to check out UBound for later.

Thanks again.

-- If the apocalypse comes... beep me.

Link to comment
Share on other sites

Hi,

if you are not using a func which returns the number of elements in [0] then use Ubound.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

This would break if $FullName = "First Last"

Considering that you may want to keep the same amount of elements in each separate array created.

You can use StringReplace to check and add extra whitespace to compensate.

$FullName = "First Middle Last"
$FullName = "First Last"
If StringReplace($FullName, " ", "") And @extended = 1 Then
    $FullName = StringReplace($FullName, " ", "  ")
EndIf

$temp = StringSplit($FullName," ")
If Not @error Then
    For $i = 1 To $temp[0]
        MsgBox(0, '', $temp[$i])
    Next
EndIf

$FirstName = $temp[1]
$MiddleName = $temp[2]
$LastName = $temp[3]

:whistle:

Edited by MHz
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...