Jump to content
Sign in to follow this  
Realm

Arrays and Elements

Recommended Posts

Realm

How Do I check to for an Array size, Lets say I need to check a string to see if it has 6 or 7 elements, and than if it has 6 elements, How do I add an element in the proper spot, lets say element3

7 elements string

"Item,value1,value2,category1,category2,edition,number,binary(yes/no)" should pass as ok

6 element string

"Item,value1,value2,category2,edition,number,binary(yes/no)" need to add Category1 in the correct element location

Help is appreciated. Thanks ahead of time!


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Malkey

Here are two different examples with the same inputs and similar outputs.

Array method:-

#include <Array.au3>

Local $string = "Item,value1,value2,category2,edition,number,binary(yes/no)"
Local $sStrInsert = "category1"
Local $iInsertAfterPos = 3
Local $sNewstring

Local $aArray = StringSplit($string, ",", 2) ; flag = 2, disable the return the count in the first element
ConsoleWrite("Number of elements in array = " & UBound($aArray) & @CRLF)

_ArrayInsert($aArray, $iInsertAfterPos, $sStrInsert)
ConsoleWrite("Number of elements in new array = " & UBound($aArray) & @CRLF)
_ArrayDisplay($aArray)

; Back to string
For $i = 0 To UBound($aArray) - 1
    $sNewstring &= $aArray[$i] & ","
Next

ConsoleWrite('New string is "' & StringTrimRight($sNewstring, 1) & '"' & @CRLF)

String method:-

Local $string = "Item,value1,value2,category2,edition,number,binary(yes/no)"
Local $sStrInsert = "category1"
Local $iInsertAfterPos = 3
Local $sNewString, $iNumElements, $iNumNewElements

StringReplace($string, ",", ",")
$iNumElements = @extended + 1
ConsoleWrite("Number of comma separated elements in string = " & $iNumElements & @CRLF)

If $iInsertAfterPos >= $iNumElements Then
    $sNewString = $string & "," & $sStrInsert
Else
    $sNewString = StringRegExpReplace($string, "((?:.+?,){" & $iInsertAfterPos & "})(.*){:content:}quot;, "${1}" & $sStrInsert & ",${2}")
EndIf

StringReplace($sNewString, ",", ",")
$iNumNewElements = @extended + 1

ConsoleWrite("Number of comma separated elements in new string = " & $iNumNewElements & @CRLF)
ConsoleWrite('New string is "' & $sNewString & '"' & @CRLF)

Share this post


Link to post
Share on other sites
Realm

Thanks a lot for your quick replies. Was not what I was looking for, but I enjoyed the material to learn from. Thanks again!

I have found the solution thanks to another member of the board!

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
JohnOne

Hi Realm

I was reading this thread with interest, but found that Malkeys example done exactly what you asked.

I wonder if you can help me out and link me to the thread that gave you your solution.

Thanks.

Edited by JohnOne

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
Realm

Hi Realm

I was reading this thread with interest, but found that Malkeys example done exactly what you asked.

I wonder if you can help me out and link me to the thread that gave you your solution.

Thanks.

I quoted that wrong, my appologies. His suggestion was great, and I shall learn from it. But I ended up with a couple more problems after using those suggestions, not that they were wrong, but my csv and ini lists had more obstacles. Another forum member helped me to come to an ultimate solution. Sorry for wording my last quote wrong.


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.