Forum Discussion

slecault's avatar
slecault
Contributor
13 years ago

How to loop for each line of a multi-line string

I need to split a text file into many small ones.  My source text files are in the range of 1500 to 8000 lines.



The source file contains a list of controls with all their properties (this file is not generated within my code)

exemple:

...

[Control1]

Caption:'hello'

With:12

Height:22



[Control333]

Caption:'hello'

With:12

Height:22



From the above, I need to create a file for Control1 containing all the lines until Control333 is reached, then create another file...



What I have got so far is to read the file line by line with ReadLn, analyse the content, cumulate the lines and write the cumulated lines.

I saw that I could ReadAll lines at once. 



My question, if all my text source lines are in a single variable,  how can I loop trough this variable line by line?


3 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Stephane,



    Basically, you need to split the string by the new line character and then iterate through individual items in the resulting array. For example:

    var arr = str.split("\r\n");

    for (var i = 0; i < arr.length; i++)

    {

      // Do something with arr

    }




    You can also loop through the text using TestComplete's aqString.GetListItem object if you set aqString.ListSeparator to the new line character ("\r\n" in JScript, vbNewLine constant in VBScript or #13#10 in DelphiScript). Have a look at examples in the linked topics.





    By the way, your file looks similar to an INI file, only with a non-standard separator for name-value pairs.

    If you could modify your file into:



    ; NB: The Root section is mandatory

    [Root]



    [Control1]

    Caption='hello'

    With=12

    Height=22



    [Control333]

    Caption='hello'

    With=12

    Height=22



    you'd be able to access the sections and name-value pairs using TestComplete's Storages.INI API, instead of manually parsing the file.
  • I am using Delphi scripts...



    I tried the aqstring approach.  My data is actually comes from the clipboard and is then copied into a variable. 




    aqString.ListSeparator := '#13';                                            // I have tried #13#10, \r\n, #13


    ShowMessage(MyTextToWrite);                                          // Shows the entire variable human readable


    ShowMessage(aqString.GetListLength(MyTextToWrite));   // in all attemps listed above, it returns the value 1, I tried '.' (period) and it does return a large number not usable in this case.



    How should I specify the CrLf in the ListSeperator ?



    Here's a paste of what was written to file using ...(The file contains a little more than 14000 lines (331KB))

    aqFile.WriteToTextFile(sPath, MyTextToWrite, aqFile.ctUTF8);




    ************************************************************

    QA Title: GetQACurrentScreenInfo

    QA From: frmRxPro.pgcMain.tbsSummary.fraSummary.pgcSummary.tbsDrug.fraDrug.dbgrdSummary

    QA Début: 2012-03-05 11:37:24.278

    ************************************************************

    [frmRxPro.pgcMain.tbsSummary.fraSummary.pgcSummary.tbsDrug.fraDrug (TframeSumDrug)]

    Caption=

    Align: alClient

    AlignWithMargins: False

    Anchors: [akLeft,akTop,akRight,akBottom]

    AutoScroll: False

    AutoSize: False

    BiDiMode: bdLeftToRight

    Color: 13160660

    Constraints: (TSizeConstraints)

    Constraints: MaxHeight: 0

    Constraints: MaxWidth: 0

    Constraints: MinHeight: 0

    Constraints: MinWidth: 0

    Ctl3D: True

    Cursor: 0

    CustomHint: (TCustomHint)

    DockSite: False

    DoubleBuffered: False

    DragCursor: -12

    DragKind: dkDrag

    DragMode: dmManual

    Enabled: True

    Font: (TFont)

    Font: Charset: 1

    Font: Color: -16777208

    Font: Height: -11

    Font: Name: 'Tahoma'

    Font: Orientation: 0

    Font: Pitch: fpDefault

    Font: Size: 8

    Font: Style: []

    Height: 535

    HelpContext: 20100

    HelpKeyword: ''

    HelpType: htContext

    Hint: ''

    HorzScrollBar: (TControlScrollBar)

    HorzScrollBar: ButtonSize: 0

    HorzScrollBar: Color: -16777196

    HorzScrollBar: Increment: 8

    HorzScrollBar: Margin: 0

    HorzScrollBar: ParentColor: True

    HorzScrollBar: Position: 0

    HorzScrollBar: Range: 0

    HorzScrollBar: Size: 0

    HorzScrollBar: Smooth: False

    HorzScrollBar: Style: ssRegular

    HorzScrollBar: ThumbSize: 0

    HorzScrollBar: Tracking: False

    HorzScrollBar: Visible: True

    Left: 0

    Margins: (TMargins)

    Margins: Bottom: 3

    Margins: Left: 3

    Margins: Right: 3

    Margins: Top: 3

    Name: 'fraDrug'

    Padding: (TPadding)

    Padding: Bottom: 0

    Padding: Left: 0

    Padding: Right: 0

    Padding: Top: 0

    ParentBackground: True

    ParentBiDiMode: True

    ParentColor: True

    ParentCtl3D: True

    ParentCustomHint: True

    ParentDoubleBuffered: True

    ParentFont: False

    ParentShowHint: True

    PopupMenu: (TPopupMenu)

    ShowHint: False

    TabOrder: 0

    TabStop: False

    Tag: 0

    Top: 0

    Touch: (TTouchManager)

    Touch: GestureManager: (TCustomGestureManager)

    Touch: InteractiveGestureOptions: [igoPanSingleFingerHorizontal,igoPanSingleFingerVertical,igoPanInertia,igoParentPassthrough]

    Touch: InteractiveGestures: [igPan,igPressAndTap]

    Touch: ParentTabletOptions: True

    Touch: TabletOptions: [toPressAndHold]

    VertScrollBar: (TControlScrollBar)

    VertScrollBar: ButtonSize: 0

    VertScrollBar: Color: -16777196

    VertScrollBar: Increment: 8

    VertScrollBar: Margin: 0

    VertScrollBar: ParentColor: True

    VertScrollBar: Position: 0

    VertScrollBar: Range: 0

    VertScrollBar: Size: 0

    VertScrollBar: Smooth: False

    VertScrollBar: Style: ssRegular

    VertScrollBar: ThumbSize: 0

    VertScrollBar: Tracking: False

    VertScrollBar: Visible: True

    Visible: True

    Width: 982


    [frmRxPro.pgcMain.tbsSummary.fraSummary.pgcSummary.tbsDrug.fraDrug.Label11 (TLabel)]

    Caption=&Dossier:

    Align: alNone

    Alignment: taLeftJustify

    AlignWithMargins: False

    Anchors: [akLeft,akTop]

    AutoSize: False

    BiDiMode: bdLeftToRight

    Caption: '&Dossier:'

    Color: 13160660

    Constraints: (TSizeConstraints)

    Constraints: MaxHeight: 0

    Constraints: MaxWidth: 0

    Constraints: MinHeight: 0

    Constraints: MinWidth: 0

    Cursor: 0

    CustomHint: (TCustomHint)

    DragCursor: -12

    DragKind: dkDrag

    DragMode: dmManual

    EllipsisPosition: epNone

    Enabled: True

    FocusControl: cboSumPeriod

    Font: (TFont)

    Font: Charset: 1

    Font: Color: -16777208

    Font: Height: -11

    Font: Name: 'Tahoma'

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Stephane,



    It should be #13#10 without the surrounding apostrophes: