Jump to content

Split any given number into array of numbers


0x90h
 Share

Recommended Posts

once again i have found myself in troubles trying to split every given number into array of multiply of number 5 and the rest.

for example, given numbers are:

6,10,18,22

i want to split each of these numbers into array like this:

6 = [5, 1]

10 = [5, 5]

18 = [5, 5, 5, 3]

22 = [5, 5, 5, 5, 2]

would you be so nice and give me a hand on this? :(

Link to comment
Share on other sites

once again i have found myself in troubles trying to split every given number into array of multiply of number 5 and the rest.

for example, given numbers are:

6,10,18,22

i want to split each of these numbers into array like this:

6 = [5, 1]

10 = [5, 5]

18 = [5, 5, 5, 3]

22 = [5, 5, 5, 5, 2]

would you be so nice and give me a hand on this? :)

Maybe this (not tried)

$NumElements = Int($Number/5) + 1;add one for remainder even if it's zero
$remainder =  Mod($number,5)

Dim $array[$NumElements]
For $n = 0 to $NumElements - 2
  $array[$n] = 5
next
$array[$NumElements - 1] = $Remainder
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Global $Split[7], $Data

$Split[1] = Split(5, 5)
$Split[2] = Split(10, 5)
$Split[3] = Split(18, 5)
$Split[4] = Split(22, 5)
$Split[5] = Split(2, 8)
$Split[6] = Split(76, 8)
For $i = 1 To 6
    $Data &= $Split[$i] & @CRLF
Next
MsgBox("", "", $Data)

Func Split($Number, $Limit)
    $Test = ""
    While $Number > $Limit
        $Number -= $Limit
        $Test &= $Limit & ", "
    WEnd
    $Test &= $Number
    Return $Test
EndFunc   ;==>Split

That func not create array but will be useful for you i think.

Edited by Jex
Link to comment
Share on other sites

$Count = 0
$Test = ""
$Number = 28
Do
    $Number -= 5
    $Count += 1
Until $Number < 5
For $i = 1 To $Count
    $Test &= "5, "
Next
$Test &= $Number
MsgBox("", "", $Test)

That not give array but give like that :

If $Number = 28 Then $Test = "5, 5, 5, 5, 5, 3"

Not quite. If you start with a number which is less than 5 it will be wrong

$number=28
while $number >= 5
 $number -= 5
 $Test &= "5,"
wend
$test &= $number
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Not quite. If you start with a number which is less than 5 it will be wrong

$number=28
while $number >= 5
 $number -= 5
 $Test &= "5,"
wend
$test &= $number
Ahh sorry i'm didn't noticed will be wrong if number less than 5 :P

I'm fixed now thanks :)

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