Jump to content
Wicked_Caty

Problems with arrays

Recommended Posts

Wicked_Caty

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
alien4u

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
Wicked_Caty
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
Wicked_Caty
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
alien4u
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
Wicked_Caty
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
AutoBert

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
Wicked_Caty
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
markyrocks

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
mikell

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

    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
×