# Grabbing 1 Value From a Long Strin

**URL:** <https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [June 27, 2018, 6:18pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647 "2018-06-27T18:18:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![klichtenauer4](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@klichtenauer4](https://forum.inductiveautomation.com/u/klichtenauer4)\
**Post date:** [June 27, 2018, 6:18pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/1 "2018-06-27T18:18:38Z")

</div>

So I am reading in a value from a scale head that is set to continuously output. Right now I have a button that says when to grab a value from the scale. The only problem is that it gives me a long list of what appears to be every value since the last time I pressed the button. For example, I would get something like:

17.9 lbG  
18.6 lbG  
19.2 lbG  
19.2 lbG  
19.2 lbG  
And this could go on for 20 or so more values.

The scripting I have on my button is as follows:  
reading = event.source.parent.getComponent(‘ScaleReadIn’)  
weight = reading.readString()  
print weight

I’m wondering what’s the best way to go about grabbing the very last value? I would use a .split() script but it seems that wouldn’t work if I don’t know how many values there will be. I was thinking about adding all the values to a list and then using something like list[-1]. Does this sound plausible or is there an easier way of going about it?

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [June 27, 2018, 6:34pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/2 "2018-06-27T18:34:46Z")

</div>

Split() creates a list, so

```auto
lastVal = weight.split("\n")[-1]

```

should work fine.

---

<div class="post-metadata">

**Author:** ![zacslade](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacslade/32/3587_2.png) [@zacslade](https://forum.inductiveautomation.com/u/zacslade)\
**Post date:** [June 27, 2018, 6:35pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/3 "2018-06-27T18:35:33Z")

</div>

In python you can get the last few characters from a string using this syntax:

```auto
mystring = "i want to get only the last part"
substring = mystring[-4:]
print substring
#output > "part"
```

---

<div class="post-metadata">

**Author:** ![klichtenauer4](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@klichtenauer4](https://forum.inductiveautomation.com/u/klichtenauer4)\
**Post date:** [June 27, 2018, 6:37pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/4 "2018-06-27T18:37:30Z")

</div>

Good to know. My only problem here would be when a weight goes from, say, 99 lbs to 105 lbs. It would leave off the ‘1’ correct?

---

<div class="post-metadata">

**Author:** ![zacslade](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacslade/32/3587_2.png) [@zacslade](https://forum.inductiveautomation.com/u/zacslade)\
**Post date:** [June 27, 2018, 6:38pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/5 "2018-06-27T18:38:27Z")

</div>

Ah right, So @mrogers 's solution is better 🙂

---

<div class="post-metadata">

**Author:** ![klichtenauer4](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@klichtenauer4](https://forum.inductiveautomation.com/u/klichtenauer4)\
**Post date:** [June 27, 2018, 6:44pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/6 "2018-06-27T18:44:45Z")

</div>

Thank you for the quick response. So where am I going wrong here? This is what I’ve got and I’m just getting a blank output.

reading = event.source.parent.getComponent(‘ScaleReadIn’)  
weight = reading.readString()

finalWeight = weight.split(“G”)[-1]

print finalWeight

Edit: i figured it out. The last line should be  
print finalWeight[-1]

instead of the [-1] where I originally had it. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [June 27, 2018, 6:51pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/7 "2018-06-27T18:51:49Z")

</div>

Is it possible `weight` is blank?

_Tip: put triple reverse quote / grave marks (often to left of “1” on keyboard) before and after code to make it easy to read on forum._

---

<div class="post-metadata">

**Author:** ![klichtenauer4](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@klichtenauer4](https://forum.inductiveautomation.com/u/klichtenauer4)\
**Post date:** [June 27, 2018, 7:50pm UTC](https://forum.inductiveautomation.com/t/grabbing-1-value-from-a-long-strin/18647/8 "2018-06-27T19:50:28Z")

</div>

Thanks for the tip. I was wondering how to do that.
