Jump to content

An help with array splitting


Recommended Posts

Hello. I have a 1D array. Is made in this way:

1. This is a line
Messages: a message etc
Context: a context etc
2. This is a line
Messages: a message etc
Context: a context etc
3. This is a line
Messages: a message etc
Correction: a correction etc
Context: a context etc

I need to make something like:

1. This is a line|Messages: a message etc|Context: a context etc
2. This is a line|Messages: a message etc|Context: a context etc
3. This is a line|Messages: a message etc|Correction: a correction|Context: a context etc

For exporting in another software. I need to split every time there is a number when the line start, can be 1. until something like 3.976. Since i don't know if there a 2 line after a number or 3 i have opened this thread. Thanks

Link to comment
Share on other sites

Hi @myheart:)
Remember the powerful use of Step in For loops:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Local $arrArray[] = ["1. This is a line", _
                    "Messages: a message etc", _
                    "Context: a context etc", _
                    "2. This is a line", _
                    "Messages: a message etc", _
                    "Context: a context etc", _
                    "3. This is a line", _
                    "Messages: a message etc", _
                    "Correction: a correction etc", _
                    "Context: a context etc"], _
    $i = 0

For $i = 0 To UBound($arrArray) - 1 Step 3
    If $i + 2 > UBound($arrArray) Then
        ExitLoop
    Else
        ConsoleWrite($arrArray[$i] & "|" & $arrArray[$i+1] & "|" & $arrArray[$i+2] & @CRLF)
    EndIf
Next

Best Regards.

Francesco :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Hello,

What you presented looks like a plain text file?

So If I get your task correctly, any line starting with numbers (int or floating) will mark a new data set, at least one line will follow, but it also can be a row of lines that belong to the same single data set?

 

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

 The For with Step N method doesn't work since the number N of lines to be grouped isn't a constant.

Try this:

Local $aStr = [ _
    "1. This is a line", _
    "Messages: a message etc", _
    "Context: a context etc", _
    "2. This is a line", _
    "Messages: a message etc", _
    "Context: a context etc", _
    "3. This is a line", _
    "Messages: a message etc", _
    "Correction: a correction etc", _
    "Context: a context etc", _
    "4. This is a line", _
    "Messages: a message etc", _
    "Remark: a remark etc", _
    "Correction: a correction etc", _
    "Correction: another correction etc", _
    "Context: a context etc" _
]
Local $aGrouped = StringSplit(StringRegExpReplace(_ArrayToString($aStr), "\|(\d)", @LF & "$1"), @LF, 2)
_ArrayDisplay($aGrouped)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

×
×
  • Create New...