Announcement

Collapse
No announcement yet.

EFS to read and sort Files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • EFS to read and sort Files

    Does anyone have an efs written that will read, index and sort the rows of an existing file based on one of the values in the row that I can use/modify? I performed a cursory search of the BB and Fileshare areas and could not find any. I thought I would ask before I would have to attempt to code from scratch.

    TIA

    Steve
    Last edited by Guest; 01-02-2004, 08:30 PM.

  • #2
    Sorting Data...

    Steve,

    I have done something like this for an internal array.

    I would suggest you use the FILE IO to open and load the file into an ARRAY, then sort the array (any way you like), then use the FILE IO to re-save the file (if necessary).

    There is a SORT function for arrays, so that may help you. But if you need to sort on multiple fields, then you'll need to create your own sorting routine. The basics of a sorting routine are as follows...

    1. create a loop for the total number of records in your array.
    for (x=0; x<array.length;x++) {

    2. create a second loop to complete the sorting function.
    for (y=0; y<array.length;y++) {

    The first loop is used to step through the entire array as a "master record locator" type of system. The second loop is used to sort individual items.

    So, for example, x = 0 - the first record of the array. y then would loop from 0 to array.length-1 to sort the first value of the array into proper location. Then we fall back to the x loop to sort the remainder of the data...

    3. Now, compare the value of the array y to y+1. At this point, you'll also need temporary storage variables used to flip the values around. In the example below, we will test for an Ascending sort.

    if (array[y] > array[y+1]) { //need to change positions in array
    vTempy = array[y];
    array[y] = array[y+1];
    array[y+1] = vTempy;
    }

    4. Close both loops..

    }
    }

    This will create an ASCENDED SORT of your single field data. If you were going to try a DESCENDING sort, then you would have to reverse the loops and the logic. In other words, start at the END of the array and loop to the start of the array - plus reverse the test logic.

    To create a multiple field sort, I would think you could convert multiple fields into a text variable, then test that condition.. Like this..

    var vTempTXT1 = ""+array[y].value+array[y].value1;
    var vTempTXT2 = ""+array[y+1].value+array[y+1].value1;

    if (vTempTXT1 > vTempTXT2) { //need to change positions in array
    vTempy1 = array[y].value;
    vTempy2 = array[y].value1;
    array[y].value = array[y+1].value;
    array[y].value1 = array[y+1].value1;
    array[y+1].value = vTempy1;
    array[y+1].value1 = vTempy2;
    }

    I hope this helps you out. The file I have is for another client and thus I can't simply post it up onto this BB. But this should be enough help to get you through most of it..

    Remember to use debugPrintln() code to test everything as you are building it.

    Brad
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Re: Reply to post 'EFS to read and sort Files'



      Brad,

      Thank you for replying. This information is as good as a routine, I
      figure, because I would have to figure out someone elses code then
      change the logic anyways. I start out with an 8 element array, append
      to a file over the duration of an efs, then at the end, read all the
      rows back into an array from the file and then sort it (this is what I
      have to code) and select the top 10 from column1, store it to a file,
      resort the array and select the top 10 from column2, etc, etc. Thanks
      again!


      Happy New Year!

      Steve


      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      Comment


      • #4
        This is kind of related to the previous posts, so I will pose my question here. I have some code where I am testing a function I am creating which will maintain a sorted array list, e.g. top ten trades of the week. You send it your trade results and it puts it in the correct array order. In the process of testing this out, I put together the attached code. I populate an array with values and I pop it into the larger array. Then I send the results to the function, which takes the last array value and sticks it where it should be in the 2 dimensional array. I am past this point in testing, but I saved the file I was having problems with to post on the board to see if anyone could tell me for sure why I am having the following problem.

        I populate the array (or so I think) with values using another global array (fillArray). then I send them to a function, they come back and I print them out. I am surprised to see that all of the array rows contain the same values (of the last global array I stuck in there).

        I tried making fillArray a local variable, with no luck. Here is the output of the code. The top lines 33 - 42 are what I am filling the arrays with. The lines with the 45s are where I am popping the array rows off (notice the length of the array is decreasing each line)
        PHP Code:
        3310,10,10,10
        34
        10,9,10,10
        35
        10,9,9,10
        36
        9,10,10,10
        37
        9,9,9,10
        38
        8,8,8,10
        39
        8,8,6,10
        40
        7,7,7,10
        41
        7,5,5,10
        42
        8,8,7,10

        45
        :len;9---8,8,7,10
        45
        :len;8---8,8,7,10
        45
        :len;7---8,8,7,10
        45
        :len;6---8,8,7,10
        45
        :len;5---8,8,7,10
        45
        :len;4---8,8,7,10
        45
        :len;3---8,8,7,10
        45
        :len;2---8,8,7,10
        45
        :len;1---8,8,7,10
        45
        :len;0---8,8,7,10 
        I've attached the efs. I think that since I am adding the rows to the array with the same variable, just with different numbers, is why I am having the problem. (the above output from the efs goes to a file in the formula output directory, entitled Trace order 2X array.txt) Each row in effect has the same object in it, so as I am changing the values of that object, all the values in the array are changing. But how is this easily avoided?
        Attached Files

        Comment


        • #5
          Potential Solution...

          Steve,

          It appears to me that your code has some obvious issues.

          The first that I see is that "SampleArray" was a single variable (set to 0), not an array. Thus, I believe it is impossible for it to hold more than one "row" of data at a time. I hope this is making sense...

          The way I would resolve this is to use "Arrays of Objects". I have converted the following code to include an example of an array of objects for you. You'll see that it is basically building a single-dymensional array. Then into each array item, we place an "object of ITEMS".

          You'll have to change your sorting routines and possibly some other routines, but this should work. I use these all the time (for storing "records" of data into an array. This is a very easy tool to use to create multiple ITEMS for a single array entry.

          Let me know if this helps..

          B
          Attached Files
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment


          • #6
            Brad,

            You missed something in my code (pretty sloppy, I'll admit), the

            function Initialize_1X_Array(n1){//initialize 1 dim array

            works very well in initiating arrays, check that out and if you wish also check out the

            function Initialize_2X_Array(n1,n2){//initialize 2 dim array


            that also works very well at initiating arrays. As long as I declare a variable before premain as global, e.g. var fillArray; and then elswhere in the code I stick this line

            fillArray = Initialize_1X_Array(4);

            fillArray will will become a global array of length 4, zero filled. I have tested these quite extensively and I can assure you they work very well.

            Anyways, I look forward to reviewing what you sent, thanks!

            I already have the sorting issues worked out with the arrays, this was just a test that went a little awry that I was hoping for help to figure out.

            Thanks again Brad,

            Steve

            Comment


            • #7
              Brad,

              I took a look at what you had marked up and I think that your idea of filling the array/object in that manner solves the problem I was having.

              PHP Code:
              fPopObjArray(10101010);//calling the function to fill the object



              function fPopObjArray(Input1Input2Input3Input4)
              {
                  var 
              vArrayObj = new Object;
                  
              vArrayObj.aItem1 Input1;
                  
              vArrayObj.aItem2 Input2;
                  
              vArrayObj.aItem3 Input3;
                  
              vArrayObj.aItem4 Input4;

                  
              SampleArray.push(vArrayObj);


              I'll have to play aroung with this, thanks again Brad!

              Comment


              • #8
                You are very welcome

                Steve,

                Sometimes (not all the time), I've been thru most of this already and have tried to find the best soluion.

                I started playing with the array objects about 8 months ago and built a series of functions to control them (add/adjust them).

                When you get really advanced, you can include ON/OFF switches and others that allow to you to control individual characteristics of each array item.

                Have fun.

                B
                Brad Matheny
                eSignal Solution Provider since 2000

                Comment

                Working...
                X