Jump to content

SplitString to Integer


Recommended Posts

Hi

i have a quick question , how do I split a string into int. I want to sort it numerically

Spliting string into int

Func _TestSort()

$Matrix = "12,21,2,3,1"

local $currentmatrix = StringSplit( $Matrix,",",0)

_ArrayDisplay($currentmatrix)

_ArraySort($currentmatrix)

_ArrayDisplay($currentmatrix)

EndFunc

return - > 1 , 12 2, 21

Link to comment
Share on other sites

Using StringSplit creates an array of strings. You have to convert the strings to numbers.

#include <Array.au3>

_TestSort("12,21,2,3,1")

Func _TestSort($Matrix)
    local $currentmatrix = StringSplit( $Matrix,",",0)
    _ArrayDisplay($currentmatrix) ; before conversion
    For $i = 1 To $currentmatrix[0] ; loop through the array
        $currentmatrix[$i] = Number($currentmatrix[$i]) ; Convert strings to numbers
    Next
    _ArraySort($currentmatrix, 0, 1) ; Start sorting from index 1
    _ArrayDisplay($currentmatrix) ;  after sorting
EndFunc

Edit

If the array was created differntly, then the conversion may not be necessary. For example - if you simply declare an array of numbers like so:

#include <Array.au3>

Local $matrix[6] = [5,12,21,2,3,1]
_ArraySort($matrix, 0, 1)
_ArrayDisplay($matrix)
Edited by czardas
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...