Jump to content

this script keeps having contradicting loop


Recommended Posts

i used this code to take names for a text file called data and put the info of the image and it location from the data file into the gui interface but it only detect the lines with the info and it do not show the images . here is the code 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiComboBox.au3>
#include <GuiTab.au3>
#include <file.au3>
#include <array.au3>




_Main()


Func _Main()


Local $idTab1

$Form1 = GUICreate("Form1", 500, 334, 240, 182)
$Group1 = GUICtrlCreateGroup("Group1", 20, 12, 249, 314)
$idButton6 = GUICtrlCreateButton("Info", 50, 270, 185, 49)

GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

Global $fileMPA = "C:\T-in\MPA\data.txt"

FileOpen($fileMPA, 0)

   Global $line1 = FileReadLine($fileMPA , 1)

$imp = 0


$idTab1 = GUICtrlCreateTab(300, -3, 180, 330)
GUICtrlCreateTabItem("imglist1")
GUICtrlSetTip(-1, '#Region TAB1')
GUICtrlSetTip(-1, '#Region LIST')
Local $imglist1 = GUICtrlCreateList("", 307, 26, 164, 304)


$cline =_FileCountLines($fileMPA)

 While  $imp < $cline


$line2 = FileReadLine($fileMPA , $imp + 2)
 $imp = $imp + 7

if $line2 = ""  then

    ExitLoop

   else

 GUICtrlSetData(-1, ""& $line2 &"", "")

 endif

WEnd

GUICtrlCreateTabItem("imagelist2")
GUICtrlSetTip(-1, '#Region TAB2')
GUICtrlCreateTabItem("imagelist3")
GUICtrlSetTip(-1, '#Region TAB3')

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            Exit
            Global $whatnow = GUICtrlRead($imglist1)
Case $Msg = $idButton6

            $imp2 = 0
             While  $imp2 < 30


$line2 = FileReadLine($fileMPA ,$imp2 + 2)
$line3 = FileReadLine($fileMPA , $imp2 + 3)
$line7 = FileReadLine($fileMPA , 7)

 $imp2 = $imp2 + 7

if $line2 = ""  then  ExitLoop
            If GUICtrlRead($imglist1) = ""& $line2 &""  Then MsgBox($MB_SYSTEMMODAL, "img info" , $line3)
               WEnd
            EndSelect
$imp2 = 0
             While  $imp2 < 30


$line2 = FileReadLine($fileMPA ,$imp2 + 2)
$line3 = FileReadLine($fileMPA , $imp2 + 3)
$line7 = FileReadLine($fileMPA , 7)

 $imp2 = $imp2 + 7

    Select
Case GUICtrlRead($imglist1) = ""& $line2 &""
if $line2 = "" then ExitLoop
    $Pic1 = GUICtrlCreatePic(""& $line7 &"", 185, 28, 233, 230)

If GUICtrlRead($imglist1) then  ExitLoop
EndSelect
WEnd

    WEnd

 EndFunc   ;==>_Main

the data.txt file content is 

Hibiscus
Hibiscus is a hardy perennial which grows in variety of colors, sizes and fragrances. Actually they are tropical flowers which require ample sunlight and moisture to grow well. These flowers start blooming in late spring and continuously bloom through July and August.



C:\T-in\IMG\2.jpg

Lilies
There are different types of lily flowers which bloom in August including water lilies, tiger lilies and gold band lilies. Tiger lilies generate orange flowers having black spots. This lily blooms in delayed July and beginning of August. Gold band lily produce exotic white blooms. All varieties of lilies need enough space to grow and protection from summer sun.



C:\T-in\IMG\1.jpg

Turtlehead
Growing in humid areas, turtleheads are small flowers which bloom from July to September. They mostly produce flowers of white and pink color.



C:\T-in\IMG\3.jpg

Hydrangea
These are ever green bushes which produce flowers in different colors including white, purple, blue and pink. They are easy to grow bushes and can grow 3 to 10 feet tall. They require morning sun to grow but they should be protected from noon and afternoon sun.



C:\T-in\IMG\4.jpg

Dahlias
August proves to be the peak blooming season for dahlias. Dahlias come in colors like white, orange, yellow, red and purple. They can tolerate all types of soil and require full sun to grow.
Link to comment
Share on other sites

I suggest using a 2 dimension Array holding the data from the textfile. Creeating array can be done also with regex, the rest of script is just a demo for easy solving with array:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <StaticConstants.au3>

$sFile = FileRead('flowers.txt')    ;change to your path
$aFlowers = StringSplit($sFile, '.jpg', 1)
If IsArray($aFlowers) Then
    For $j = $aFlowers[0] To 1 Step -1
        If StringStripWS($aFlowers[$j], 8) = '' Then
            _ArrayDelete($aFlowers, $j)
            $aFlowers[0] -= 1
        EndIf
    Next
    _ArrayColInsert($aFlowers, 1)
    _ArrayColInsert($aFlowers, 1)
    ;_ArrayDisplay($aFlowers)
    For $i = 1 To $aFlowers[0][0]
        $aSplit = StringSplit($aFlowers[$i][0], @CRLF, 1)
        For $j = $aSplit[0] To 1 Step -1
            If $aSplit[$j] = '' Then
                _ArrayDelete($aSplit, $j)
                $aSplit[0] -= 1
            EndIf
        Next
        ;_ArrayDisplay($aSplit, 'Split')
        If $aSplit[0] = 0 Then ExitLoop
        $aFlowers[$i][0] = $aSplit[1]
        #cs
            $sDescription = ''
            For $j = $aSplit[2] To $aSplit[0]; - 1
            if $aSplit[$j]<>'' Then $sDescription &= $aSplit[$j] & @CRLF
            Next
        #ce
        $aFlowers[$i][0] = $aSplit[1]
        $aFlowers[$i][1] = $aSplit[2]
        $aFlowers[$i][2] = $aSplit[$aSplit[0]] & '.jpg'
    Next
EndIf
;_ArrayDisplay($aFlowers)

Global $idPic =-999
$hMainGui=GUICreate('JPG-Viewer',640,480)
$idList=GUICtrlCreateList('',490,5,135,470)
For $i=1 to $aFlowers[0][0]
    GUICtrlSetData($idList,$aFlowers[$i][0])
Next
$idLblDescription=GUICtrlCreateLabel('Test',5,405,470,70);,$SS_LEFT)
GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idList
            $sVal = GUICtrlRead($idList)
            $iNo= _ArraySearch($aFlowers,$sVal)
            ConsoleWrite($iNo & ' '&$sVal & ': '& $aFlowers[$iNo][1]&@CRLF)
            GUICtrlSetData($idLblDescription,$aFlowers[$iNo][1])
            If $idPic <>-999 Then GUICtrlDelete($idPic)
            $idPic=GUICtrlCreatePic($aFlowers[$iNo][2],5,5,200,200)
    EndSwitch
WEnd

 

Edited by AutoBert
Link to comment
Share on other sites

am still not good with the autoit yet so your script will take me some time to understand especially

StringStripW

but i will give it my time to understand it .am sooo thankful . you are a life saver ,and the way you write scripts is so clean . i hope someday i could help you with something.

for now i will give this script some time to see how can i edit it

Link to comment
Share on other sites

May be @mikell or some one else of the StringRegExp experts, show a shorter way to get the 2 dimension array. Btw. there is a typo in your quote the func name is

Quote

StringStripWS

Strips the white space in a string.

, i use it to detect empty lines.

the pic's are visible in the testscript, i couldn't test it? And found typos in this line:

$idPic=GUICtrlCreatePic($aFlowers[$iNo][2],5,5,200,200)

 

Edited by AutoBert
Link to comment
Share on other sites

You could try something like the code below, so you get a 1D array
You can then use it in a For loop with step 3, or build a 2D array with 3 cols, etc

This code implies that the description is a one-liner string and that the data.txt is formatted exactly like the text in post #1 ... regular expression requirements   :)

#Include <Array.au3>

$txt = FileRead("C:\T-in\MPA\data.txt")

$res = StringRegExp($txt, '(?is)(c:\N+jpg)\s*(\N+)\R(\N+)', 3)
 _ArrayDisplay($res)
 
 #cs
Local $u = UBound($res), $flowers[$u/3][3]
For $i = 0 to $u-1 step 3
   $flowers[$i/3][0] = $res[$i+1]
   $flowers[$i/3][1] = $res[$i]
   $flowers[$i/3][2] = $res[$i+2]
Next
 _ArrayDisplay($flowers)
#ce

 

Edited by mikell
Link to comment
Share on other sites

@Jcreator But why format the text file like that? Why not line by line and separated by some character (comma would make sense except there is free text so pipes or tabs are often better)

Quote

c:\x\y.jpg|my title|This, some text, is the rest of the explanation.

... then use StringSplit or StringRegexp to split it in much the same way, but with easier regexps? :) 

Added benefit: file is easy to maintain in a spreadsheet software like excel.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

@SadBunny: i think the textfile is generated from https://orchidflowers.wordpress.com/2012/12/13/list-of-perennials-which-bloom-all-year/

@Jcreator: now the script is shortened:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <StaticConstants.au3>
#include <GuiListBox.au3>

$sFile = FileRead('flowers.txt') ;change to your path
$aRes = StringRegExp($sFile, '(?is)(c:\N+jpg)\s*(\N+)\R(\N+)', 3)
Global $aFlowers[UBound($aRes) / 3 + 1][3], $j
For $i = 0 To UBound($aRes) - 1 Step 3
    $j += 1
    $aFlowers[$j][0] = $aRes[$i + 1]
    $aFlowers[$j][1] = $aRes[$i + 2]
    $aFlowers[$j][2] = $aRes[$i]
Next
$aFlowers[0][0] = $j
;_ArrayDisplay($aFlowers)

Global $idPic = -999
$hMainGui = GUICreate('JPG-Viewer', 640, 480)
$idList = GUICtrlCreateList('', 490, 5, 135, 470)
For $i = 1 To $aFlowers[0][0]
    GUICtrlSetData($idList, $aFlowers[$i][0])
Next
$idLblHeader = GUICtrlCreateLabel('Header', 5, 5, 470, 40);,$SS_LEFT)
GUICtrlSetFont(-1, 14, 800)
$idLblBody = GUICtrlCreateLabel('Body', 5, 395, 470, 70);,$SS_LEFT)
GUICtrlSetFont(-1, 10, 400)
$sFlower=_GUICtrlListBox_GetText($idList, 0)
_FindFlowerByName($sFlower)
_GUICtrlListBox_SelectString($idList, $sFlower)
GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idList
            $sVal = GUICtrlRead($idList)
            _FindFlowerByName($sVal)
    EndSwitch
WEnd

Func _FindFlowerByName($sFlower)
    Local $iNo = _ArraySearch($aFlowers, $sFlower)
    _FindFlowerByID($iNo)
EndFunc   ;==>_FindFlowerByName

Func _FindFlowerByID($iNo)
    ;ConsoleWrite($iNo & ': '& $aFlowers[$iNo][1]&@CRLF)
    GUICtrlSetData($idLblHeader, $aFlowers[$iNo][0])
    GUICtrlSetData($idLblBody, $aFlowers[$iNo][1])
    If $idPic <> -999 Then GUICtrlDelete($idPic)
    $idPic = GUICtrlCreatePic($aFlowers[$iNo][2], 5, 55, 200, 200)
EndFunc   ;==>_FindFlowerByID

@mikell: i hoped with StringRegExp a one liner is possible for creating and assigning a 2 dimension array, but seems not possible.

Edited by AutoBert
Link to comment
Share on other sites

I found a way to get small (not so  small as StringR egExp), easy readable script using stringreplace to get a working file   for the use of _FileReadToArray:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <StaticConstants.au3>
#include <GuiListBox.au3>

$sPath = 'flowers.txt' ;change to your path
$sFile = FileRead($sPath)
$sFile = Stringreplace($sFile,'.jpg'&@CRLF,chr(255)); row seperator placeholder
Do
    $sFile = Stringreplace($sFile,@CRLF&@CRLF,@CRLF)
Until not @extended
$sFile =Stringreplace($sFile,@CRLF,'|')             ;col seperator
$sFile =Stringreplace($sFile,Chr(255), '.jpg'&@CRLF);row seperator restored
$hFile=FileOpen($sPath&'.work',2)
$sFile='|'&$sFile                                   ;all other rows have this also
FileWrite($hFile,$sFile)
FileClose($hFile)
Global $aFlowers
_FileReadToArray($sPath&'.work',$aFlowers,$FRTA_COUNT,'|')
$aFlowers[0][2]=$aFlowers[0][1] ;saving colcount
$aFlowers[0][1]=$aFlowers[0][0] ;saving rowcount
_ArrayColDelete($aFlowers,0) ;col 0 has no data so deleting
;_ArrayDisplay($aFlowers)

Global $idPic = -999
$hMainGui = GUICreate('JPG-Viewer', 640, 480)
$idList = GUICtrlCreateList('', 490, 5, 135, 470)
For $i = 1 To $aFlowers[0][0]
    GUICtrlSetData($idList, $aFlowers[$i][0])
Next
$idLblHeader = GUICtrlCreateLabel('Header', 5, 5, 470, 40);,$SS_LEFT)
GUICtrlSetFont(-1, 14, 800)
$idLblBody = GUICtrlCreateLabel('Body', 5, 395, 470, 70);,$SS_LEFT)
GUICtrlSetFont(-1, 10, 400)
$sFlower=_GUICtrlListBox_GetText($idList, 0)
_FindFlowerByName($sFlower)
_GUICtrlListBox_SelectString($idList, $sFlower)
GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idList
            $sVal = GUICtrlRead($idList)
            _FindFlowerByName($sVal)
    EndSwitch
WEnd

Func _FindFlowerByName($sFlower)
    Local $iNo = _ArraySearch($aFlowers, $sFlower)
    _FindFlowerByID($iNo)
EndFunc   ;==>_FindFlowerByName

Func _FindFlowerByID($iNo)
    ;ConsoleWrite($iNo & ': '& $aFlowers[$iNo][1]&@CRLF)
    GUICtrlSetData($idLblHeader, $aFlowers[$iNo][0])
    GUICtrlSetData($idLblBody, $aFlowers[$iNo][1])
    If $idPic <> -999 Then GUICtrlDelete($idPic)
    $idPic = GUICtrlCreatePic($aFlowers[$iNo][2], 5, 55, 200, 200)
EndFunc   ;==>_FindFlowerByID

 

Edited by AutoBert
Link to comment
Share on other sites

this last one did more then what i had in mind ^^ thank you so much i had to do some changes  the line For $i = 1 To $cline

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <StaticConstants.au3>
#include <GuiListBox.au3>

$sPath = 'flowers.txt' ;change to your path
$sFile = FileRead($sPath)
$sFile = Stringreplace($sFile,'.jpg'&@CRLF,chr(255)); row seperator placeholder
Do
    $sFile = Stringreplace($sFile,@CRLF&@CRLF,@CRLF)
Until not @extended
$sFile =Stringreplace($sFile,@CRLF,'|')             ;col seperator
$sFile =Stringreplace($sFile,Chr(255), '.jpg'&@CRLF);row seperator restored
$hFile=FileOpen($sPath&'.work',2)
$sFile='|'&$sFile                                   ;all other rows have this also
FileWrite($hFile,$sFile)
FileClose($hFile)
Global $aFlowers
_FileReadToArray($sPath&'.work',$aFlowers,$FRTA_COUNT,'|')
_ArrayColDelete($aFlowers,0) ;row 0 has no data so deleting
_ArrayDisplay($aFlowers)

Global $idPic = -999
$hMainGui = GUICreate('JPG-Viewer', 640, 480)
$idList = GUICtrlCreateList('', 490, 5, 135, 470)
$cline =_FileCountLines($sPath&'.work') ; i had to add this to make it work for me
For $i = 1 To $cline
    GUICtrlSetData($idList, $aFlowers[$i][0])
Next
$idLblHeader = GUICtrlCreateLabel('Header', 5, 5, 470, 40);,$SS_LEFT)
GUICtrlSetFont(-1, 14, 800)
$idLblBody = GUICtrlCreateLabel('Body', 5, 395, 470, 70);,$SS_LEFT)
GUICtrlSetFont(-1, 10, 400)
$sFlower=_GUICtrlListBox_GetText($idList, 0)
_FindFlowerByName($sFlower)
_GUICtrlListBox_SelectString($idList, $sFlower)
GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idList
            $sVal = GUICtrlRead($idList)
            _FindFlowerByName($sVal)
    EndSwitch
WEnd

Func _FindFlowerByName($sFlower)
    Local $iNo = _ArraySearch($aFlowers, $sFlower)
    _FindFlowerByID($iNo)
EndFunc   ;==>_FindFlowerByName

Func _FindFlowerByID($iNo)
    ;ConsoleWrite($iNo & ': '& $aFlowers[$iNo][1]&@CRLF)
    GUICtrlSetData($idLblHeader, $aFlowers[$iNo][0])
    GUICtrlSetData($idLblBody, $aFlowers[$iNo][1])
    If $idPic <> -999 Then GUICtrlDelete($idPic)
    $idPic = GUICtrlCreatePic($aFlowers[$iNo][2], 5, 55, 200, 200)
EndFunc   ;==>_FindFlowerByID

 

Link to comment
Share on other sites

am really thankful for all your help guys and am sorry for being slow at understanding the answer -.- still trying to learn this 

by the way the 

_FileReadToArray

was really useful and i tried to use it in another part of the script where it will show the flowers information and picture by typing the name of the flower 

script

#include <Misc.au3>
#include <EditConstants.au3>
#include <Constants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiComboBox.au3>
#include <GuiTab.au3>
#include <file.au3>
#include <array.au3>

Global $sfPath = 'flowers.txt'
Global $aFlowers
_FileReadToArray($sfPath,$aFlowers,$FRTA_NOCOUNT,'|')
_ArrayColDelete($aFlowers,0) ;row 0 has no data so deleting

$Form1 = GUICreate("Search Flower Name",320, 480)
$input = GUICtrlCreateInput("", 60, 360, 209, 25)
$idss = GUICtrlCreateButton("Search", 130, 400, 73, 65)

GUISetState(@SW_SHOW)




Func Searchf()



Global $Fname = GUICtrlRead($input)


        For $iNo = 0 To UBound($aFlowers[0][0]) - 1 ;

           if $Fname = $aFlowers[$iNo][0] then

MsgBox($MB_SYSTEMMODAL, "","Flower nfo", ""& $aFlowers[$iNo][1] &"")

pic()

EndIf
        Next

EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE

            Exit
         Case $nMsg = $idss

        Searchf()

    EndSwitch
 WEnd

func pic()
   For $iNo = 0 To UBound($aFlowers[0][0]) - 1
   GUICtrlCreatePic($aFlowers[$iNo][2], 20, 30, 280, 280)
    Next
   EndFunc

 

Link to comment
Share on other sites

Just a quick off-topic friendly word of advice: use the Tidy tool (Tools -> Tidy AutoIt Source or Ctrl+T). It will format your code to make it much more readable, and while doing that it will also very clearly indicate potential syntax problems. This will assist you in writing proper code and catching problems early. I press it, like, every five seconds (it also saves your code, so for me it's pretty much a Ctrl+S replacement :)).

Also, "tidy"-ing your code before posting here on the forum will also go a long way to make it much easier to read for others.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

16 hours ago, Jcreator said:

i had to do some changes  the line For $i = 1 To $cline

ups, there was an error the last flower wasn't read. So i had just corrected an edited last post. Here only the snipet to see changes:

_FileReadToArray($sPath&'.work',$aFlowers,$FRTA_COUNT,'|')
$aFlowers[0][2]=$aFlowers[0][1] ;inserted=>saving colcount      
$aFlowers[0][1]=$aFlowers[0][0] ;inserted=>saving rowcount
_ArrayColDelete($aFlowers,0) ;col 0 has no data so deleting

in your func Searchf you are using ubound wrong, must be 

For $iNo = 0 To UBound($aFlowers) - 1 ;

 

Edited by AutoBert
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...