Import a .txt to an array, where each line has 4 terms and point out each line separately

edited May 2015 in How To...

What should be the best solution to import a .txt to a (String?) array, where each line has 4 terms (or more) divided by comma (,), and then convert them inside the sketch into this scheme: {float, int, string, string}, where they could be accessed and referenced as numbers or strings?

I wouldn't need to convert the string ones, as it would be possible to search inside each String[] for such elements (by using equals() function), but how to reference the numbers on the same line? And I could refer to the int or float ones only by using int() e float() conversion functions (while pointing at its positions at the array), right?

The whole question is that I need to search strings inside the sketch that points out to the float and int numbers on the same line as the chosen string (to be used as parameters).

Another detail: I may need to create one array per .txt file (there are more than one).

.txt random example:

1.0, 5000, red, circle

1775.0, 15000, green, quad

3225.0, 6000, yellow, circle, left


Answers

    • As I understand, the relevant data is the float & int pair.
    • Then you got an array of String[] w/ at least 2 elements: "color" & "shape".
    • And sometimes there can be more than 2, correct?
    • Now you say you wanna search the data pair via the String[].
    • But my question is: which 1 of them is relevant: the "color" or the "shape"?
  • edited May 2015

    Hello! Yes, sometimes there can be more than 2!

    In fact, both of these terms are relevant, but only one is needed by time. The examples I gave were made in a generic way.

    I'm working with the idea that each string is a TAG that I can use to reference each line of numbers, and each line of numbers has more than one tag, ok?

    After, I will join randomly the lines that envolves the same tag (one of the strings per line).

    Example: If I choose "red", I would get 2 numbers (one 'float' and one 'int') by line of each line that has "red" as a string element.

    A better example of the .txt line (each string as an object of the same "kind").

    1.0, 5000, red, green

    1775.0, 15000, green, brown

    3225.0, 6000, yellow, red, brown

  • edited May 2015 Answer ✓
    • So you only search by 1 "tag" at a time, right?
    • That is, if you search for "yellow", you wanna all number pairs associated w/ it.
    • And never by 2 or more "tags" like gimme everything that is both "red" & "circle".
    • If that is so, you can create 1 Map category for each number pair.

    // forum.processing.org/two/discussion/10799/
    // import-a-txt-to-an-array-where-each-line-has-4-terms-
    // and-point-out-each-line-separately
    
    import java.util.Map;
    
    final Map<String, Pair> colors = new HashMap<String, Pair>();
    final Map<String, Pair> shapes = new HashMap<String, Pair>();
    final Map<String, Pair> direct = new HashMap<String, Pair>();
    
    void setup() {
    }
    
    class Pair {
      final float f;
      final int i;
    
      Pair(float $f, int $i) {
        f = $f;
        i = $i;
      }
    
      @ Override String toString() {
        return "f: " + f + ", i: " + i;
      }
    }
    
  • GoToLoop, thanks for your attention and help! Yes, I only search for 1 "tag" at a time, and I want the pair of numbers associated w/it.

    I don't have a deep knowledge about it, so I'm going to read this code and research how to interact with it using the data I have.

  • Ok, thanks for the links! Now I'm figuring out how to map the lines of the txt file.

  • Gonna need loadStrings() to read the files:
    https://processing.org/reference/loadStrings_.html

    And split() to extract each element from each line read:
    https://processing.org/reference/split_.html

  • Oh yes! I'm already aware of these functions! I'm working on link the string data loaded from the txt file to the Map category.

Sign In or Register to comment.