Sign in to follow this  
Followers 0
buzz44

_ArrayAddComma()

11 posts in this topic

#1 ·  Posted (edited)

I needed to have numbers I was using to have comma's in them, so I created this UDF and decided to share it :(.

;=================================================
; Function Name:    _ArrayAddComma()
; Description:      Add comma's to an array of numbers (can have alphabetic characters.
; Return Value(s):  A new array with the values delimited by comma's.
;=================================================

#include <Array.au3>

Dim $avArray[9]

$avArray[0] = "975738222836526532626264"
$avArray[1] = "2546578657872"
$avArray[2] = "65856783"
$avArray[3] = "64657865785678387"
$avArray[4] = "6356"
$avArray[5] = "105678567856029"
$avArray[6] = "7656786578341"
$avArray[7] = "573"
$avArray[8] = "This is a testing sentence."

_ArrayDisplay($avArray, "Without Comma's")
$avNew = _ArrayAddComma($avArray)
_ArrayDisplay($avNew, "With Comma's")

Func _ArrayAddComma($avArray)
   For $I = 0 To UBound($avArray) - 1
      If StringIsDigit($avArray[$I]) And StringLen($avArray[$I]) > 3 Then
         $sComma = StringLeft($avArray[$I], StringLen($avArray[$I]) - 3) & "," & StringRight($avArray[$I], 3)
         Do
            If Not StringInStr(StringLeft($sComma, 4), ",") Then
               $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
            EndIf
         Until StringInStr(StringLeft($sComma, 4), ",")
         _ArrayDelete($avArray, $I)
         _ArrayInsert($avArray, $I, $sComma)
      EndIf
   Next
   Return $avArray
EndFunc
Edited by buzz44

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites



Seems useful to me :(

Should also accept non-arrays though.


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Yes I came to that conclusion as soon as I tried to use it lol. Seeing as I got a reply so quick and you mentioned it, I'll to it now lol :(.


[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

Glad I could be of, uh, assistance? :(


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank you. @Error is now set to 1 when the interger/array contains no numbers.

;=================================================
; Function Name: _ArrayAddComma()
; Description: Add comma's to an array/integer of numbers.
; Return Value(s): On Success: A new array/integer with the value(s) delimited by comma's.
; Return Value(s): On Failure:
;                 @Error:    = 0 = No error.
;                               1 = $nValue isn't a number or the array contains no numbers.
;=================================================

;Example 1 - Array

#include <Array.au3>

Dim $avArray[9]

$avArray[0] = "975738222836526532626264"
$avArray[1] = "2546578657872"
$avArray[2] = "65856783"
$avArray[3] = "64657865785678387"
$avArray[4] = "6356"
$avArray[5] = "105678567856029"
$avArray[6] = "7656786578341"
$avArray[7] = "573"
$avArray[8] = "This is a testing sentence."

_ArrayDisplay($avArray, "Without Comma's")
$avNew = _ArrayAddComma($avArray)
_ArrayDisplay($avNew, "With Comma's")

;Example 2 - Integer

$nNum = "981298409731156"
$Commad = _ArrayAddComma($nNum)
MsgBox(4096, "Result", $nNum & " to..." & @CRLF & $Commad)

Func _ArrayAddComma($nValue)
   Local $sComma, $nValue, $sError = 0
   If IsArray($nValue) Then
      For $I = 0 To UBound($nValue) - 1
         If StringIsAlpha ($nValue) Then
            $sError = $sError + 1
            If $sError = UBound($nValue) Then
               SetError(1)
               Return (0)
            EndIf
         EndIf
      Next
      For $I = 0 To UBound($nValue) - 1
         If StringIsDigit($nValue[$I]) And StringLen($nValue[$I]) > 3 Then
            $sComma = StringLeft($nValue[$I], StringLen($nValue[$I]) - 3) & "," & _
            StringRight($nValue[$I], 3)
            Do
               If Not StringInStr(StringLeft($sComma, 4), ",") Then
                  $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _
                  StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
               EndIf
            Until StringInStr(StringLeft($sComma, 4), ",")
            $nValue[$I] = $sComma)
         EndIf
      Next
      Return $nValue
   Else
      If Not StringIsDigit($nValue) Then
         SetError(1)
         Return (0)
      Else
         If StringLen($nValue) > 3 Then
            $sComma = StringLeft($nValue, StringLen($nValue) - 3) & "," & StringRight($nValue,3)
            Do
               If Not StringInStr(StringLeft($sComma, 4), ",") Then
                  $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _
                  StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
               EndIf
            Until StringInStr(StringLeft($sComma, 4), ",")
            Return $sComma
         EndIf
      EndIf
   EndIf
EndFunc
Edited by buzz44

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If any one is interested I have now changed the name of it because it no longer only deals with arrays but also non-array's.

;=================================================
; Function Name: _AddComma()
; Description: Add comma's to an array/integer of numbers.
; Return Value(s): On Success: A new array/integer with the value(s) delimited by comma's.
; Return Value(s): On Failure:
;                 @Error:    = 0 = No error.
;                               1 = $nValue isn't a number or the array contains no numbers.
;=================================================

;Example 1 - Array

#include <Array.au3>

Dim $avArray[9]

$avArray[0] = "975738222836526532626264"
$avArray[1] = "2546578657872"
$avArray[2] = "65856783"
$avArray[3] = "64657865785678387"
$avArray[4] = "6356"
$avArray[5] = "105678567856029"
$avArray[6] = "7656786578341"
$avArray[7] = "573"
$avArray[8] = "This is a testing sentence."

_ArrayDisplay($avArray, "Without Comma's")
$avNew = _AddComma($avArray)
_ArrayDisplay($avNew, "With Comma's")

;Example 2 - Integer

$nNum = "981298409731156"
$Commad = _AddComma($nNum)
MsgBox(4096, "Result", $nNum & " to..." & @CRLF & $Commad)

Func _AddComma($nValue)
   Local $sComma, $nValue, $sError = 0
   If IsArray($nValue) Then
      For $I = 0 To UBound($nValue) - 1
         If StringIsAlpha ($nValue) Then
            $sError = $sError + 1
            If $sError = UBound($nValue) Then
               SetError(1)
               Return (0)
            EndIf
         EndIf
      Next
      For $I = 0 To UBound($nValue) - 1
         If StringIsDigit($nValue[$I]) And StringLen($nValue[$I]) > 3 Then
            $sComma = StringLeft($nValue[$I], StringLen($nValue[$I]) - 3) & "," & _
            StringRight($nValue[$I], 3)
            Do
               If Not StringInStr(StringLeft($sComma, 4), ",") Then
                  $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _
                  StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
               EndIf
            Until StringInStr(StringLeft($sComma, 4), ",")
            $nValue[$I] = $sComma)
         EndIf
      Next
      Return $nValue
   Else
      If Not StringIsDigit($nValue) Then
         SetError(1)
         Return (0)
      Else
         If StringLen($nValue) > 3 Then
            $sComma = StringLeft($nValue, StringLen($nValue) - 3) & "," & StringRight($nValue,3)
            Do
               If Not StringInStr(StringLeft($sComma, 4), ",") Then
                  $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _
                  StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
               EndIf
            Until StringInStr(StringLeft($sComma, 4), ",")
            Return $sComma
         EndIf
      EndIf
   EndIf
EndFunc
Edited by buzz44

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

I wrote one of these a while back too. I never thought to use StringInStr and StringLeft to check if you have enough commas - that's a cool trick!

Here is my routine:

CODE

Func NumberCommas($Number)

;Given a number such as 12345678 will return 12,345,678

Dim $Result='', $Pos

If StringIsDigit($Number) Then

$Pos = StringLen($Number)-2

While $Pos > -2

If $Pos < 1 Then

$Result = StringMid($Number, 1, $Pos + 2) & ',' & $Result

Else

$Result = StringMid($Number, $Pos, 3) & ',' & $Result

EndIf

$Pos = $Pos - 3

WEnd

$Result=StringLeft($Result, StringLen($Result) - 1)

EndIf

Return $Result

EndFunc

I basically go through the number as a string, breaking it off 3 characters at a time, and concatenating commas in.

Share this post


Link to post
Share on other sites

Cool, when I was creating it I was faced with many options of how to do it, and there are many ways I found it. I just used the first one that same to my head, but I also wanted it to add comma's to arrays aswell.


[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Updated, now works for floating point numbers.

;=================================================
; Function Name: _AddComma()
; Description: Add comma's to an array/integer of numbers.
; Return Value(s): On Success: A new array/integer with the value(s) delimited by comma's.
; Return Value(s): On Failure:
; @Error: = 0 = No error.
; 1 = $nValue isn't a number or the array contains no numbers.
;=================================================

;Example 1 - Array

#include <Array.au3>

Dim $avArray[9]

$avArray[0] = "975738222836526532626264"
$avArray[1] = "2546578657872"
$avArray[2] = "65856783"
$avArray[3] = "64657865785678387"
$avArray[4] = "6356"
$avArray[5] = "105678567856029"
$avArray[6] = "7656786578341"
$avArray[7] = "573"
$avArray[8] = "This is a testing sentence."

_ArrayDisplay($avArray, "Without Comma's")
$avNew = _AddComma($avArray)
_ArrayDisplay($avNew, "With Comma's")

;Example 2 - Integer

$nNum = "981298409731156.37268587"
$Commad = _AddComma($nNum)
MsgBox(4096, "Result", $nNum & " to..." & @CRLF & $Commad)

Func _AddComma($nValue)
   Local $sComma, $sError = 0
   If IsArray($nValue) Then
      For $I = 0 To UBound($nValue) - 1
         If StringIsAlpha($nValue) Then
            $sError = $sError + 1
            If $sError = UBound($nValue) Then
               SetError(1)
               Return (0)
            EndIf
         EndIf
      Next
      For $I = 0 To UBound($nValue) - 1
         If StringIsDigit($nValue[$I]) And StringLen($nValue[$I]) > 3 Then
            $sComma = StringLeft($nValue[$I], StringLen($nValue[$I]) - 3) & "," & _
                  StringRight($nValue[$I], 3)
            Do
               If Not StringInStr(StringLeft($sComma, 4), ",") Then
                  $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _
                        StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
               EndIf
            Until StringInStr(StringLeft($sComma, 4), ",")
            $nValue[$I] = $sComma
         EndIf
      Next
      Return $nValue
   Else
      If Not StringIsDigit($nValue) And Not StringIsFloat($nValue) Then
         SetError(1)
         Return (0)
      Else
         If StringIsFloat($nValue) Then
            Local $Split = StringSplit($nValue, ".")
            $nValue = $Split[1]
         EndIf
         If StringLen($nValue) > 3 Then
            $sComma = StringLeft($nValue, StringLen($nValue) - 3) & "," & StringRight($nValue,3)
            Do
               If Not StringInStr(StringLeft($sComma, 4), ",") Then
                  $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _
                        StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4)
               EndIf
            Until StringInStr(StringLeft($sComma, 4), ",")
            If IsDeclared("Split") Then
               $sComma = $sComma & "." & $Split[2]
            EndIf         
            Return $sComma
         EndIf
      EndIf
   EndIf
EndFunc
Edited by buzz44

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

Thanx thanx thanx... its works sooo well...

cheers

Share this post


Link to post
Share on other sites

I know this may be too old to update:

For some reason floats with less then 3 digits before the decimals don't get reassambled.

Share this post


Link to post
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
Sign in to follow this  
Followers 0