get word after offset ?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
get word after offset ?
Hi to all,
I've a variable with text in this form:
"item1=value item2=value item3=value
item4="another value"
I need to get values of several items and I searched if exists a function inside livecode that does the job ( something like get the end of the word after "item1=" ) without luck. Before I begin to code a solution, do you know if something has been done in Livecode ? Do you have in mind an example or howto where this problem is solved?
Thanks
I've a variable with text in this form:
"item1=value item2=value item3=value
item4="another value"
I need to get values of several items and I searched if exists a function inside livecode that does the job ( something like get the end of the word after "item1=" ) without luck. Before I begin to code a solution, do you know if something has been done in Livecode ? Do you have in mind an example or howto where this problem is solved?
Thanks
-
- Livecode Opensource Backer
- Posts: 1336
- Joined: Sun Jul 12, 2009 10:53 am
- Location: Bordeaux, France
Re: get word after offset ?
Not too sure what you are wanting to do here...
In the above example, there are four items in the variable theList... You can then query theList anyway that you wish...
the handler above would return :-
January,February,March,April
April
rch
Code: Select all
on mouseUp
put "January,February,March,April" into theList
put theList & cr & item 4 of theList & cr & char 3 to (the number of chars of item 3 of theList) of item 3 of theList
end mouseUp
the handler above would return :-
January,February,March,April
April
rch
Re: get word after offset ?
Thanks for your answer and your example Dixie.
The problem that I try to solve is a little more complicated. Example:
I've in a field the following text content:
"example1=12345 example2=98765 example3=abcde
example4=other"
I want the string after example1 and example3 ( in this case 12345 and abcde )
By now I'mk trying to develop a function that will do the following (pseudocode)
The problem that I try to solve is a little more complicated. Example:
I've in a field the following text content:
"example1=12345 example2=98765 example3=abcde
example4=other"
I want the string after example1 and example3 ( in this case 12345 and abcde )
By now I'mk trying to develop a function that will do the following (pseudocode)
Code: Select all
function nextwordafter alltext, sstring
initial = get the first position of sstring inside alltext
last = get the length of sstring
initial = initial + last
-- now we know where to begin
While character is not enter or space or endoffile do
add the character in position initial inside alltext inside subtext
initial = initial + 1
End while
return subtext
Re: get word after offset ?
It may depend a lot more on the consistency of the data string but
item 2 and 4 relate to the values of elements 1 and 3 since the itemdel has been set to strip off the lead chars in a dummy itemset the itemDelimiter to "="
put word 1 of item 2 of the string && word 1 of item 4 of theString
-
- Livecode Opensource Backer
- Posts: 1336
- Joined: Sun Jul 12, 2009 10:53 am
- Location: Bordeaux, France
Re: get word after offset ?
SparkOut... you beat me to it.. I was sat wondering how to explain what you summed up in the phrase 'It may depend a lot more on the consistency of the data string'... which is the op's problem here...
Re: get word after offset ?
Thanks! It's a great advance, 'cause if i can get the data and make a substring copy with the desired item as the begining of the substring, the value will be always the second item
-
- VIP Livecode Opensource Backer
- Posts: 9752
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: get word after offset ?
What everyone said.
But it is good practice to set up your data more formally early on, that is, to add reliable delimiters in reliable places. in your first post, you might have delimited by spaces, thus being able to count and identify words. This is not usually a good idea. Better to use commas, returns, or perhaps a special character of your choice to separate the data for later parsing.
Further, just to make my point, in your very first post you had an additional quote placed in the last word. This was removed in a later post. It just shows how text can change, or be loosely formed, and that this can throw you for a loop.
Better, (since I assume you created the original text) to have formed it like:
Craig Newman
But it is good practice to set up your data more formally early on, that is, to add reliable delimiters in reliable places. in your first post, you might have delimited by spaces, thus being able to count and identify words. This is not usually a good idea. Better to use commas, returns, or perhaps a special character of your choice to separate the data for later parsing.
Further, just to make my point, in your very first post you had an additional quote placed in the last word. This was removed in a later post. It just shows how text can change, or be loosely formed, and that this can throw you for a loop.
Better, (since I assume you created the original text) to have formed it like:
Here is another point. Were all four of your "items" intentionally set to the same "value"? The last example separates these. And the naming scheme itself becomes simple to parse, since there are many ways to access the last char of each element of the data, which is a number, and therefore easy to isolate and count. This is just an example of the preparation I spoke of at the beginning. It makes life easier at the end.item1 = value1
item2 = value2
item3 = value3
item4 = value4
Craig Newman
Re: get word after offset ?
Thanks foru your answer Craig.
I'm totally agree with you, unfortunately the data comes from a live system "as is". It's a router and I access it throught ssh. Example of returned data :
This is stored in livecode variable as
I wanna take the values of uptime-used, download-used, upload-used and last-seen. The best way until now is to search the word "uptime-used=" and store character from there until space, cr or EOF, repeating the procedure for all searched items. AFAIK, is the only way if the data is not structured.
I'm totally agree with you, unfortunately the data comes from a live system "as is". It's a router and I access it throught ssh. Example of returned data :
Code: Select all
Flags: X - disabled, A - active, I - incomplete
0 ;;; comentario de usuario
customer=admin name="user1" actual-profile="test profile" password="password" location="loctest" shared-users=1 wireless-psk="" wireless-enc-key=""
wireless-enc-algo=none uptime-used=8m31s download-used=12180371 upload-used=1766641 last-seen=feb/09/2015 22:27:27
[camping@hstest2015] >
Flags: X - disabled, A - active, I - incomplete
0 ;;; comentario de usuario
customer=admin name="user1" actual-profile="test profile"
password="password" location="camp_name" shared-users=1
wireless-psk="" wireless-enc-key="" wireless-enc-algo=none
uptime-used=8m31s download-used=12180371 upload-used=1766641
last-seen=feb/09/2015 22:27:27
I wanna take the values of uptime-used, download-used, upload-used and last-seen. The best way until now is to search the word "uptime-used=" and store character from there until space, cr or EOF, repeating the procedure for all searched items. AFAIK, is the only way if the data is not structured.
Re: get word after offset ?
If spaces are always element delimiters and not in the element values, you could set itemDelimiter to "space" first, then create an array or carriage-return-delimited list of elements. Then, set the itemDelimiter to "=" and for each line (or array element), item 1 would be the attribute and item 2 would be the value.
Walt Brown
Omnis traductor traditor
Omnis traductor traditor
Re: get word after offset ?
Thanks WaltBrown for your tip.
Effectively it's a good way to convert it in an organized list of pairs and I note your suggestion ( I will need something like this in few hours )
Effectively it's a good way to convert it in an organized list of pairs and I note your suggestion ( I will need something like this in few hours )
-
- VIP Livecode Opensource Backer
- Posts: 9752
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: get word after offset ?
Hi.
You might say that using any of the various "offset" functions are simply additional tools for parsing data. This is perfectly valid, and can be very powerful, especially if you can skip the first instance to more distant ones. You are explicitly using EOF as the terminating delimiter. All well and good. The trick is to have, as I said, reliable delimiters in reliable places.
Craig
You might say that using any of the various "offset" functions are simply additional tools for parsing data. This is perfectly valid, and can be very powerful, especially if you can skip the first instance to more distant ones. You are explicitly using EOF as the terminating delimiter. All well and good. The trick is to have, as I said, reliable delimiters in reliable places.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 253
- Joined: Fri Dec 09, 2011 11:17 am
- Location: Warszawa / Poland
Re: get word after offset ?
Try something like this:
I didn't check it.
Marek
Code: Select all
local tRegex, tUptime, tDownload, tUpload, tLast
put "^.*?-used=(.+)\s.*?(.+)\s.*?(.+)\s.*?(.+)\s*$ into tRegex
get matchText ("your text", tRegex, tUptime, tDownload, tUpload, tLast)
Marek