Wicked_Caty

Problems with arrays

10 posts in this topic

In my current project I have a part that should convert from string to array, and later on that array should be converted to a string again.

The first part works just fine and I get the array just as I want it, but on the way back I'm encountering some issues...

Func Encode($data0)

    ; works just fine, just here for clarification.
    ; That if-statement in the middle is just there that I'm always getting a string with 5 symbols
    ; $data0 = "A" in this example

    Local $data1 = ""
    Local $tmp = ""
    Local $arr = StringSplit($data0, "", 2)

    For $i = 0 To UBound($arr)-1

        $tmp = AscW($arr[$i])

        If $tmp = 0 Then
            $tmp = "00000" & $tmp
        ElseIf $tmp > 0 And $tmp < 10 Then
            $tmp = "0000" & $tmp
        ElseIf $tmp >= 10 And $tmp < 100 Then
            $tmp = "000" & $tmp
        ElseIf $tmp >= 100 And $tmp < 1000 Then
            $tmp = "00" & $tmp
        ElseIf $tmp >= 1000 And $tmp < 10000 Then
            $tmp = "0" & $tmp
        ElseIf $tmp >= 10000 And $tmp <= 65535 Then
            $tmp = $tmp
        EndIf

        $data1 = $data1 & $tmp

    Next

    Return $data1

EndFunc
Func Decode($data0)

    ; does NOT work
    ; $data0 = "00065"
    ; via MsgBox and ConsoleWrite I could find out that it fails already before StringSplit
    ; when it fails, no message is given and everything just disappears

    Local $data1 = ""
    Local $tmp = ""
    Local $arr = StringSplit(String($data0), "", 2)

    For $i = 0 To UBound($arr)-1 Step 5

        $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4] & $arr[$i+5]
        $tmp = ChrW($tmp)
        $data1 = $data1 & $tmp

    Next

    Return $data1

EndFunc

Sorry for dropping all the #include, the GUI, the login screen, etc... It's not necessary for this problem and I needn't give you a 613kB file full of plain text for finding an error in 3 lines of code.

I found out that $arr is always empty, so there must be an issue with StringSplit. I suspected "00065" might be seen as an integer and converted it to a string, but that doesn't make it work. I already played around quite a while with that issue now, I hope some of you can finally relieve me.

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Wrong Suggestion again I just miss read it.

Regards
Alien.

Edited by alien4u
Wrong Suggestion, see next 2 post.

Share this post


Link to post
Share on other sites
1 minute ago, alien4u said:

Could you provide example Input data? the one you make the first StringSplit?

Regards
Alien.

Is in the comments I added at the beginning of the files. In Encode() it's "A" and in Decode() it's "00065"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

14 minutes ago, alien4u said:

Sorry you already mention it.

So:

$data0 = "A" in this example

Then StringSplit() here in the first step is working because is only one character and you use a null delimiter:

Local $arr = StringSplit($data0, "", 2)

What is happening is in your second stage you have a bigger string with no Delimiters so the StringSplit() is not working properly.

Regards
Alien.

Actually, using "ABC" in the first step is working too. 

Edit: Using delimiters, when there are none, is also a bit difficult :/

Edited by Wicked_Caty

Share this post


Link to post
Share on other sites
1 hour ago, Wicked_Caty said:

Actually, using "ABC" in the first step is working too. 

Edit: Using delimiters, when there are none, is also a bit difficult :/

You are right sorry I just miss read it.

You could try to use IsInt() or IsString function to know if you are right and I think you are.

Regards
Alien.

Share this post


Link to post
Share on other sites
2 minutes ago, alien4u said:

You could try to use IsInt() or IsString function to know if you are right and I think you are.

$data0 in the second function is a string indeed.

Share this post


Link to post
Share on other sites

in your decode func is the line comcatenating string wrong, you try using 1 element to much  (0,1,2,3,4,5=6 elements)m correct it to: 

For $i = 0 To UBound($arr)-1 Step 5

        $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4]
        $tmp = ChrW($tmp)
        $data1 = $data1 & $tmp

    Next

 

Share this post


Link to post
Share on other sites
5 minutes ago, AutoBert said:

in your decode func is the line comcatenating string wrong, you try using 1 element to much  (0,1,2,3,4,5=6 elements)m correct it to: 

For $i = 0 To UBound($arr)-1 Step 5

        $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4]
        $tmp = ChrW($tmp)
        $data1 = $data1 & $tmp

    Next

 

Thanks. I really didn't expect the problem there. I'm coding for almost 2 years now in several languages, but I still get confused by starting to count at zero xD

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

If you're saying  $data0 fails before the string split(),  which is right at the beginning of decode (). I'd have to assume there is a problem with the variable in another part of the program. 

Or I could be completely misinterpreting what you mean by "fails"

Edited by markyrocks

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

You could use some integrated String* funcs

$data0 = "ABC"

$test1 = Encode($data0)
Msgbox(0,"encode", $test1) 

$test2 =  Decode($test1)
Msgbox(0,"decode", $test2)


Func Encode($data)
    Local $data1 = ""
    Local $arr = StringSplit($data, "", 2)
    For $i = 0 To UBound($arr)-1
        $data1 &= StringFormat("%05s", AscW($arr[$i]))
    Next
    Return $data1
EndFunc

Func Decode($data)
    Local $data1 = ""
    Local $arr = StringRegExp($data, '.{5}', 3)
    For $i = 0 To UBound($arr)-1
        $data1 &= ChrW($arr[$i])
    Next
    Return $data1
EndFunc

 

Edit
For the fun only

$data0 = "ABC"

$test1 = Encode($data0)
Msgbox(0,"encode", $test1) 

$test2 =  Decode($test1)
Msgbox(0,"decode", $test2)


Func Encode($data)
   Return Execute('"' & StringRegExpReplace($data, '.', '" & StringFormat("%05s", AscW("$0")) & "') & '"')
EndFunc

Func Decode($data)
   Return Execute('"' & StringRegExpReplace($data, '.{5}', '" & ChrW("$0") & "') & '"')
EndFunc

 

Edited by mikell

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

  • Similar Content

    • Dimmae
      By Dimmae
      Hello,
      at first: i'm new here, so please forgive me my mistakes, and show them to me, just that i can learn to do better in the future.
      Now to my Problem: i have an excel sheet, where i just need some columns for further actions, but i have no idea how to add single columns to a new array.
      I found the following code(the one i just added as a file) from 'water' in this forum, but i wont get how i could add multiple columns into a new array.
      The biggest problem in my situation is that i dont know the count of the rows i need for the array, i just got a fix number of rows, which is 4.
       
      Hope you can help me, and sry again for this 'unlucky illustration'.
       
      btw: how can i add code shown as code here, instead of posting it as a attached file?.
       
       
       
      autoit-select-column.au3
      defects.xlsx
    • czardas
      By czardas
      Haven't had much time to code recently. However the following thread inspired me.
      The debate about linear, parallel and binary search methods was rather interesting and, in an attempt to be diplomatic, I decided to combine @jchd's suggestion with @LarsJ's binary search example. I decided that the binary search algorithm required modification to make it more linear. As usual, 'if you invent something, it probably already exists and if it already exists, it exists for a reason'. My first attempt was not all that good. The code worked but was really a mess. I blame peer pressure (to post an example of a parallel search method).  I will delete that old code in due course.
      With a little memory jogging and a glance at the help file, the solution turned out to be quite easy: I just needed a better understanding of Euler. Further modification will be needed to work with more complicated unicode strings. The output could be returned as an array or a delimitered string. I'm not so interested in those details. I'm just going to post the algorithm for now and anyone, who wants to, can modify it to suit their needs. Both arrays must contain at least 1 element.
      Local $aFoo = [0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,16,19,20,23,24,26,30,35,39,40,41] Local $aBar = [0,1,5,6,7,8,9,10,11,12,13,14,17,18,19,21,24,25,26,27,34,35,38,40] ParallelExponetialSearch($aFoo, $aBar) ; Compares two lists - returning positive matches. Each input array must be unique (individually) and in alphabetical order. Func ParallelExponetialSearch($aFoo, $aBar) Local $sFind, _ $iMin_F = -1, $iMax_F = UBound($aFoo) -1, $Lo_F = $iMin_F, $Hi_F, _ $iMin_B = -1, $iMax_B = UBound($aBar) -1, $Lo_B = $iMin_B, $Hi_B While $iMin_F < $iMax_F And $iMin_B < $iMax_B ; Toggle Arrays - Which array has most untested elements? This is the one we want to search next, ; so we can bypass more comparisons because (in theory) mismatches have a greater chance of being skipped. If $iMax_F - $iMin_F >= $iMax_B - $iMin_B Then ; $aFoo has more (or an equal number of) untested elements $Hi_F = $iMax_F $iMin_B += 1 $sFind = $aBar[$iMin_B] While $Lo_F < $Hi_F ; search $aFoo For $i = 0 To Floor(Log($Hi_F - $Lo_F) / Log(2)) $Lo_F = $iMin_F + 2^$i If $aFoo[$Lo_F] = $sFind Then $iMin_F = $Lo_F ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $Lo_F & "] = $aBar[" & $iMin_B & "]" & @LF) ExitLoop 2 ElseIf $aFoo[$Lo_F] > $sFind Then $Hi_F = $Lo_F -1 $iMin_F += Floor(2^($i -1)) $Lo_F = $iMin_F ContinueLoop 2 EndIf Next $iMin_F = $Lo_F ; minimum increment is one WEnd Else ; $aBar has more untested elements $Hi_B = $iMax_B $iMin_F += 1 $sFind = $aFoo[$iMin_F] While $Lo_B < $Hi_B ; search $aBar For $i = 0 To Floor(Log($Hi_B - $Lo_B) / Log(2)) $Lo_B = $iMin_B + 2^$i If $aBar[$Lo_B] = $sFind Then $iMin_B = $Lo_B ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $iMin_F & "] = $aBar[" & $Lo_B & "]" & @LF) ExitLoop 2 ElseIf $aBar[$Lo_B] > $sFind Then $Hi_B = $Lo_B -1 $iMin_B += Floor(2^($i -1)) $Lo_B = $iMin_B ContinueLoop 2 EndIf Next $iMin_B = $Lo_B ; minimum increment is one WEnd EndIf WEnd EndFunc ;==> ParallelExponetialSearch I hope this will be useful to someone. I believe it deserved a thread of its own!
    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • RC86
      By RC86
      Afternoon!
      I have a requirement to return both a string and an array from a function so as a result I put them both into an array and returned that.  I can access them in their entirety after returning them but then I can't seem to access the array elements after this.  Should I be able to or is there a prettier way?
      #include <Array.au3> ;Memory info returned as a string and an array $memoryInfo = _getMemoryInfo() msgbox(0,"Memory Info",$memoryInfo[0]) _ArrayDisplay($memoryInfo[1],"Memory as an Array") Local $newArray[7] $memoryInfo[1] = $newArray msgbox(0,"Test element",$newArray[0]) _ArrayDisplay($newArray) Func _getMemoryInfo() Local $newArray[7] Local $array = MemGetStats() $newArray[0] = $array[0] ;% of memory in use $newArray[1] = Round($array[1]/1024 * 0.001,2) ;Total physical RAM $newArray[2] = Round($array[2]/1024 * 0.001,2) ;Availaible physical RAM $newArray[3] = Round($array[3]/1024 * 0.001,2) ;Total pagefile $newArray[4] = Round($array[4]/1024 * 0.001,2) ;Available pagefile $newArray[5] = Round($array[5]/1024 * 0.001,2) ;Total virtual $newArray[6] = Round($array[6]/1024 * 0.001,2) ;Available virtual $memoryUsage = $newArray[1] - $newarray[2] $pagefileUsage = $newArray[3] - $newarray[4] ;Output/Return Local $returnArray[2] $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB " $returnArray[1] = $newArray return $returnArray EndFunc A bit messy but hopefully it's understandable what I'm trying to achieve.
      Thanks!
    • FroVN
      By FroVN
      Hi, i have a problem :" can't set the name of file with a special character like: \;/;";|;...  have anyway to short the StringInSrt and Stringreplace? i am using this code but too long
      $title=InputBox(0,'','','')
         if StringInStr($title,'\') or StringInStr($title,'/') or StringInStr($title,':') or StringInStr($title,'*') or StringInStr($title,'?') or StringInStr($title,'"') or StringInStr($title,'<') or StringInStr($title,'>') or StringInStr($title,'|') Then
             $title=StringReplace($title,'\','-')
              $title=StringReplace($title,'/','-')
               $title=StringReplace($title,':','-')
                $title=StringReplace($title,'*','-')
                 $title=StringReplace($title,'?','-')
                  $title=StringReplace($title,'"','-')
                   $title=StringReplace($title,'<','-')
                    $title=StringReplace($title,'>','-')
                     $title=StringReplace($title,'|','-')
         EndIf