# How to extract values from a PVector?

**URL:** https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778
**Category:** Electronics (Arduino, etc.)
**Created:** [November 28, 2020, 12:58am UTC](https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778 "2020-11-28T00:58:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [November 28, 2020, 12:58am UTC](https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778/1 "2020-11-28T00:58:48Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I am using the leapmotion library and some values are coming in a PVector.  
They print out fine, but when I put them in a string to be sent to Arduino I do get brackes and commas…

String HandOut = (“H”+ handPosition + " " + handRoll+" “+handPitch+” “+handPitch +” "+handPinch)  
puts out  
H[539.59766, 413.71576, 23.220325] -13.140116 49.55902 49.55902 0.76153696

This is very hard to parse in values on the Arduino side.  
The "handPostion is defined as a Pvector and the rest are just floats.  
PVector handStabilized = hand.getStabilizedPosition();

What to do?

Thanks a lot,  
Mitch

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 28, 2020, 1:53am UTC](https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778/2 "2020-11-28T01:53:36Z")

</div>

> [@laptophead](#):
>
> This is very hard to parse in values on the Arduino side.

You can create some function which **replace()** the undesirable characters w/ a common delimiter 1. 💡

For example the util function **toTsvPVector()** below go w/ dilimeter character `'\t'` 🙃:

> **[Tab-separated values](https://en.wikipedia.org/wiki/Tab-separated_values)**
>
> Tab-separated values (TSV) is a simple, text-based file format for storing tabular data. Records are separated by newlines, and values within a record are separated by tab characters. The TSV format is thus a delimiter-separated values format, similar to comma-separated values.
> TSV is a simple file format that is widely supported, so it is often used in data exchange to move tabular data between different computer programs that support the format. For example, a TSV file might be used to transfe...

```auto
void setup() {
  final PVector vec = PVector.random3D(this).mult(100);
  println(vec);
  println(toTsvPVector(vec));
  exit();
}

static final String toTsvPVector(final PVector v) {
  return v.toString().replace("[", "").replace("]", "").replace(", ", "\t");
}

```

> [@laptophead](#):
>
> `String HandOut = (“H”+ handPosition + " " + handRoll+" “+handPitch+” “+handPitch +” "+handPinch)`

You can simply pick character TAB as the delimiter for all those 5 sent variables: 😀  
final String tsv = toTsvPVector(hand.getStabilizedPosition()) + TAB + handRoll + TAB + handPitch + TAB + handPitch + TAB + handPitch + ENTER;

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [November 28, 2020, 2:53am UTC](https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778/3 "2020-11-28T02:53:26Z")

</div>

yey, that worked.  
Thanks a lot. Now I am faced with too many decimals. I could round the lone floats, but what to do about the vals inside the PVector?

the final String tsv = toTsvPVector(hand.getPosition()) + " "  
+ round(handRoll) +" " + round(handPitch) + " " + round (handYaw) + " " + round (handPinch\*100) + ‘\n’;  
gives out:

985.884 371.61603 54.546585 2 41 -10 86

PS. Thanks for the TAB but I am using spacebar to delimitate.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 28, 2020, 4:46am UTC](https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778/4 "2020-11-28T04:46:18Z")

</div>

> [@laptophead](#):
>
> PS. Thanks for the TAB but I am using spacebar to delimitate.

You can choose whatever character as the separator delimiter as long as you stick to it:

> **[Delimiter-separated values](https://en.wikipedia.org/wiki/Delimiter-separated_values)**
>
> Formats that use delimiter-separated values (also DSV): 113  store two-dimensional arrays of data by separating the values in each row with specific delimiter characters. Most database and spreadsheet programs are able to read or save data in a delimited format. Due to their wide support, DSV files can be used in data exchange among many applications.
> A delimited text file is a text file used to store data, in which each line represents a single book, company, or other thing, and each line has f...

```auto
void setup() {
  final PVector vec = PVector.random3D(this).mult(100);
  println(vec);
  println(toDsvPVector(vec));
  println(toDsvPVector(vec, " "));
  println(toDsvPVector(vec, ","));
  exit();
}

static final String toDsvPVector(final PVector v) {
  return toDsvPVector(v, "\t");
}

static final String toDsvPVector(final PVector v, final String delim) {
  return v.toString().replace("[", "").replace("]", "").replace(", ", delim);
}

```

> [@laptophead](#):
>
> I could **round()** the lone floats, but what to do about the vals inside the PVector?

You can’t change its 3 `float` primitive datatype values!  
You’re gonna need another container for it; for example an `int[]` array.  
BtW you can grab a `float[]` outta a PVector via its **array()** method:

> **[array() / Reference](https://processing.org/reference/PVector_array_.html)**
>
> Return a representation of this vector as a float array. This is only for temporary use. If used in any other fashion, the contents should be copied by using the copy() method to copy into your…

After that you just need another util function to convert that `float[]` to a rounded `int[]`.  
Then use Arrays.**toString()** to convert the whole array to a String and use some other String methods like **substring()** & **replace()**:  
[Arrays (Java SE 11 & JDK 11 )](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#toString(int%5B%5D))  
[String (Java SE 11 & JDK 11 )](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#substring(int,int))

```auto
// Discourse.Processing.org/t/how-to-extract-values-from-a-pvector/25778/4
// GoToLoop (2020/Nov/28)

import java.util.Arrays;

void setup() {
  final PVector vec = PVector.random3D(this).mult(100);
  final int[] roundedVec = roundedArray(vec.array());
  println(vec);

  final String tsv = toDsvArr(roundedVec);
  println(tsv);

  final String dsv = toDsvArr(" ", roundedVec);
  println(dsv);

  final String csv = toDsvArr(",", roundedVec);
  println(csv);

  exit();
}

@SafeVarargs static final int[] roundedArray(final float... arr) {
  final int[] rounded = new int[arr.length];
  for (int i = 0; i < arr.length; rounded[i] = round(arr[i++]));
  return rounded;
}

@SafeVarargs static final String toDsvArr(final int... arr) {
  return toDsvArr("\t", arr);
}

@SafeVarargs static final String toDsvArr(final String sep, final int... arr) {
  final String arrStr = Arrays.toString(arr);
  return arrStr.substring(1, arrStr.length() - 1).replace(", ", sep);
}

```

You can use **join()** in place of Arrays.**toString()**. That’d be easier now that I’ve checked that out: 😉

> **[join() / Reference](https://processing.org/reference/join_.html)**
>
> Combines an array of Strings into one String, each separated by the character(s) used for the separator parameter. To join arrays of ints or floats, it's necessary to first convert them to Stri…

```auto
@SafeVarargs static final String toDsvArr(final String sep, final int... arr) {
  return join(str(arr), sep);
}

```

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [November 28, 2020, 7:17pm UTC](https://discourse.processing.org/t/how-to-extract-values-from-a-pvector/25778/5 "2020-11-28T19:17:31Z")

</div>

Thanks a lot,  
I ended up with

```auto
PVector v = hand.getPosition();
float[] f = v.array();
int X= round(f[0]); 
int Y= round(f[1]); 
int Z= round(f[2]); 
  
     final String tsv = "H"+X + " "+Y + " "+Z + " "
+ round(handRoll) +" " + round(handPitch) + " " + round (handYaw) + " " + round (handPinch*100) + '\n';
   

 println (tsv);

```

and I am very happy.
