Like dictionary

edited June 2014 in How To...

Hi all

How I can make a array of string but like a dictionary in python, e.g. I have a list of names, addres, etc, so I can retrieve this list like I can made in python, for exemple:

mylist[0] = {"Name": "Pedro Paulo", "address": "street number ten", "etc": "etcetera"}
mylist[1] = {"Name": "Nirma Racunda", "address": "street number eleven", "etc": "etcetera01"}
mylist[n] = {"name": "Elektra", "address": "kitchen hell", "etc": "etc02"}

its possible to make anithing like this in processing?

thank's!

Tagged:

Answers

  • Or HashMap.

  • edited June 2014

    hi friend, thank you for reply, its dont solve my problem, see above:

    1 - Im using beziersqlib to get the data from a table;

    2 - I need to retrieve the data of the fields in this table to print it in the screen;

    3 - I tried using stringlist to do this, but it returns a large list of all records in the table with a key starting at 0 for the first record in the first field, 1 for the same record in the second field, and so on.

    but what I need is to get the first record in all fields of the table to an array, get the second record in all fields in this table in a subarray of that array and so on. I need anything just like a dict in python!

    peace!

  • _vk_vk
    edited June 2014

    I don't know nothing about Sql and the way to get your data, but maybe you want a double dimension array?

    String[][] mylist = new String[3][];
    
    
    //mylist[0] = { 
    //  "Name:", "Pedro Paulo", "address:", "street number ten", "etc:", "etcetera"
    //};
    
    //could not make the above initialization work, hence teh temp Strings[]
    // but there must be a way to use this... anyone?
    
    
    // so temp [] Strings
    String[] s = { 
      "Name:", "Pedro Paulo", "address:", "street number ten", "etc:", "etcetera"
    };
    
    String[] s1 = {
      "Name:", "Nirma Racunda", "address:", "street number eleven", "etc:", "etcetera01"
    };
    
    String[] s2 = {
      "name:", "Elektra", "address:", "kitchen hell", "etc:", "etc02"
    };
    
    
    mylist[0] = s ;
    mylist[1] = s1;
    mylist[2] = s2;
    
    
    for (String[] a:mylist) {
      println(a);
    }
    
    
    
    
    for (String[] a:mylist) {
      for (String st:a) {
        println(st);
      }
    }
    
  • edited June 2014

    Perhaps a Table is more organized than a double array:
    http://processing.org/reference/Table_findRow_.html

    We can use findRow() to search for an entry by providing the column:
    http://processing.org/reference/Table_findRow_.html

  • _vk_vk
    edited June 2014

    @GoToLoop as was hoping you would have some to add about initialising the arrays using : {"xxx","yyy"}; ...

    Won't you? ;)

  • Answer ✓

    Class Table didn't have many tricks to allow multiple inserts. :-S Actually, Java in general isn't good about it at all! ~X(
    Most I've come up w/ is having a String[][] similar to @_vk. But transferring it to a Table later:

    /**
     * Table Row Insert (v1.0)
     * by GoToLoop (2014/Jun)
     *
     * forum.processing.org/two/discussion/5765/like-dictionary
     */
    
    final int ROWS = 3, COLS = 3;
    final String[][] entries = new String[ROWS][COLS];
    
    entries[0] = new String[] {
      "Pedro Paulo", "street number ten", "etcetera"
    };
    entries[1] = new String[] {
      "Nirma Racunda", "street number eleven", "etcetera01"
    };
    entries[2] = new String[] {
      "Elektra", "kitchen hell", "etc02"
    };
    
    final Table table = new Table();
    
    table.setColumnTitles(new String[] {
      "Name", "Address", "Etc"
    }
    );
    
    println(table.getColumnTitles());
    println();
    
    for (String[] row: entries) {
      table.addRow(row);
      println(row);
      println();
    }
    
    saveTable(table, dataPath("Addresses.csv"));
    
    exit();
    
  • edited June 2014

    I still say that HashMap (in an array if needed) is the structure reproducing what you show.

  • ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    
    String[] columns = 
    {
      "Name", "Address", "Etc"
    };
    
    String[][] rows = {
      { "Pedro Paulo", "street number ten", "etcetera" },
      { "Nirma Racunda", "street number eleven", "etcetera01" },
      { "Elektra", "kitchen hell", "etc02" },
    }; 
    
    for (String[] rawRow : rows)
    {
      HashMap<String, String> row = new HashMap<String, String>();
      data.add(row);
      for (int i = 0; i < columns.length; i++)
      {
        row.put(columns[i], rawRow[i]);
      }
    }
    println(data);
    

    That's the data structure the closest of what you ask for.
    Now, perhaps it might not be the best for your need, as the keys are always the same. A table structure, as given in the other answers, might be better.

  • edited June 2014 Answer ✓

    shouldn't we go for OOP and make a class "Person" with "Name", "Address", "Etc"

    and just an ArrayList of that class?

    ;-)

  • Hi @GoToLoop, its work, but I need to make the inverse with you say, e.g. I need to get the data from a table, in your example it is a .csv file, no problem if it is in a sql table fo me, becuase for it I use de beziersqlib to get the data, my doubt is how to pass these data to a array and print it on the screen one by one.

    Hi @Chrisir, you can tell-me a example of how I can get a array in a class?

    sorry guys, its because Im a python programer and in python, array is so simple, but I need to write a code in java and so I choose processing to make this.

    thank's for all

    peace!

  • Answer ✓

    ... and in Python, array is so simple, ...

    Unfortunately, Java indeed needs too much boilerplate in order to insert & grab elements from data structures! :-S

    ... how to pass these data to an array and print it on the screen one by one.

    There's a Table's undocumented method called getStringRow() which returns a String[] of values from a specified row.
    I did an example of printing a whole row 1 by 1 below. Check it out: (*)

    /**
     * Table Row Print (v1.0)
     * by GoToLoop (2014/Jun)
     *
     * forum.processing.org/two/discussion/5765/like-dictionary
     */
    
    final Table table = loadTable("Addresses.csv", "header");
    final int len = table.getRowCount();
    final String[] titles = table.getColumnTitles();
    
    println("# of rows: " + len + "\n");
    for (String s: titles)  print("\t" + s + "\t");
    println("\n");
    
    for (int i = 0; i != len; println()) {
      print("#" + nf(i, 4) + "\t");
      for (String entry: table.getStringRow(i++))  print(entry + "\t\t");
    }
    
    exit();
    
  • edited September 2014 Answer ✓

    Printing a whole row in 1 line is highly prone to be messy! X_X
    As a fix for that I did version 2. This time it prints each column entry in its own line, like a form: *-:)

    /**
     * Table Row Print (v2.0)
     * by GoToLoop (2014/Jun)
     *
     * forum.processing.org/two/discussion/5765/like-dictionary
     */
    
    final Table table = loadTable("Addresses.csv", "header, csv");
    final int len = table.getRowCount();
    final String[] titles = table.getColumnTitles();
    
    println("# of rows: " + len + "\n");
    
    for (int i = 0, ii = 0; i != len; println(), ii = 0) {
      println("#" + nf(i, 4) + ":");
    
      for (String entry: table.getStringRow(i++))
        println(titles[ii++] + ":\t " + entry);
    }
    
    exit();
    
  • edited June 2014 Answer ✓

    I am not saying HashTable and Table are not a good solution

    the OOP approach is dead simple really.

    ;-)

    Person [] persons = new Person[3];
    
    void setup() {
      size(700, 700);
    
      // here you can loadStrings and loop over your array 
      persons[0] = new Person("Pedro Paulo", "street number ten", "etcetera");
      persons[1] = new Person("Nirma Racunda", "street number eleven", "etcetera01");
      persons[2] = new Person("Elektra", "kitchen hell", "etc02");
    } // func 
    
    void draw() {
      // Use a for() loop to quickly iterate
      // through all values/objects in an array.
      for (int i=0; i < persons.length; i++) {         
        persons[i].update();
        persons[i].display();
      } // for
      noLoop();
    } // func 
    
    // ======================
    
    class Person {
    
      // properties 
      String name;
      String adress;
      String etc; 
    
      // constructor
      Person ( String name_, String adress_, String etc_ ) {
        name   = name_;
        adress = adress_;
        etc    = etc_;
      }  
    
      void update() {
        //
      }
    
      void display() {
        println (name+" / "+ adress + " / " + etc);
      }
      //
    } // class
    //
    
  • Nice, @GoToLoop, its work.

    now, you can explain how I can convert it to get these result from a class?

    thank you!

  • I did something like saveTable() + loadTable() + put them into a class in this thread:
    http://forum.processing.org/two/discussion/2801/picking-cards-at-random-then-excluding-those-from-further-picking-

    But since he only asked for printing arrays, I didn't bother to make a Person class for it! :P

  • ... result from a class?

    I only know how to insert the result into a class, like @Chrisir's Person! :(|)

  • thank you all, especially for the @GoToLoop and for @Chrisir, its solved now!

Sign In or Register to comment.