Stringleft and Array

Go to solution Solved by Musashi,

Recommended Posts

Hello

Can Stringleft a array ?

```#include <array.au3>

Local \$aArray = [1111, 2222222222, 333333333333333333333]

_ArrayDisplay(\$aArray)

\$SLeft = StringLeft(\$aArray,3)

_ArrayDisplay(\$SLeft)  ;~  this not work of course :)```

so the result is ; 111 , 222 , 333

tnx.

Edited by ahmeddzcom
Share on other sites

Take a look at the _ArrayTrim function.

Share on other sites

• Solution

Or :

```#include <array.au3>
Local \$aArray = [111111111, 2222222222, 3333333333]
_ArrayDisplay(\$aArray)

For \$i = 0 To UBound (\$aArray) -1 Step 1
\$aArray[\$i] = StringLeft(\$aArray[\$i] , 3)
Next
_ArrayDisplay(\$aArray)```

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share on other sites

Hi @ahmeddzcom,

like @Musashi already posted (only few minutes before I could write this here down 😅😞

```#include-once
#include <Array.au3>

Local \$aArray = [1111111111, 2222222222, 3333333333]
_ArrayDisplay(\$aArray, 'Before')

Local Const \$aNewArray = _StringLeftIn1dArray(\$aArray, 3)
_ArrayDisplay(\$aNewArray, 'After')

Func _StringLeftIn1dArray(\$aArray, \$iCount)
For \$i = 0 To Ubound(\$aArray) - 1 Step 1
\$aArray[\$i] = StringLeft(\$aArray[\$i], \$iCount)
Next

Return \$aArray
EndFunc```

[...] anyways, I guess the suggestion of @Danp2 might be the best - it's already provided by AutoIt, so no need for a custom function.

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

💎 Code Katas: [...] (comming soon)

👨‍🏫 WebDriver Tutorial (coming soon)

Share on other sites

I am a bit confused @ahmeddzcom, because the solution should be @Danp2s post in my opinion. Of course at the end it's up to you what you prefer (and also sorry to @Musashi - nothing personaly 😇). But for other people it would be more helpful to know about what is already provided by AutoIt (Danp2 post) and this should be the solution.

Best regards
Sven

Stay innovative!

Spoiler

💎 Code Katas: [...] (comming soon)

👨‍🏫 WebDriver Tutorial (coming soon)

Share on other sites

2 minutes ago, SOLVE-SMART said:

I am a bit confused @ahmeddzcom, because the solution should be @Danp2s post in my opinion. Of course at the end it's up to you what you prefer (and also sorry to @Musashi - nothing personaly 😇). But for other people it would be more helpful to know about what is already provided by AutoIt (Danp2 post) and this should be the solution.

Best regards
Sven

i can't use _ArrayTrim . Because _ArrayTrim  remove only (remove left and rigth) . i need get three characters
so i think  _ArrayTrim not working for me 😃
tnx @SOLVE-SMART

Share on other sites

The option is to remove left OR right.

Share on other sites

4 minutes ago, OJBakker said:

The option is to remove left OR right.

Hi @OJBakker

i think  _ArrayTrim not working with this code :

```#include <array.au3>
Local \$aArray = [1111, 2222222222, 333333333333333333333333]
_ArrayDisplay(\$aArray)

For \$i = 0 To UBound (\$aArray) -1 Step 1
\$aArray[\$i] = StringLeft(\$aArray[\$i] , 3)
Next
_ArrayDisplay(\$aArray)```

Edited by ahmeddzcom
Share on other sites

You are right. _ArrayTrim() won't work.

You want as parameter: Number of characters to remove.

_ArrayTrim() wants as parameter: Number of characters to trim.

Share on other sites

You will now encounter another problem:
In your original question, the elements of the array looked like this :

`Local \$aArray = [111111111, 2222222222, 3333333333]`

An hour ago you edited your starting post, and now it contains the following values :

`Local \$aArray = [1111, 2222222222, 333333333333333333333333]`

The integer value 333333333333333333333333 is outside the allowed limits, i.e. :

-2^63 largest negative value , 2^63-1 largest positive value (= 9223372036854775807)  .

As a result, the element \$sArray is displayed as 9223372036854775807 not as 333333333333333333333333

Edited by Musashi
Typo

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share on other sites

yes i did , because i need trim lines not have the same number of characters

Share on other sites

11 minutes ago, ahmeddzcom said:

... because i need trim lines not have the same number of characters

I got that . The problem is the size of the third value.

If you need to work with values that exceed the above limits, then it is worth taking a look at the bignum-udf

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share on other sites

Here are two one-liners that will return StringLeft() and StringRight() of all items in a 1D or 2D array.

```#include <array.au3>

; 1D Aarray
Local \$aArray[] = ["02", 1234, 23456789, "3456733333333333333333333"]

; Or, 2D Array
;Local \$aArray[][] = [["02", 1234, 23456789, "3456733333333333333333333"],["ab", "cdefg","hijklmnop"]]

_ArrayDisplay(\$aArray)

\$aArray1 = _ArrayFromString(StringRegExpReplace(_ArrayToString(\$aArray, "!"), "([^!\v]{3}).[^!\v]*", "\1"),  "!") ; _ArrayAllItemsStringLeft
_ArrayDisplay(\$aArray1, "Left")

\$aArray2 = _ArrayFromString(StringRegExpReplace(_ArrayToString(\$aArray,  "!"), "[^!\v]*([^!\v]{3})", "\1"),  "!") ; _ArrayAllItemsStringRight
_ArrayDisplay(\$aArray2, "Right")```

Share on other sites

On 1/16/2023 at 1:01 AM, Malkey said:

Here are two one-liners that will return StringLeft() and StringRight() of all items in a 1D or 2D array.

```#include <array.au3>

; 1D Aarray
Local \$aArray[] = ["02", 1234, 23456789, "3456733333333333333333333"]

; Or, 2D Array
;Local \$aArray[][] = [["02", 1234, 23456789, "3456733333333333333333333"],["ab", "cdefg","hijklmnop"]]

_ArrayDisplay(\$aArray)

\$aArray1 = _ArrayFromString(StringRegExpReplace(_ArrayToString(\$aArray, "!"), "([^!\v]{3}).[^!\v]*", "\1"),  "!") ; _ArrayAllItemsStringLeft
_ArrayDisplay(\$aArray1, "Left")

\$aArray2 = _ArrayFromString(StringRegExpReplace(_ArrayToString(\$aArray,  "!"), "[^!\v]*([^!\v]{3})", "\1"),  "!") ; _ArrayAllItemsStringRight
_ArrayDisplay(\$aArray2, "Right")```

thanx @Malkey
exactly what I needed 😃

Edited by ahmeddzcom

Create an account

Register a new account