Replacing an array of strings with integer

Hi,

I have an array of Strings, {a,b,c,b,a,d,a,c,a}. I wanted to sort and replace them with integers, and change it as {0,1,2,1,0,3,0,2,0}

As the first step I tried removing the repeating elements from the array and made another array of strings {a,b,c,d}. Then I used a for loop and assigned values for these strings as {0,1,2,3}.

Now I wanted to replace the individual members in the first array of strings, where "a" is replaced with "0", "b" with "1", "c " with "2" and "d" with "3", and make an array {0,1,2,1,0,3,0,2,0}

String [] name = {"a", "b", "c", "b", "a", "d", "a", "c", "a"};

IntDict sortedName;
IntDict finalNameList;


void setup() {
  size(600, 600);
  smooth();

  sortedName = new IntDict();
  finalNameList = new IntDict();


  for (int i=0; i<name.length; i++) {
    finalNameList.increment(name[i]);
  }

  String [] names = sortedName.keyArray(); //// sorted array of strings {a,b,c,d}

  for (int i=0; i<names.length; i++) {
    finalNameList.set(names[i], i);
  }

  int [] values = finalNameList.valueArray(); ///// assigned values for a,b,c,d-- 0,1,2,3


  ////// now i wanted to replace the strings in the first array, name with the integers

  for (int i =0; i<name.length; i++) {
    name[i]= name[i].replaceAll(names[i], i-names.length);

    println(name[i]);
  }
}


void draw() {

  background(125);
}

would be great if someone can help me with this. many thanks in advance :)

Answers

  • Hey.

    In this special case, this can be really straight forward.

    String [] names = {"a", "b", "c", "b", "a", "d", "a", "c", "a"};
    int[] numbers;
    
    void setup() {
      // create int-array with same length as names-array
      numbers = new int[names.length]; 
    
      // iterate over names-array
      for (int i =0; i<names.length; i++) {
    
        // get first character from string and subtract value of 'a'
        int newValue = names[i].charAt(0) - 'a';
    
        //assign value to int-array
        numbers[i] = newValue;
      }
      // print content of int-array
      printArray(numbers);
    }
    
  • Note, that the example above only works if your "names" are single characters and the mapping is exactly as you described above (a:0, b:1, c:2, d:4).

  • edited October 2015

    @benja Thank you for the reply! :)

    But I would like to know how it could be done if i replace the a,b,c, with some other names :) like tomato, potato,ginger,potato,tomato :)

  • edited October 2015
    // forum.processing.org/two/discussion/13178/
    // replacing-an-array-of-strings-with-integer
    
    // GoToLoop (2015-Oct-22)
    
    static final String[] VEGS = {
      "tomato", "potato", "ginger", "sweet potato", "camu-camu", "açaí"
    };
    
    static final int LEN = VEGS.length;
    final int[][] wordVals = new int[LEN][];
    
    void setup() {
      for (int i = 0; i != LEN; wordVals[i] = abc123(VEGS[i++]));
      for (int i = 0; i != LEN; println(i), println(wordVals[i++]));
      exit();
    }
    
    static final int[] abc123(String word) {
      final char[] letters = word.toLowerCase().toCharArray();
      final int len = letters.length, values[] = new int[len];
    
      for (int i = 0; i != len; ++i) {
        final int val = letters[i] - 'a';
        values[i] = val >= 0 & val <= 'z' - 'a' + 1? val : -1;
      }
    
      return values;
    }
    
  • Answer ✓

    Ok, then the approach with an IntDict would work.

    String [] names = {"tomato", "potato","ginger","potato","tomato"};
    IntDict dict;
    int[] numbers;
    
    void setup() {
      dict = new IntDict();
    
      // iterate over names-array
      for (int i =0; i<names.length; i++) {
        // add new key to dictionary, if it doesn't exist
        if ( !dict.hasKey(names[i])) {
          dict.add(names[i], dict.size());
        }
      }
    
      // create int-array with same length as names-array
      numbers = new int[names.length]; 
    
      // assign values to int-array
      for (int i= 0; i< names.length; i++) {
        numbers[i] = dict.get(names[i]);
      }
    
      // print content of int-array
      printArray(numbers);
    }
    

    I don't know what name should be mapped to what number, at the moment the first occurence in names has the lowest number.

  • Answer ✓

    Or could use a HashMap

    import java.util.*;
    
    HashMap<String, Integer> dict = new HashMap<String, Integer>();
    
    String s = "apple pear orange orange pomegranite apple grape";
    String[] words;
    int[] wordnbr;
    
    void setup() {
      String[] words = split(s, " ");
      int[] wordnbr = new int[words.length];
      // Number words
      for (int i = 0; i < words.length; i++) {
        if (dict.containsKey(words[i])) {
          wordnbr[i] = dict.get(words[i]);
        } else {
          wordnbr[i] = dict.size();
          dict.put(words[i], wordnbr[i]);
        }
      }
      // Show dictionary
      for (String word : dict.keySet()) {
        println(word + " " + dict.get(word));
      }
      println();
      // show int array
      for (int i = 0; i < wordnbr.length; i++)
        print(wordnbr[i] + " ");
      println();
    }
    
  • edited October 2015

    @benja and @quark. Thanks a lot! :)

    This is exactly what i wanted :) :)

    Thank you @GoToLoop

Sign In or Register to comment.