10 Replies Latest reply on Jan 26, 2007 7:07 AM by zoidberg84

    Searching array's

      i have a horizontal list that contains 24 pictures each with a "catID" value unique to each one, so catID=1 for the first picture and catID=24 for the last one. When a user clicks on one of the pictures a variable called "catSelected" is passed the catID value of the picture. So if a user clicks on the twelve picture, "catSelected" becomes 12.

      however, i have an ArrayCollection of 600 films with each film having a catID value bestowed to it. Some of the films have the same catID value because they belong to the same category. What i want to do is to be able to search the film ArrayCollection and retrieve all the films that have the same "catID" as the "catID" selected by the user that is placed in "catSelected", does anyone know how to do this??

      if that is confusing, here is a step by step guide to what im trying to do:

      1 - user clicks a picture and the picture's "catID" value is passed to "catSelected"
      2 - a function is called that searches through the film array table that retrieve's all the films that have the same "catID" value, the function has to return ALL the records with the same "catID" value!
      3 - these films are displayed in a panel or VBox and change upon the user clicking a different picture and therefore calling the function again.

      Thank you for any help!
        • 1. Re: Searching array's
          peterent Level 2
          You can use a for-loop and compare each item and those that match can be added to another ArrayCollection. Something like:

          var matches:ArrayCollection = new ArrayCollection();
          for(var c:int=0; c < catCollection.length; c++) {
          if( catCollection[c].catID == catSelected ) matches.addItem( catCollection[c] );

          You might also check out the collection's filterFunction. This is a function that eliminates from the view items in the collection that fail its test. An example for your case might be:

          private function catFilter( item:Object ) : Boolean
          return item.catID == catSelected;

          if your ArrayCollection is called filmCollection, then filmCollection.filterFunction = catFilter; filmCollection.refresh() would cause only those items which have the same catID as catSelected to appear in the list.

          Now this may not be what you are asking, but I thought I'd give you a design alternative.
          • 2. Re: Searching array's
            zoidberg84 Level 1
            ahh thank you very much, this works very well, when i use a trace(matches) statement the console shows all the films i want, however ..... I am having a lot of trouble accessing the films in the new matches array as the problems page tells me matches is "undefined"!?

            basically what i want to display the films in the matches array in a VBox style form, but I cant seem to access the matches array outside of using a trace statement?

            thank you for your last help btw, it was brilliant, i tried filterFunction too but got a little confused... no matter cos the for loop was brilliant!
            • 3. Re: Searching array's
              peterent Level 2
              It sounds like the matches collection is in the wrong scope. If you declared it in the function that has the loop, then as soon as the function ends the variable goes out of scope and its contents are not accessible.

              If you declared matches as a member of the class, then it should be available anywhere in the scope of the class.
              • 4. Re: Searching array's
                zoidberg84 Level 1
                oh right yeah, of course, i'll play around and see if i can move it out of the function... that would explain why the trace statement works when called inside the function!
                • 5. Re: Searching array's
                  zoidberg84 Level 1
                  ok i get a feeling my problem is too complicated to explain but i will try and tell you what's going on:

                  Basically i have an xml file with 600 films on, each film has director, description, duration, and certificate values. I use a HTTPService to read in the data in E4X format and each line is read in and placed into a custom built ActionScript file that builds up an ArrayCollection called filmCollection.

                  With using your for loop and then using a trace statement to visualy make sure it has worked i only retrieve a string as an array entry that is made up of the FilmTitle and Certificate....

                  this is because in the as file i have this function:-

                  public function toString():String{
                  return "[filmByCat]"+this.FilmTitle+" "+this.Certificate;

                  I've tried to change this function to send back an entry of all the values from the xml file but instead just get one huge long string that i can't really split up to show one film on one line, for say if i wanted to make each one a button...???
                  • 6. Re: Searching array's
                    peterent Level 2
                    I thought you had an ArrayCollection of objects, not an XMLListCollection. How are you converting the XML from the HTTPService result into an Object stored in the filmCollection ArrayCollection?
                    • 7. Re: Searching array's
                      zoidberg84 Level 1
                      ok let me show you...i feel i've really confused you!

                      the HTTPService:
                      <mx:HTTPService id="prodByCatRPC" url=" http://www.screenrentals.co.uk/filmByCat.xml" result="categoryHandler(event), filmHandler(event)" resultFormat="e4x"/>

                      so then filmHandler function looks like:
                      private var filmInventory:ArrayCollection;

                      public var catSelected:int;

                      private function filmHandler(event:ResultEvent):void{
                      var prodArray:Array = new Array();
                      for each (var p:XML in event.result..film){
                      var prod:filmByCat = new filmByCat
                      filmInventory = new ArrayCollection(prodArray);

                      so then the filmByCat ActionScript file looks like:
                      package valueObjects
                      import mx.collections.ArrayCollection;

                      public class filmByCat

                      public var FilmTitle:String = "";
                      public var FilmDirector:String = "";
                      public var FilmDuration:Number = 0;
                      public var FilmDescription:String = "";
                      public var FilmRating:Number = 0;
                      public var Certificate:String = "";
                      public var catName:String = "";
                      public var catID:Number = 0;

                      public function filmByCat(_FilmTitle:String, _FilmDirector:String, _FilmDuration:Number, _FilmDescription:String, _FilmRating:Number, _Certificate:String, _catName:String, _catID:Number){
                      FilmTitle = _FilmTitle;
                      FilmDirector = _FilmDirector;
                      FilmDuration = _FilmDuration;
                      FilmDescription = _FilmDescription;
                      FilmRating = _FilmRating;
                      Certificate = _Certificate;
                      catName = _catName;
                      catID =_catID;

                      public function toString():String{
                      return "[filmByCat]"+this.FilmTitle+" "+this.Certificate; }

                      public static function buildfilmByCat(o:Object):filmByCat{
                      var p:filmByCat = new filmByCat(o.FilmTitle, o.FilmDirector, o.FilmDuration, o.FilmDescription, o.FilmRating, o.Certificate, o.catName, o.catID);

                      return p;

                      ok then i've done exactly the same and called another function called categoryHandler (as you can see in the HTTPSerivce) that retrieves the XML from the same URL but only stores the catID and catName values. Then i make the Horizontal list like so:

                      <mx:HorizontalList id="catHorizontal" dataProvider="{categoryList}" height="150" itemRenderer="views.TextAndPic" click="categorySelect()"/>

                      does this help?
                      • 8. Re: Searching array's
                        peterent Level 2
                        Thanks. That does clear it up. Your filmHandler function looks fine. You are creating a valid ArrayCollection of filmCat objects.

                        You don't need the toString and buildFilmByCat functions in the filmByCat class.

                        So what you want to do is go through the filmInventory collection and build a new collection of those filmCat objects whose catID's match a specific ID in a variable you call "catSelected".

                        The for-loop I wrote in my first response does that. It will make an ArrayCollection called matches and then fill it with those object which match.

                        All you have to do is use the matches collection as the dataProvider to some control, like the HorizontalList, TileList, DataGrid, etc.
                        • 9. Re: Searching array's
                          zoidberg84 Level 1
                          oh awesome man, i'll crack on to that tomorrow morning and see what i can do, you're basically saying to completely delete the toString and buildFilmByCat functions from filmByCat.as and then i'll prob use a dataGrid and set the dataProvider to matches....

                          brilliant, thank you!
                          • 10. Re: Searching array's
                            zoidberg84 Level 1
                            HEY that works brilliantly, you're a star!!!

                            Thank you very very much