11 Replies Latest reply on Feb 23, 2009 1:04 AM by

    how to add loop

      hi everybody!
      i have prob in doing loop in my coding
      i'm new in flex
      anyone know how to add loop in my coding so that when the data insert, it not just loop in one sentences?
      this is the code
      var stringToSplit:String = myText.text
      var result:Array = stringToSplit.split(" ");
      var Split:LogReader = new LogReader();
      Split.ip = result[0];
      Split.date = result[3] + result[4];
      Split.method = result[5];
      Split.info = result[6] + result[7];
      Split.status = result[8];
      sourceArray.push(Split);
      dp.refresh();

      really hope some guys can help me sort out some solutions
        • 1. Re: how to add loop
          Gregory Lafrance Level 6
          This works, but try it on a Mac just to make sure it is cross platform:
          1 person found this helpful
          • 2. Re: how to add loop
            thanks for reply!
            i try but i still got error..
            are u sure this will loop not just one sentences in log?
            this is my complete coding and i just need to add your code under my function applicationcompletehandler right?


            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.events.FileEvent;



            private var fileReference:FileReference = new FileReference()



            public var investigator:ArrayCollection = new ArrayCollection(
            [ {label:"Encik Nawawi", data:1},
            {label:"Puan Nazlina", data:2},
            {label:"Cik Khairunnisa", data:3} ]);

            private function onSaveText():void
            {

            fileReference.save(myText.text,"myText.txt");

            }


            private function onLoadText():void
            {
            var f:FileFilter = new FileFilter("Text","*.txt");
            fileReference.browse([f]);
            fileReference.addEventListener(Event.SELECT,onFileSelect);
            fileReference.addEventListener(Event.COMPLETE,onFileComplete);

            }
            private function onFileSelect(event:Event):void
            { fileReference.load();
            }
            private function onFileComplete(event:Event):void
            {
            myText.text = fileReference.data.readUTFBytes(fileReference.data.length);

            }

            [Bindable] private var ac:ArrayCollection;

            private function applicationCompleteHandler():void
            {
            var stringToSplit:String = myText.text
            var result:Array = stringToSplit.split(" ");
            var Split:LogReader = new LogReader();
            var arr:Array = new Array();
            Split.ip = result[0];
            Split.date = result[3] + result[4];
            Split.method = result[5];
            Split.info = result[6] + result[7];
            Split.status = result[8];
            sourceArray.push(Split);
            dp.refresh();

            }




            ]]>
            </mx:Script>
            <mx:DataGrid width="772" height="241" x="10" y="341">
            <mx:dataProvider>
            <mx:ArrayCollection id="dp">
            <mx:source>
            <mx:Array id="sourceArray"/>
            </mx:source>
            </mx:ArrayCollection>
            </mx:dataProvider>
            <mx:columns>
            <mx:DataGridColumn dataField="ip"/>
            <mx:DataGridColumn dataField="date"/>
            <mx:DataGridColumn dataField="method"/>
            <mx:DataGridColumn dataField="info"/>
            <mx:DataGridColumn dataField="status"/>
            </mx:columns>
            </mx:DataGrid>
            <mx:ComboBox dataProvider="{investigator}" width="160" color="0x000000"
            x="140" y="38"/>



            <mx:Panel id="myPanel" width="772" height="240" verticalScrollPolicy="off"
            horizontalScrollPolicy="off" title="Log File" x="10" y="81">
            <mx:TextArea width="753" height="202" id="myText"
            text=""/>

            <mx:ControlBar height="60">
            <mx:ProgressBar id="bar" labelPlacement="bottom" themeColor="#F20D7A"
            minimum="0" visible="true" maximum="100" label="CurrentProgress 0%"
            direction="right" mode="manual" width="100%"/>
            <mx:Button label="Load Text" click="onLoadText()"/>
            <mx:Button label="Filter" click="applicationCompleteHandler()" />
            </mx:ControlBar>

            </mx:Panel>
            <mx:TextInput id="checkcase" x="140" y="8" width="160" borderStyle="outset" editable="true" borderThickness="0"/>
            <mx:Label x="47" y="10" text="Case No:" fontWeight="bold"/>
            <mx:Text width="85" color="black" text="Investigator" fontWeight="bold" x="31" y="40"/>


            its too early to love flex maybe..

            • 3. Re: how to add loop
              Gregory Lafrance Level 6
              What error are you getting? You should boil your code down to a minimum that still presents the problem. I don't have your data files.
              • 4. Re: how to add loop
                okey.
                i try so many times to make your code really connect to my code.
                i also change a little bit but i still stuck!

                this is the code after editing. some functions still uselss and i dont know how.

                and myoutput to make the entire log split using loop still does not achieved..

                [Bindable] private var ac:ArrayCollection;

                private function applicationCompleteHandler():void
                {
                var stringToSplit:String = myText.text
                var result:Array = stringToSplit.split(" ");
                var Split:LogReader = new LogReader();

                Split.ip = result[0];
                Split.date = result[3] + result[4];
                Split.method = result[5];
                Split.info = result[6] + result[7];
                Split.status = result[8];
                sourceArray.push(Split);
                dp.refresh();

                var arr:Array = new Array();
                for each(var line:String in myText.text.split("\n")){
                for each(var word:String in line.split(" ")){
                arr.push(word);
                }
                }
                ac = new ArrayCollection(arr);
                }






                ]]>
                </mx:Script>
                <mx:DataGrid width="772" height="241" x="10" y="341">
                <mx:dataProvider>
                <mx:ArrayCollection id="dp">
                <mx:source>
                <mx:Array id="sourceArray" />

                </mx:source>
                </mx:ArrayCollection>
                </mx:dataProvider>
                <mx:columns> applicationCompleteHandler

                hope u will be patient to me..
                i love flex
                • 5. Re: how to add loop
                  hi!
                  your code really help me!!
                  thank you so much. (hehe. so emotional..)
                  but..
                  it is loop for the same sentences.. how to make it loop for next sentences?

                  somebody help me..
                  • 6. Re: how to add loop
                    Level 7
                    The loop that Greg gave works for splitting sentences, assuming that the
                    sentences are on different lines. If they're on the same line, you could
                    try using a for each loop that's over the text split by a period.

                    I think you may need to do a bit more research on how loops work. You
                    might want to have a read through the documentation
                    ( http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_16.html)
                    and if there's any part of it you don't understand, do a search or ask
                    about that part.
                    1 person found this helpful
                    • 7. Re: how to add loop
                      can i know how to split by a period?
                      • 8. Re: how to add loop
                        Split.ip = result[0];
                        Split.date = result[3] + result[4];
                        Split.method = result[5];
                        Split.info = result[6] + result[7];
                        Split.status = result[8];
                        for each(var line:String in myText.text.split("\n"))
                        {
                        for each(var myText:String in line.split(" ")
                        )
                        sourceArray.push(Split);
                        ac.refresh();
                        }}

                        this is the code that make only the first sentence looping?
                        how to make the next sentences also looping?
                        the code that Greg gave is sure right but the prob is how to connect her code to my code..
                        i try but it come out with wrong output..
                        can u try to see my code?
                        • 9. Re: how to add loop
                          Level 7
                          I can't say for certain, as I don't know what's in your string.

                          The thing is that Greg's code was an example of a loop, and you have to
                          substitute your own contents and looping condition. If you can't do
                          that, then I don't think you understand what a loop is, and I'm honestly
                          not sure how to explain a core concept like that in a short forum post.
                          • 10. Re: how to add loop
                            Gregory Lafrance Level 6
                            You need to look at the FB3 help sys or Flex LiveDocs on the String and RegEx classes. Amy raises a good point. Most of the posts on this forum assume either some basic expertise in general programming concepts, or at least the ability and motivation to take suggestions and try to "run with them", figuring out the rest on your own.

                            This is not to discourage you, but to encourage you to become "nimble" in seeking out solutions. That makes you valuable, IMNAHO.
                            1 person found this helpful
                            • 11. Re: how to add loop
                              now, i know what is the problems..
                              why it is always loop in the same sentences because of this formula...
                              now, do u have idea how to split the logfile without using this-->
                              Split.ip = result[0];
                              Split.date = result[3] + result[4];
                              Split.method = result[5];
                              Split.info = result[6] + result[7];
                              Split.status = result[8];

                              for each(var line:String in myText.text.split("\n"))
                              for each (var word:String in line.split(""))
                              arr.push(Split);
                              ac.refresh();

                              and this is the example of log file that need to be splitted into those categories?

                              6.249.70.56 - - [31/Oct/2008:02:24:21 +0800] "GET /bppklms/index.php?cal_m=2&cal_y=1995 HTTP/1.1" 200 14400 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; + http://www.google.com/bot.html)"

                              92.37.121.146 - - [31/Oct/2008:02:27:27 +0800] "GET /bppk_en/login/login.php HTTP/1.1" 200 3736 " http://www.bppk.gov.my/login/login.php?msj=You%20Have%20Been%20Forced%20to%20Logout" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)"

                              114.121.64.64 - - [31/Oct/2008:02:36:01 +0800] "GET /TMimages/line.gif HTTP/1.1" 200 63 " http://bppk.gov.my/" "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"

                              really need your advises..
                              thank you..