Sign in to follow this  
Followers 0
Nyxeka

Redim Array problem

3 posts in this topic

May be a lot, but the problem I am getting is that right before the OpenIniFile I have a multi-dimensional Array called $story[1][6], and I am declaring it as a global... It turns out that when I:

Redim($story[numStories][6])<< When I put that inside the function, and declare $story[1][6] right outside the function,

The problem with the code

Dim $story[1][6] = [["", "", "","","",""]] ;It HAS to be out here so that I can use it in my while loop.

Func OpenIniFile($filename)
        ;BUT! it only works when I put it in here
    $numStories = IniRead($filename, "numStories", "key", "NotFound")
    MsgBox(0,"Done!",$numStories)
    $numStories = int($numStories)
    Redim $story[$numStories][6] ;HERE is where it thinks that $story, is NOT indeed an array (BUT IT CLEARLY IS.)
    GUICtrlSetData($StoryList,"")
    For $i = 0 to $numStories-1
        $story[$i][$StoryTitleIndex] = IniRead($filename, $i + 1, "Title", "NotFound")
        $story[$i][2] = IniRead($filename, $i + 1, "StoryID", "NotFound")
        $story[$i][3] = IniRead($filename, $i + 1, "Word_Count", "NotFound")
        $story[$i][4] = IniRead($filename, $i + 1, "Author_ID", "NotFound")
        $story[$i][5] = IniRead($filename, $i + 1, "Summary", "NotFound")
        
        GUICtrlSetData($StoryList,$i&":::"&$story[$i][1] & "     " & $story[$i][3])
    Next
    return $story
EndFunc

The full code:

;===============================
;
;   FanFiction eBook Builder
;
;   Made by Jon Hylands
;   Editted by Nicholas Hylands
;   I know that I'm being selfish here in making much of this program non-reusable...

#include "LogModule.au3"
#include "Math.au3"
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <EditConstants.au3>
#Include <GuiListBox.au3>
#include <eBookBuilder.au3>
#include <ButtonConstants.au3>


Local $inputBoxHandler
Local $inputBoxHandler2
Local $extractButton
Local $checkboxComplete
Local $checkboxCrossover
Local $progressBar
Local $Combo_2
Local $logListViewID
Local $title
Local $StoryList
Local $numStories
Local $SummaryBox
Local $ExtractStoryButton


Func initGUI()
    GUICreate("GUI menu", 800,900) ;The GUI window
    GUISetBkColor(0x00E0FFFF)
    GuiCtrlCreateLabel("Story",11,380)
    GuiCtrlCreateLabel("Word Count",350,380)
    GuiCtrlSetState($checkboxComplete, $GUI_CHECKED)
    GuiCtrlCreateLabel("Progress:",250,342)
    GuiCtrlCreateLabel("LOG",250,10)
    GuiCtrlCreateGroup("Page",5,10,200,80)
    GuiCtrlCreateLabel("Summary",450,430)
    $StoryList = GUICtrlCreateList("",10,400,400,350)
    $logListViewID = GUICtrlCreateListView("Timestamp|Record", 250,30, 500,300)
    $inputBoxHandler = GuiCtrlCreateInput("http://www.fanfiction.net/community/Independent_or_Dark_or_Powerful_Harry/7694/99/0/1/", 10, 30, 130, 20)
    $inputBoxHandler2 = GuiCtrlCreateInput("500000", 10, 60, 130, 20)
    $extractButton = GuiCtrlCreateButton("Extract Community",100,105)
    $checkboxComplete = GuiCtrlCreateCheckbox("Complete?", 10, 100, 80, 20)
    $checkboxCrossover = GuiCtrlCreateCheckbox("Crossover?", 10, 120, 80, 20)
    $progressBar = GuiCtrlCreateProgress(310,340, 440, 20)
    $SummaryBox = GUICtrlCreateEdit("", 450, 450, 300,200, $ES_AUTOVSCROLL + $WS_VSCROLL)
    $ExtractStoryButton = GuiCtrlCreateButton("  Extract Story  ",450,675,150,50)
    GUISetState()
EndFunc


Func SwapAll ($text, $findString, $replaceString)
    Local $index
    Local $parts
    Local $textOut

    $parts = StringSplit ($text, $findString)
    $textOut = ""
    for $index = 1 to $parts [0]
        if $index = $parts [0] Then
            $textOut = $textOut & $parts [$index]
        Else
            $textOut = $textOut & $parts [$index] & $replaceString
        EndIf
    Next

    return $textOut
EndFunc


Func ReadPageCount ($communityPage)

    $rawSource = BinaryToString (InetRead ($communityPage))
    $pageCount = ExtractBetween ($rawSource, "parseInt(document.myform.po.value) <= ", ")location")
    return Int ($pageCount)
EndFunc


Func ExtractSummary ($story)
    
    $title2 = ExtractBetween ($story, '">', "</a>")
    $storyId = ExtractBetween ($story, '<a  href="/s/', "/1/")
    $authorId = ExtractBetween ($story, '<a href="/u/', "/")
    $summary = ExtractBetween ($story, "<div class='z-indent z-padtop'>", "<div class='gray z-padtop2'>")
    $wordCount = ExtractBetween ($story, "Words: ", " - Reviews:")
    $reviewCount = ExtractBetween ($story, "Reviews: ", " - Updated:")
    $characterList = ExtractBetween ($story, "Published: ", " - Complete</div>")
    $textStartIndex = StringInStr ($characterList, " - ") + 3
    $characterList = StringMid ($characterList, $textStartIndex)
    
    $result = "<b>" & $title2 & '</b><p><b>Story Id</b>: <a href="http://www.fanfiction.net/s/' & $storyId & '">' & $storyId & "</a>"
    $result = $result & '<br><b>Author</b>: <a href="http://www.fanfiction.net/u/' & $authorId & '">' & $authorId & "</a>"
    $result = $result & "<br><b>Summary</b>: " & $summary & "<br><b>Word Count</b> " & $wordCount & "<br><b>Review Count</b>: " & $reviewCount
    $result = $result & "<br><b>Characters</b>: " & $characterList & "<p>"
    
    return $result
EndFunc


Func ProcessCommunity ($communityPage, $minWordCount)

    if StringRight ($communityPage, 2) = "1/" Then
        $communityPage = StringLeft ($communityPage, StringLen ($communityPage) - 2)
    EndIf
    
    $pageCount = ReadPageCount ($communityPage & "1")
    $newSource = ""
    $title = ""
    $numStories = 0
    for $pageIndex = 1 to $pageCount
        $rawSource = BinaryToString (InetRead ($communityPage & $pageIndex))
        if $title = "" Then
            $title = ExtractBetween ($rawSource, "<title>Community - ", " - FanFiction.Net</title>")
            $title = Swap ($title, " ", "_")
            $title = SwapAll ($title, "!,':?&", "")
            $CommunityTitle = $title
            FileDelete("Community-" & $CommunityTitle & ".ini")
            LogAdd ("Title: " & $title)
        EndIf
        
        LogAdd ("Parsing Page " & $pageIndex & " of " & $pageCount)
        GuiCtrlSetData($progressBar,$pageIndex*(100/$pageCount))
        $source = ExtractBetween ($rawSource, "<div class='z-list'>", "</div></div></div><hr size=1 >")
        $stories = StringSplit ($source, "<div class='z-list'>", 1)
        
        
        for $index = 1 to $stories [0]
            $story = $stories [$index]
            $StoryIsOkay = 1
            if not GUICtrlRead($checkboxComplete) = $GUI_CHECKED then
                if StringInStr($story,"Complete</div>") Then
                    $StoryIsOkay = 0
                EndIf
            EndIf
            
            if not GUICtrlRead($checkboxCrossover) = $GUI_CHECKED then 
                if StringInStr($story,"Crossover - ") Then
                    $StoryIsOkay = 0
                EndIf
            EndIf           
            
            if $StoryIsOkay = 1 Then
                $wordCount = ExtractBetween ($story, "Words: ", " - Reviews:")
                $wordCount = Int (StringReplace ($wordCount, ",", ""))
                if $wordCount >= $minWordCount Then
                    $storySummary = ExtractSummary ($story)
                    $newSource = $newSource & $storySummary & "<p><hr><p>"
                    $numStories = $numStories + 1
                    IniWrite("Community-" & $CommunityTitle & ".ini",$numStories, "Title",ExtractBetween ($storySummary, "<b>", "</b><p><b>"))
                    IniWrite("Community-" & $CommunityTitle & ".ini",$numStories, "StoryID",ExtractBetween ($storySummary, "http://www.fanfiction.net/s/", """>"))
                    IniWrite("Community-" & $CommunityTitle & ".ini",$numStories, "Word_Count",ExtractBetween ($storySummary, "<b>Word Count</b> ", "<br><b>"))
                    IniWrite("Community-" & $CommunityTitle & ".ini",$numStories, "Author_ID",ExtractBetween ($storySummary, "http://www.fanfiction.net/u/", """>"))
                    IniWrite("Community-" & $CommunityTitle & ".ini",$numStories, "Summary",ExtractBetween ($storySummary, "<b>Summary</b>: ", "<br><b>"))
                EndIf
            EndIf
            
            
        Next
        
    Next
    IniWrite("Community-" & $CommunityTitle & ".ini","numStories", "key",$numStories)
    FileDelete ("Community-" & $title & ".html")
    FileWrite ("Community-" & $title & ".html", $newSource)
EndFunc

Dim $story[1][6] = [["", "", "","","",""]]
Func OpenIniFile($filename)
    $numStories = IniRead($filename, "numStories", "key", "NotFound")
    MsgBox(0,"Done!",$numStories)
    $numStories = int($numStories)
    Redim $story[$numStories][6]
    GUICtrlSetData($StoryList,"")
    For $i = 0 to $numStories-1
        $story[$i][$StoryTitleIndex] = IniRead($filename, $i + 1, "Title", "NotFound")
        $story[$i][2] = IniRead($filename, $i + 1, "StoryID", "NotFound")
        $story[$i][3] = IniRead($filename, $i + 1, "Word_Count", "NotFound")
        $story[$i][4] = IniRead($filename, $i + 1, "Author_ID", "NotFound")
        $story[$i][5] = IniRead($filename, $i + 1, "Summary", "NotFound")
        
        GUICtrlSetData($StoryList,$i&":::"&$story[$i][1] & "     " & $story[$i][3])
    Next
    return $story
EndFunc

Func getSummaryFromParsedFile($fileName)
    
    $rawSource = BinaryToString (InetRead ("file:///" & @ScriptDir & "Community-" & $title & ".html"))
    $stories = StringSplit($rawSource,"<p><hr><p>")
    
EndFunc

Const $StoryTitleIndex = 1

Local $allTheStories [1][1]
Local $allStoriesRead = False
Local $lastSelectedStory = -1

initGUI()

LogInitialize ($logListViewID)
LogAdd ("FanFiction.net Commmunity Parser")
While 1
        $msg = GUIGetMsg()
        $msg2 = GUICtrlRead($inputBoxHandler)
        $msg3 = GUICtrlRead($inputBoxHandler2)
        
        if $allStoriesRead Then
            For $z = 0 to $numStories - 1
                
                $storySelected = _GUICtrlListBox_GetSel($StoryList, $z)
                If $storySelected and $lastSelectedStory <> $z Then
                    GuiCtrlSetData($SummaryBox,$allTheStories[$z][$StoryTitleIndex]&":::"&$z & ": " & $allTheStories[$z][5])
                    $lastSelectedStory = $z
                EndIf
            Next
        EndIf
        
        Select
            Case $msg = $GUI_EVENT_CLOSE 
                ExitLoop
            Case $msg = $extractButton
                $minWordCount  = $msg3
                $communityPage = $msg2
                if StringRight($communityPage,8) = "/99/0/1/" Then
                    ProcessCommunity ($communityPage, $minWordCount)
                    MsgBox(0,"Done!","Finished Parsing Community")
                    $allTheStories = OpenIniFile("Community-" & $title & ".ini")
                    $allStoriesRead = True
                    ;_IENavigate($object, "file:///" & @ScriptDir & "/Community-" & $title & ".html")
                    GuiCtrlSetData($progressBar,0)
                Else
                    MsgBox(48,"Error!","Community Page Address Invalid.")
                EndIf
            Case $msg = $ExtractStoryButton
                $storySelected = _GUICtrlListBox_GetSel($StoryList, $z)
                if $storySelected and _GUICtrlListBox_GetCurSel($StoryList) > -1 Then
                    ProcessStory (_GUICtrlListBox_GetCurSel($story[$StoryList][2]))
                EndIf
        EndSelect
    WEnd
GUIDelete()

Now, I am kind of new to AutoIt, and I've been learning to program for the last 4 years, but I have NO IDEA what is going on here XD

I've searched the F1 Help File -- Naughtta... (That I could find...)

Some Help would be very helpful and appreciated. :unsure:

-Nick

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Change it to

Global $story[1][6];  You don't even need the rest of this line because you are just declaring an empty array.
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

It is not obvious to me that your script doesn't work if you make the $story array global, or that the function OpenIniFile only works if you make it a Local variable. Changing Dim $story to Global $story will have no effect IMO. You haven't shown several functions so the problem could be with them.

I recommend reconsidering the way you have written the script because I think there is a good chance you will have problems, or perhaps I should say other problems.

In your main loop you assign the return of a function to $allTheStories, so now you have the global $story and, at that point at least, an identical $story array. Any function which later changes $story does not change $allTheStories, and equally, if you were to pass $allTheStories to a function which changed it it would not be the same as $story. In some functions you deal with $story and return $story but you don't use the result, although because $story is global it doesn't matter but it's a bit confusing that it returns the array.

I recommend that you steer towards an approach where any function is passed parameters for information it needs and otherwise uses local variables, and the function returns the result or has parameters which are passed using ByRef.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
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
Sign in to follow this  
Followers 0