Jump to content
Sign in to follow this  
butterfly

Smarter way of: Array (char) (0-9) to behave as INT

Recommended Posts

butterfly

#include <Array.au3>

    $removecommand = "1 2 8  8   11  27 99 06 041 245 875  88 8  7  45  99 076 789 654  108a"       ;This is Userinput and they are freeish to enter the string as they wish, but they Normally don't enter above 999
    $removecommand = StringReplace($removecommand, " ", ",")
    $arrayRemovecommand = StringSplit($removecommand, ",")
    _ArrayDisplay($arrayRemovecommand)
    For $z = 1 To UBound($arrayRemovecommand) - 1
        $arrayRemovecommand[$z] = DescFromCharacterToInt($arrayRemovecommand[$z])
    Next
    _ArraySort($arrayRemovecommand, 0, 1)
    _ArrayDisplay($arrayRemovecommand)

Func DescFromCharacterToInt($dfcti)
         $Aora = StringRight($dfcti,1)
         $TROLL97    = Number ($Aora)
;~          MsgBox(0,"",$TROLL97 )
            If $Aora = "a" Or $Aora = "A" Then
                $dfctiSize = StringLen($dfcti)
                If $dfctiSize = 2 Then $dfcti = "00" & $dfcti
                If $dfctiSize = 3 Then $dfcti = "0" & $dfcti
                Return $dfcti
            ElseIf ISINT($TROLL97) Then ;QST
                $dfctiSize = StringLen($dfcti)
                If $dfctiSize = 1 Then  $dfcti = "00" & $dfcti
                If $dfctiSize = 2 Then  $dfcti = "0" & $dfcti
                Return $dfcti
            else
                Return True
            EndIf
        EndFunc

I have the stress out that I never got any programming before and although everything above is working fine for me, I was wondering if there is a smarter way of doing this?

I would love to see if lessons can be learned from this

to explain a few things above here, users are free to type what they want but if they type a number twice to remove two times the object from me, but if they type for example 108a or 108A I will remove all the the quantities from object 108, the user is just instructed to use whitespace for my stringsplit

TLDR = am I forced to do as above because my array is char based to change 1, 5, 27 , 121 into 001, 005, 027, 121 in order to be able to sort my array

My multi dimensional  array requests comes from a sql request

Edited by butterfly

Share this post


Link to post
Share on other sites
JohnOne

Not sure if this is what you want.

#include <Array.au3>

$removecommand = "1 2 8  8   11  27 99 06 041 245 875  88 8  7  45  99 076 789 654  108a" ;This is Userinput and they are freeish to enter the string as they wish, but they Normally don't enter above 999
$removecommand = StringRegExpReplace($removecommand, "\x20+", ",")
$arrayRemovecommand = StringSplit($removecommand, ",")
_ArrayDisplay($arrayRemovecommand)
For $z = 1 To UBound($arrayRemovecommand) - 1
    $arrayRemovecommand[$z] = DescFromCharacterToInt($arrayRemovecommand[$z])
Next
_ArraySort($arrayRemovecommand, 0, 1)
_ArrayDisplay($arrayRemovecommand)

Func DescFromCharacterToInt($dfcti)
    Return stringformat("%03d",$dfcti)
EndFunc   ;==>DescFromCharacterToInt
  • Like 1

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
butterfly

Hello John, I absolute appreciate your effort and it seems to work.

except that it removed the "a" from 108a   (that is actually not a biggy cause my function does work around that)

but because my ambitions are high and my current understanding and knowledge about programming is so poor I can dig and look into other things

I have no one skilled around me and never went to school he he

I will check this for myself out now what you have written

   ▼▼▼ <--- I love the weird characters Down here they are like alien to me, and I need to research about it -->   ▼▼▼
StringRegExpReplace($removecommand, "\x20+", ",")

StringRegExpReplace($sInput, '(\d{2})/(\d{2})/(\d{4})', ' $2.$1.$3 ')  

StringRegExpReplace($sInput, "[aeiou]", "@") 

StringRegExpReplace($sInput, '%([^%]*?)%', 'C:\\WINDOWS\\Some Other Folder$')

("Input:\t%s\n\nOutput:\t%s", $sInput, $sOutput)

Thank you Johnone, and If anyone has good reading and Info about the this .. I would love to know

 

ps.  How can I john Autoit Chat forum?

Share this post


Link to post
Share on other sites
czardas

Perhaps something like this. It removes preceeding zeros. Note: the name of the function is no longer accurate.

;

#include <Array.au3>

$removecommand = "1 2 8  8   11  27 99 06 041 245 875  88 8  7  45  99 076 789 654  108a"       ;This is Userinput and they are freeish to enter the string as they wish, but they Normally don't enter above 999
$removecommand = StringRegExpReplace($removecommand, "\x20+", ",")
$arrayRemovecommand = StringSplit($removecommand, ",")

_ArrayDisplay($arrayRemovecommand)
For $z = 1 To $arrayRemovecommand[0]
    $arrayRemovecommand[$z] = DescFromCharacterToInt($arrayRemovecommand[$z])
Next
_ArraySort($arrayRemovecommand, 0, 1)
For $z = 0 To $arrayRemovecommand[0]
    If Not IsInt($arrayRemovecommand[$z]) Then
        $arrayRemovecommand[$z] = StringReplace($arrayRemovecommand[$z], ".1", "a")
    EndIf
Next

_ArrayDisplay($arrayRemovecommand)

Func DescFromCharacterToInt($dfcti)
    Local $Aora = StringRight($dfcti,1)
    If $Aora = "A" Then ; Case insensitive comparison
        $dfcti = StringTrimRight($dfcti, 1) + 0.1
    ElseIf StringIsDigit($dfcti) Then ;QST
        $dfcti = Number($dfcti)
    Else
        $dfcti = True ; I don't understand this part
    EndIf
    Return $dfcti
EndFunc

;

LOL, I left some code in which I meant to remove. I have removed that and fixed an edit bug. This could easily be modified to include letters a-z by (adding 0.01 through 0.26) and replacing the decimal fraction with letters after sorting.

Edited by czardas

Share this post


Link to post
Share on other sites
Chimp

... another way

#include <Array.au3>
$removecommand = "1 2 8  8   11  27 99 06 041 245 875  88 8  7  45  99 076 789 654  108a" ;This is Userinput and they are freeish to enter the string as they wish, but they Normally don't enter above 999
$arrayRemovecommand = StringSplit(StringReplace(StringStripWS(StringUpper($removecommand), 4), " ", ","), ",", 2)
$digits = StringLen(Number(_ArrayMax($arrayRemovecommand, 1)))
For $z = 0 To UBound($arrayRemovecommand) - 1
    $arrayRemovecommand[$z] = StringFormat("%0" & $digits & "s", Number($arrayRemovecommand[$z]))
Next
$arrayRemovecommand = _ArrayUnique($arrayRemovecommand)
_ArrayDelete($arrayRemovecommand, 0)
_ArraySort($arrayRemovecommand)
; _ArraySort($arrayRemovecommand, 0, 1)
_ArrayDisplay($arrayRemovecommand)

this returns a 0-based array,

if you want instead the count in the first element (in $arrayRemovecommand[0]) then comment lines 18 and 19 and uncomment line 20

Edited by PincoPanco

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
mikell

...my 2 cents :D

#include <Array.au3>

$removecommand = "1 2 8  8   11  27 99 06 041 245 875  88 8  7  45  99 076 789 654  108a"      
$arc = StringRegExp($removecommand, "\S+", 3)
$n = UBound($arc)

Dim $temp[$n+1][2]
For $z = 0 To $n-1
  $res = StringRegExp($arc[$z], '(\d+)(\D*)', 3)
  $temp[$z][0] = Number($res[0])
  $temp[$z][1] = $res[1]
Next
_ArraySort($temp)
$temp[0][0] = $n
_ArrayDisplay($temp)

Dim $final[$n+1]
For $z = 0 To $n
  $final[$z] = $temp[$z][0] & $temp[$z][1]
Next
_ArrayDisplay($final)

Share this post


Link to post
Share on other sites
butterfly

Hello all, thank you for all the help, I went with mikells example and continued from there

after some thoughts this is what I really wanted as outcome

I have 2 questions:

1. What could I have coded smarter? (maybe a smarter way)

2. what can I do to filter out everything except "numeric" and the letter "a" or "A"

a. this is preferred method of client user input towards me: "     8 1 2 8  8   11  27a 99 06 041 245 875  88 8  7  45 2  99 076 789 654  108a"

b. this is NOT preferred method of client user input towards me: " remove     8 1 2 8  8   11  27a 99 %^$ 06 041x2  245 875  88 8 aioqweur etc. dunno  7  45 2  99 076 789 654  108a"

but I still want to able to deal as good as possible with client input

if client does not start with a numeric value I am not interested by the way to process this, because I have other commands to take care of already he he such  "done"  "Slow" "take" etc ^^

#include <Array.au3>
    $removecommand = "     8 1 2 8  8   11  27a 99 06 041 245 875  88 8  7  45 2  99 076 789 654  108a"
$arc = StringRegExp($removecommand, "\S+", 3)
$n = UBound($arc)

_ArrayDisplay($arc)

Dim $temp[$n+1][2]
For $z = 0 To $n-1
  $res = StringRegExp($arc[$z], '(\d+)(\D*)', 3)
  $temp[$z][0] = Number($res[0])
  $temp[$z][1] = $res[1]
  if $temp[$z][1] = "" then $temp[$z][1] = 1

Next
_ArrayDisplay($temp)
_ArraySort($temp)
_ArrayDisplay($temp)
$temp[0][0] = $n
_ArrayDisplay($temp)

$z = 0
$arraycount =  $temp[0][0]
while 1
    $z = $z + 1
   ConsoleWrite ($arraycount & "ac" & $z  & "=" &  $temp[$z][0] & @LF)
    if $temp [$z][0] = $temp [($z - 1)][0] Then
        if $temp[$z][1] = "a" Or $temp[$z][1] = "A" Then
            _ArrayDelete($temp,$z - 1)
            Else
            $count = $temp[$z][1]
             ConsoleWrite($count  & "start" & @LF)
            $count = $count + $temp[$z - 1 ][1]
            $temp[$z][1]    =   $count
             ConsoleWrite($count & "end" & @LF)
            _ArrayDelete($temp,$z - 1)
            $z = $z - 1
        EndIf
    EndIf
    $arraycount = $arraycount - 1
if $arraycount = 1 Then ExitLoop
WEnd
_ArrayDisplay( $temp)

The reason I want this as outcome is that i need to remove items on client user input and I need to know which and how many I need to remove, I have tried to give as much as possible freedom to the user to remove Items

owwwww btw I am looking for a mentor for certain stuff, maybe anyone can help me out over skype from time to time, I won't try to ask anyone to do my own work, but i am looking some assistance on some beginner+ coding.

if anyone is willing I am eternally grateful (if this against forum policy I am sorry)

Regards Rene

Edited by butterfly

Share this post


Link to post
Share on other sites
mikell

There are 2 ways : either you manage the inputs such that the user can't enter garbage, or you include a heavy filter to get rid of the entered garbage  :)

For the 2nd case you can for instance do it like this :

#include <Array.au3>

$rc = " remove     8 1 2 8  8   11  27a 99 %^$ 06 041x2  245 875  88 8 aioqweur etc. dunno  7  45 2  99 076 789 654  108a"

$arc = StringRegExp($rc, '(?i)(?:^|\s)(\d+a?)(?=\s|$)', 3)
_ArrayDisplay($arc)
Edited by mikell

Share this post


Link to post
Share on other sites
butterfly

 

There are 2 ways : either you manage the inputs such that the user can't enter garbage, or you include a heavy filter to get rid of the entered garbage  :)

For the 2nd case you can for instance do it like this :

 

I have no control over the user input, I am unable to manage that because it the program I read from is not mine, Lets say for example skype :)

Edited by butterfly

Share this post


Link to post
Share on other sites
butterfly

Update:

 I have the following Situation:

my users have only option to have about 10 commands

1 or More

2 or help

3 or Walk

4 or Buy

5 or slow

6 or done

after I took that command and processed it I have the following option: (not that the shortcut digits are gone)

 More

 help

 Walk

 Buy

 slow

 done

&

(able to remove but they do not have to type remove before entering the function / loop

this is to undo previous steps

If my user is really not clever and types rubbish such as presented here

$rc = "    8 1 2 8  8   11  27a 99 %^$ 06 041x2  245 875  88 8 aioqweur etc. dunno  7  45 2  99 remove   076 789 654  108a"

I want my program to do the following thing from this point on

it either accepts of course

i can do for example

if chat is {More} then go to func() more etc

if chat is {help} then go to func() help  etc

if chat is {Walk} then go to func() walk etc

 {Buy} etc

 {slow} etc

 {done} etc

if chat is not either of the above then do try remove

but depending on how much rubbish it is or isnt

i want to make a some kind of calculated chance whether I accept it as a Remove command and try to process it

or that i need to dispose it as total rubbish and reply that the command is invalid

Help is appreciated

 

My other question is.  although I will try to solve this myself in the upcoming 45 minutes is

$RemoveCommandStep1 = "     8 1 2 8  8   11  27a 99 %^$ 06 041x2  245 875  88 8 aioqweur etc. dunno  7  45 2  99 076 789 654  108a"

$ArrayRemoveCommandStep1 = StringRegExp($RemoveCommandStep1, '(?i)(?:^|\s)(\d{1,3}a?)(?=\s|$)', 3)

THis is working fine as it is, I would just like to add the following 3 combinations

they can either type
8a
8all
8*

instead of just accepting the 'a' only

to select all of them

 

 

Share this post


Link to post
Share on other sites
mikell

Honestly, as english is not my native language I did't understand the first part of your post

But I understood the 2nd part  :)

#include <Array.au3>

$rc = " remove     8 1 2 8all  8   11*  27a 99 %^$ 06 041x2  245 875  88 8 aioqweur etc. dunno  7  45 2  99 076 789 654  108a"

$arc = StringRegExp($rc, '(?i)(?:^|\s)(\d+(?:a|all|\*)?)(?=\s|$)', 3)
_ArrayDisplay($arc)

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  

×