Jump to content

Extract values from string


odinhg

Recommended Posts

Hi, I'm doing some serial communication with a device which sends a string in return. This string contains different values that I want to split into separate variables.

The returned string can look something like this:

1: "1,HS,OK,12550,423,0.40,0,0\r"

2: "1,HS,OK,35000,568,1.32,11,14.4\r"

So what I want to do is to get the values something like this:

Int, String, String, Int, Int, Double, Int, Double

I was thinking regexp is what I'm looking for, but unfortunately I'm not very good with regexp, and maybe there is some other ways which is more efficient.

I would be very grateful for any tips to how to solve this!

Thanks in advance   :worshippy:

Edited by odinhg
Link to comment

Well. There are a few ways.

If the message string is always in a fixed order and fixed length and messages are not concatenated, you can use the "Scan From String" which will give you the results straight away in the format you require.

If its not, then you can use the "Spreadsheet String To Array" to break up the string at the delimiters then convert to whatever formats you like.

  • Like 1
Link to comment

Thanks for the quick reply ShaunR!

The message string does always have the same number of values but its values aren't padded so the total length of the string can differ from time to time.

Using "1,HS,%2s,%d,%d,%d,%d,%f" as format string did work when using the "Scan from string", except the last float, which just gave me an integer (for example 1.4 became 1 after the scan).

When changing the 6th parameter to float, "1,HS,%2s,%d,%d,%f,%d,%f", I get an error saying that the input string wasn't in the expected format.

The actual input string when the error message shows up is "1,HS,OK,0,21,0,0,1\r".

Thanks again!  :)

Link to comment

Thanks for the quick reply ShaunR!

The message string does always have the same number of values but its values aren't padded so the total length of the string can differ from time to time.

Using "1,HS,%2s,%d,%d,%d,%d,%f" as format string did work when using the "Scan from string", except the last float, which just gave me an integer (for example 1.4 became 1 after the scan).

When changing the 6th parameter to float, "1,HS,%2s,%d,%d,%f,%d,%f", I get an error saying that the input string wasn't in the expected format.

The actual input string when the error message shows up is "1,HS,OK,0,21,0,0,1\r".

Thanks again! :)

Thats because the number of format specifiers have to be exactly the same as the fields you are trying to extract. From your last example, the last digit is 1. If you are viewing the value in a digital indicator and "hide trailing 0" is set, you will only see the decimal places IF the digits are non-zero.

If thats not the reason and you want that to be 1.00 I would suggest using %.2f as the format specifier.

Link to comment

Thats because the number of format specifiers have to be exactly the same as the fields you are trying to extract. From your last example, the last digit is 1. If you are viewing the value in a digital indicator and "hide trailing 0" is set, you will only see the decimal places IF the digits are non-zero.

If thats not the reason and you want that to be 1.00 I would suggest using %.2f as the format specifier.

It could also mean you have a different decimal sign ('.' vs ','). use %.; in the start of your formatter to specify a dot as the decimal sign.

Ton

  • Like 1
Link to comment

I finally got it working. It's looks a little 'hacky' and it's most likely not the most efficient way to do it. But it's good enough for me atm.

exstring.png

Thanks again Shaun and Ton :)

Cool. Glad its working Looks like you've been through the string pallet and now know every function :P.

(You can replace everything up to the "Array to Cluster" with a "Spreadsheet String To Array")

Link to comment

I'm not trying to get anyone to change their implementation once they have it working, but I fail to see how the "top" is clearer and more efficient than the "bottom". Both give the same result. If this is always the format of your data (header, string, int, int, float, int, float) then the bottom "scan" should work fine.

I do agree that you certainly get to know the string palette well.

-Scott

post-415-125553777765_thumb.png

  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.