Sign in to follow this  
Followers 0
Viktor

Please help with array structures

8 posts in this topic

Dear List,

I am new to autoit and I do not seem to be able get it right with array structures. I want to do the following:

1. I have a block of text in several lines, like :

$a="Some text @here\n Some other text @there\n Yet another text @outhere"

2. I want to split this text by the @LF characters (this results in an array) like this

$b=StringSplit($a,"\n",1)

3. Then I would somehow split these individual lines up using "@" as a delimiter, and fill these in the following multi-dimensional array:

Col(0)              Col(1) 
     [0] "Some text"             "here"
     [1] "Some other text"     "there"
     [2] "Yet another text" "outhere"

This is where my problems started: I could of course split up the individual strings by using StringSplit and "@" as a delimiter, but how I would fill them into the above array structure? Also,the dimensions of the array would change every time the program runs, because the original string could contain a different number of lines, and sometimes would not have the delimiter in certain lines (i.e. "@here" would be missing from string "$a").

If someone could show me a potential solution to the above, that would be a very useful primer for me into arrays in autoit...

Any help or pointers are appreciated....

Thanks a lot : Viktor

Share this post


Link to post
Share on other sites



maybe StringFormat may help you....?


I can do signature me.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Dear List,

I am new to autoit and I do not seem to be able get it right with array structures. I want to do the following:

1. I have a block of text in several lines, like :

$a="Some text @here\n Some other text @there\n Yet another text @outhere"

2. I want to split this text by the @LF characters (this results in an array) like this

$b=StringSplit($a,"\n",1)

3. Then I would somehow split these individual lines up using "@" as a delimiter, and fill these in the following multi-dimensional array:

Col(0)              Col(1) 
     [0] "Some text"             "here"
     [1] "Some other text"     "there"
     [2] "Yet another text" "outhere"

This is where my problems started: I could of course split up the individual strings by using StringSplit and "@" as a delimiter, but how I would fill them into the above array structure? Also,the dimensions of the array would change every time the program runs, because the original string could contain a different number of lines, and sometimes would not have the delimiter in certain lines (i.e. "@here" would be missing from string "$a").

If someone could show me a potential solution to the above, that would be a very useful primer for me into arrays in autoit...

Any help or pointers are appreciated....

Thanks a lot : Viktor

I remember a similar situation...

Try to write the whole string to a file an read it back with _FileReadToArray. Cause of the \n it should work.

Yes, it's dirty - but it's me ^^

@edit: I saw the 2nd dimension to late. Sorry.

Concern StringSplit and StringTrim too.

10101

Edited by Qualitybit

[font="Courier New"][center]Me vs. 127.0.0.1 =>> 0:2But I never give up! >:-][/center][/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Just threw this together, so it's kinda ugly, but works. You could easily turn this in to a UDF type function with error checking etc...

#include <array.au3>  ; just used for _ArrayDisplay

$original_text = 'Some text @here\n Some other text @there\n Yet another text @outhere'

$split_original = StringSplit($original_text, '\n', 1)

Dim $array[1][2]

For $i = 1 To UBound($split_original) - 1
    If $split_original[$i] = '' Then ContinueLoop
    $split_second = StringSplit($split_original[$i], '@', 1)
    ReDim $array[UBound($array) + 1][2]
    $array[$i][0] = $split_second[1]
    $array[$i][1] = $split_second[2]
Next

_ArrayDisplay($array)
Edited by xcal

Share this post


Link to post
Share on other sites

Thanks a lot XCAL! BRILLIANT, WORKS !

Appreciate your help!

Best regards: Viktor

Just threw this together, so it's kinda ugly, but works. You could easily turn this in to a UDF type function with error checking etc...

#include <array.au3>  ; just used for _ArrayDisplay

$original_text = 'Some text @here\n Some other text @there\n Yet another text @outhere'

$split_original = StringSplit($original_text, '\n', 1)

Dim $array[1][2]

For $i = 1 To UBound($split_original) - 1
    If $split_original[$i] = '' Then ContinueLoop
    $split_second = StringSplit($split_original[$i], '@', 1)
    ReDim $array[UBound($array) + 1][2]
    $array[$i][0] = $split_second[1]
    $array[$i][1] = $split_second[2]
Next

_ArrayDisplay($array)

Share this post


Link to post
Share on other sites

Just threw this together, so it's kinda ugly, but works. You could easily turn this in to a UDF type function with error checking etc...

#include <array.au3>  ; just used for _ArrayDisplay

$original_text = 'Some text @here\n Some other text @there\n Yet another text @outhere'

$split_original = StringSplit($original_text, '\n', 1)

Dim $array[1][2]

For $i = 1 To UBound($split_original) - 1
    If $split_original[$i] = '' Then ContinueLoop
    $split_second = StringSplit($split_original[$i], '@', 1)
    ReDim $array[UBound($array) + 1][2]
    $array[$i][0] = $split_second[1]
    $array[$i][1] = $split_second[2]
Next

_ArrayDisplay($array)
That should work, but only as long as there is one and only one '@' in each line. Otherwise you'll get "subscript out of range or wrong number of subscripts" errors. It's more complex if you have to deal with varying numbers of '@' in each line.

:rolleyes:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#include <array.au3>  ; just used for _ArrayDisplay

$original_text = 'Some text @here\n Some other text @there\n Yet another text @outhere'

$split_original = StringSplit($original_text, '\n', 1)

Dim $array[1][1]

For $i = 1 To UBound($split_original) - 1
    If $split_original[$i] = '' Then ContinueLoop
    $split_second = StringSplit($split_original[$i], '@', 1)
    If $split_second[0] > UBound($array, 2) Then
        $width = $split_second[0]
    Else
        $width = UBound($array, 2)
    EndIf
    ReDim $array[UBound($array) + 1][$width]
    For $x = 0 To $split_second[0] - 1
        $array[$i][$x] = StringStripWS($split_second[$x + 1], 3)
    Next
Next

_ArrayDisplay($array)

Share this post


Link to post
Share on other sites

Thanks a lot Xcal, I have also noticed the problem with the script (when a line does not have @ in there), I just have not had the time yet to work on it... I am a bit of a weekend warrior unfortunately, weekdays are so busy with work (which is not programming!), that I can only ponder on these projects at the weekend... But you have gone ahead and did it again:-)) Thanks a lot! I could not resist to try out your new solution though:

I had to do just a little modification, since I wanted to get the string from the clipboard, and "\n" did not work from there...

#include <array.au3> ; just used for _ArrayDisplay

$original_text = ClipGet()

$split_original = StringSplit($original_text, @LF, 1)

;From this part the script is the same as you wrote it.

The other thing I noticed that if you have an empty line in between other lines, then this generates (rightly so) an empty place in the array... A better behaviour would be to skip that part, and empty rows would not generated...

Thanks anyway!: Viktor

#include <array.au3>  ; just used for _ArrayDisplay

$original_text = 'Some text @here\n Some other text @there\n Yet another text @outhere'

$split_original = StringSplit($original_text, '\n', 1)

Dim $array[1][1]

For $i = 1 To UBound($split_original) - 1
    If $split_original[$i] = '' Then ContinueLoop
    $split_second = StringSplit($split_original[$i], '@', 1)
    If $split_second[0] > UBound($array, 2) Then
        $width = $split_second[0]
    Else
        $width = UBound($array, 2)
    EndIf
    ReDim $array[UBound($array) + 1][$width]
    For $x = 0 To $split_second[0] - 1
        $array[$i][$x] = StringStripWS($split_second[$x + 1], 3)
    Next
Next

_ArrayDisplay($array)

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  
Followers 0