get word after offset ?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

get word after offset ?

Post by dbm » Mon Feb 23, 2015 8:27 am

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

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: get word after offset ?

Post by Dixie » Mon Feb 23, 2015 10:58 am

Not too sure what you are wanting to do here...

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
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

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: get word after offset ?

Post by dbm » Mon Feb 23, 2015 11:38 am

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)

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

SparkOut
Posts: 2857
Joined: Sun Sep 23, 2007 4:58 pm

Re: get word after offset ?

Post by SparkOut » Mon Feb 23, 2015 11:45 am

It may depend a lot more on the consistency of the data string but
set the itemDelimiter to "="
put word 1 of item 2 of the string && word 1 of item 4 of theString
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 item

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: get word after offset ?

Post by Dixie » Mon Feb 23, 2015 11:53 am

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...

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: get word after offset ?

Post by dbm » Mon Feb 23, 2015 12:10 pm

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 :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: get word after offset ?

Post by dunbarx » Mon Feb 23, 2015 4:10 pm

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:
item1 = value1
item2 = value2
item3 = value3
item4 = value4
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.

Craig Newman

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: get word after offset ?

Post by dbm » Mon Feb 23, 2015 5:11 pm

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 :

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] >
This is stored in livecode variable as
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.

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: get word after offset ?

Post by WaltBrown » Mon Feb 23, 2015 5:45 pm

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

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: get word after offset ?

Post by dbm » Mon Feb 23, 2015 6:27 pm

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 ;) )

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9752
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: get word after offset ?

Post by dunbarx » Mon Feb 23, 2015 7:52 pm

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

dbm
Posts: 21
Joined: Tue Feb 17, 2015 1:12 pm

Re: get word after offset ?

Post by dbm » Wed Feb 25, 2015 1:55 pm


snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am
Location: Warszawa / Poland

Re: get word after offset ?

Post by snm » Wed Feb 25, 2015 10:41 pm

Try something like this:

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)
I didn't check it.

Marek

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”