1 Reply Latest reply on Oct 22, 2008 2:50 AM by try67

    Multi-dimensional Array from .csv list

    Marco Lugli Level 1
      Hello,
      I have a csv list which appears like this:
      00001;Product_1
      00001;Product_2
      00002;Product_3

      I'm trying to assemble a script that will generate a Multi-dimensional Array from this csv file. I found something in PS-Script forum which, in part, suits my needs ("Compare filenames in a csv to files in a directory") and tried to modify it.

      //---< Create Multi-Dimensional Array from CSV list >---
      function main() {
      //Ask the user to select the comma-delimited file
      var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
      //Make sure the comma-delimited file exists and then open it
      datafile = new File(csvFile);
      if (datafile.exists){
      datafile.open('r');
      };
      var myRelazioni = new Array();
      while(!datafile.eof){//read each line to end of file
      myRelazioni.push(new Array(datafile.readln()));
      }
      datafile.close();
      }
      main();

      This way I can get an Array which look like this:
      myRelazioni = [["00001;Product_1"],["00001;Product_2"],["00002;Product_3"]]
      The question is that I want to obtain an Array which instead looks like this:
      myRelazioni = [["00001;Product_1","00001;Product_2"],["00002;Product_3"]]
      that is, I want to find a rule to group together those lines which start with the same set of characters into the same sub-array ("00001" in this case, but could be any set of characters).

      Any suggestion would be much appreciated

      Thanks
        • 1. Re: Multi-dimensional Array from .csv list
          try67 MVP & Adobe Community Professional
          Then you need to read and split each line into a variable and then scan the existing array, comparing the first item in each sub-array to the first item in your new array. If they are identical, you push the new array into the existing sub-array, and stop. If you arrive to the end, add the new array as a new sub-array to your existing array.

          I think I confused myself... hope you're not.