Jump to content

Problem with while loop


Recommended Posts

Hi all,

 

Today my problemis with the while loop. I have 2 loops because I read each line of 2 files. And when the second loop ends, the first loop ends too !! I don't want :/

I don't understand, I do not see where the error is

Func Scr_Save()
    Local $Chemin_Fichier_Categories = @WorkingDir & "\Categories.txt"
    Local $Chemin_Fichier_Villes = @WorkingDir & "\Villes.txt"

    $Fichier_Categories = FileOpen($Chemin_Fichier_Categories, $FO_READ)
    If $Fichier_Categories = -1 Then
        Return False
    EndIf

    $Fichier_Villes = FileOpen($Chemin_Fichier_Villes, $FO_READ)
    If $Fichier_Villes = -1 Then
        Return False
    EndIf

    While 1
        $Ligne_Categorie = FileReadLine($Fichier_Categories)
        If @error = -1 Then ExitLoop
        While 1
            $Ligne_Ville = FileReadLine($Fichier_Villes)
            If @error = -1 Then ExitLoop
            ConsoleWrite($Ligne_Categorie & "-" & $Ligne_Ville & @LF)
        WEnd
    WEnd

    FileClose($Chemin_Fichier_Categories)
    FileClose($Chemin_Fichier_Villes)
EndFunc

 

Link to comment
Share on other sites

  • Developers

I don't think that the first loop ends when the second loop ends, but rather the second loop is only ran with the first record of the first loop.
You need to open the file of the second loop each cycle of the first loop to set the point back to the beginning.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

How are the categories tied to the cities?  By key, name, breaking characacter, etc.

You are not providing enough info to help you.  I cannot open the web page you pointed to.

Kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

ATR,

If you want to read the files line by line it can be done like this...

#include <FileConstants.au3>
#include <File.au3>

Scr_Save1()

Func Scr_Save1()
    Local $Chemin_Fichier_Categories = @ScriptDir & "\Categories.txt"
    Local $Chemin_Fichier_Villes = @ScriptDir & "\Villes.txt"

    $Fichier_Categories = FileOpen($Chemin_Fichier_Categories, $FO_READ)
    If $Fichier_Categories = -1 Then
        Return False
    EndIf

    $Fichier_Villes = FileOpen($Chemin_Fichier_Villes, $FO_READ)
    If $Fichier_Villes = -1 Then
        Return False
    EndIf

    While 1
        $Ligne_Ville = FileReadLine($Fichier_Villes)
        If @error = -1 Then ExitLoop
        While 1
            $Ligne_Categorie = FileReadLine($Fichier_Categories)
            If @error = -1 Then
                FileSetPos($Fichier_Categories, 0, $file_Begin) ;   reset file to BOF
                ExitLoop
            EndIf
            ConsoleWrite($Ligne_Ville & "-" & $Ligne_Categorie & @LF)
        WEnd
    WEnd

    FileClose($Chemin_Fichier_Categories)
    FileClose($Chemin_Fichier_Villes)

EndFunc   ;==>Scr_Save1

A better way to do this is to read the file to arrays...

#include <FileConstants.au3>
#include <File.au3>

Scr_Save2()

Func Scr_Save2()

    Local $aVilles, $aCategories

    _FileReadToArray(@ScriptDir & "\Villes.txt", $aVilles)
    If @error Then Exit MsgBox(17, 'Fileread to array error - Ville', 'Error = ' & @error)
    _FileReadToArray(@ScriptDir & "\Categories.txt", $aCategories)
    If @error Then Exit MsgBox(17, 'Fileread to array error - Categories', 'Error = ' & @error)

    For $i = 1 To UBound($aVilles) - 1
        For $j = 1 To UBound($aCategories) - 1
            ConsoleWrite($aVilles[$i] & "-" & $aCategories[$j] & @LF)
        Next
    Next

EndFunc   ;==>Scr_Save2

If the files are several hundred MB this may not work, depending on your available memory.

kylomas

edit - Files I used for testing... villes.txt and categories.txt

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Func File_Pos()
    Local $File_Categories = @WorkingDir & "\Categories.txt", $File_Infra = @WorkingDir & "\Villes.txt"
    Local $Infra = Null, $Categories = Null

    _FileReadToArray($File_Infra, $Infra)
    If @error Then Exit
    _FileReadToArray($File_Categories, $Categories)
    If @error Then Exit

    For $i = 1 To UBound($Infra) - 1
        For $j = 1 To UBound($Categories) - 1
            $Contenu = _INetGetSource($Categories[$j] & "/" & $Infra[$i] & "/")
            $aNbResultats = StringRegExp($Contenu, '(?i)<span class="count">(\d*)</span>', $STR_REGEXPARRAYMATCH)
            If @error = 0 Then
                If $aNbResultats[0] = 0 Then Return False
                ConsoleWrite("Number of results : " & $aNbResultats[0] & @LF) ; IT WORKS
                $aNbPages = StringRegExp($Contenu, '(?i)"pageToc"(?:.*?)(\d+)<', $STR_REGEXPARRAYMATCH)
                If @error = 0 Then
                    ConsoleWrite("Number of results : " & $aNbPages[0] & @LF) ; IT WORKS
                    For $x = 1 To $aNbPages[0]
                        ConsoleWrite($Categories[$j] & ";" & $Infra[$i] & "/page-"  & $aNbPages[$x] & @TAB & $aNbPages[0] & @LF)
                    Next
                EndIf
            EndIf
        Next
    Next
EndFunc

And I have this error :

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
ConsoleWrite($Categories[$j] & ";" & $Villes[$i] & ";page-"  & $aNbPages[$x] & @TAB & $aNbPages[0] & @LF)
ConsoleWrite($Categories[$j] & ";" & $Villes[$i] & ";page-"  & ^ ERROR

 

Thanks in advance

Link to comment
Share on other sites

My guess would be that the RegEx doesn't match while your loop is looping, so you're not getting an array returned.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

when I write in the console the result of $aNbPages[0], it works and I have a number ! ==> 8

Why my loop For $ = 1 To $aNbPages[0] is not equal to : For $ = 1 To 8 ???

Link to comment
Share on other sites

Use _ArrayDisplay on the returned array. I don't see anywhere in the documentation for StringRegExp that says it returns a count in the 0 element.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...