Jump to content

[Solved] FileReadLine, Loops


 Share

Recommended Posts

While reading a txt file via FileReadLine all lines are read into a 7 element array. if it were to come across a line not equal to 7 elements, how could I script it to skip that line and restart the loop without exit or exitloop which stops the script.

an Example of script:

While 1 
$Data = FileReadLine($File)
    If @error = -1 Then ExitLoop
    $pos = StringInStr($Data,'",',Default,1)
    If @error Then
        ConsoleWrite("Error on line " & @ScriptLineNumber-2 & @CRLF)
        ContinueLoop
    EndIf
    $Data = StringReplace($Data,'"','')
    If @error Then
        ConsoleWrite("Error on line " & @ScriptLineNumber-2 & @CRLF)
        ContinueLoop
    EndIf
    $CardName = StringLeft($Data,$pos-2)
    If @error Then
        ConsoleWrite("Error on line " & @ScriptLineNumber-2 & @CRLF)
        ContinueLoop
    EndIf
    $Data = StringTrimLeft($Data,$pos-2)
    If @error Then
        ConsoleWrite("Error on line " & @ScriptLineNumber-2 & @CRLF)
        ContinueLoop
    EndIf
    $Data = StringSplit($Data, ",") 
    If @error Then
        ConsoleWrite("Error on line " & @ScriptLineNumber-2 & @CRLF)
        ContinueLoop
    EndIf
    $Data[1] = $Name
    
    IF $Data[0] <> 7 Then ; <<---- IF found to not equal 7 elements then
        ConsoleWrite("Error on line " & @ScriptLineNumber-2 & @CRLF) ; I need to skip current readline and restart loop
        ContinueLoop
    EndIf ;<<---
    
    Select
        Case $Data[7] = ""
            $Section = "One"
            _ArrayInsert($Data,4,"X")
        Case $Data[7] = "No"
            $Section = "Two"
        Case $Data[7] = "Yes"
            $Section = "Three"
        Case Else
            ConsoleWrite("Unexpected carddata for " & $Data[1] & @CRLF)
    EndSelect
WEnd
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. 

Link to comment
Share on other sites

  • Moderators

Realm,

What you have using ContinueLoop should work. This simplified script works for me: :idea:

$hFile = FileOpen("test.txt")

$i = 0

While 1
    $sData = FileReadLine($hFile)
    $i += 1
    If @error = -1 Then ExitLoop
    $aData = StringSplit($sData, ",")
    If $aData[0] <> 7 Then
        ConsoleWrite("Error on line " & $i & @CRLF)
        ContinueLoop
    EndIf
    ConsoleWrite("Line " & $i & " OK" & @CRLF)
WEnd

FileClose($hFile)

using this file:

1,2,3,4,5,6,7
1,2,3,4,5,6,7
1,2,3,4,5,7
1,2,3,4,5,6,7
1,2,3,4,5,6,7

I get:

Line 1 OK
Line 2 OK
Error on line 3
Line 4 OK
Line 5 OK

M23

P.S. Top Tip: Look at how I have used small letters (s, a, h) to indicate what the variable holds. It makes a script much easier to read - I was unsure at one point just what $Data was holding. :)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

sorry maybe i should have explained the entire script function more.

This is ultimately reading lines from a text file, than looking up values from an ini file.

so when it pulls $data line, and it is corrupted with a missing comma or element. it stops the script at the first case statement.

There is alot more code after the Case EndSelect statements but before the actual Wend.

What I need is for it to begin the loop over and skip the current line when it detects an error, continueloop allows the error to pass to the case statement which ultimately errors and exits, not what i need.

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. 

Link to comment
Share on other sites

  • Moderators

Realm,

continueloop allows the error to pass to the case statement

That is not what happens. From the Help file (and shown in the example code I posted):

ContinueLoop will continue execution of the loop at the expression testing statement (that is the While, Until or Next statement).

So you miss out everything from the ContinueLoop onwards and restart at the While. The code I posted does exactly that - you do not get the "OK" ConsoleWrite if the ContinueLoop is activated as the loop jumps over it to continue at the initial While.

As I said in my first post, your code should work as the ContinueLoop should bypass the Select statement. If it does not, then there is something else happening. Perhaps a bit more debugging code might help - or you need to post a bit more code (such as an extract of your file which fails) so we can test some more. :idea:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Sorry, I had a call function inside the part of the script before, where I was asking for the restart of the loop, and at the end of the function, I had statement to Exit. Sorry my fault. Thanks for your help Melba23. Please forgive me Noobishness.

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. 

Link to comment
Share on other sites

  • Moderators

Realm,

No probs - glad you got it sorted. :idea:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...