Page 1 of 2
Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 6:09 pm
by dbm
Hi again,
I'm searching in forums and google but in the name of $DEITY I don't know how to ask them correctly to get an answer.
I'm trying to get the substring of a given text, begining from a desired position. In other words:
If I've a text like this: "abc def ghi abc dde jkman de" I wanna all text starting from "def"
I don't know if exists in LC a predefined way to do it. By now I could do:
Get the position of the first occurence of "def" using the offset
walk from this position storing each char until the end of text
But How I say in LC "until the end of text" ?
Re: Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 6:33 pm
by Dixie
Using your example string...
Code: Select all
on mouseUp
put "abc def ghi abc dde jkman de" into theString
put wordOffset("def",theString) into wordPosition
put word wordPosition to -1 of theString
end mouseUp
Re: Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 6:43 pm
by dbm
I'm using your example as source, but seems that I'm doing it bad.
I've created this funtion:
Code: Select all
function wordafter letext leword ledelim
local pos, rtext
put empty into pos
put empty into rtext
put wordoffset(leword,letext) into pos
if pos is not 0 then
put word pos to -1 of letext into rtext
set the itemDelimiter to "="
--set the itemDelimiter to ledelim
put word 2 of rtext into rtext
Else
put empty into rtext
end if
return rtext
end wordafter
This function is called like this:
Code: Select all
put wordafter(ttest,"name=","=") into field cdebug
Even If I force to use the "=" literaly as delimiter, the line
put word 2 of rtext into rtext
returns the word as it is count using space delimiter.
Why uses the space delimiter after I forced to use the "=" delimiter?
Sorry for all this beginner questions
Re: Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 8:38 pm
by Dixie
Why uses the space delimiter after I forced to use the "=" delimiter?
I don't know ?... Why is "=" the delimeter ?... give an example of the string you are trying to parse
Re: Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 9:00 pm
by dbm
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
Re: Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 9:13 pm
by Dixie
Ok... What are you wanting to return from your function ?
Re: Getting string from X position until the end of text
Posted: Mon Feb 23, 2015 10:59 pm
by dbm
For example, we have the following text :
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
and in this example we want the value after the "uptime-used", but we can want any other value, the only thing that we can know is the identifier of the value (location, shared-users, wireless-enc-key...)
I want create a generic function that giving a text, a sting to search and a delimiter, returns the word after the string to search.
the call function in this example can be
wordafter(ttest,"uptime-used=","=") ( where ttest is a variable containing the example text, "uptime-used=" is the identifier to locate the value and "=" is the delimiter that must be used)
Actually, the code of my function wordafter, does the following:
Code: Select all
function wordafter letext leword ledelim ( letext -> text containing data, leword -> identifier to find, ledelim -> delimiter to be used to split in words)
-- variables to be used as index and to store text
local pos, rtext
put empty into pos
put empty into rtext
-- we get the first position of the identifier inside the text
put wordoffset(leword,letext) into pos
-- if found
if pos is not 0 then
-- copy all text from the identifier until the end of text ( then the identifier becomes the facto the first word in the copied text )
put word pos to -1 of letext into rtext
-- we force the delimiter to be "=" ( then the value we are searching for will be the 2nd word of the copied text
set the itemDelimiter to "="
--set the itemDelimiter to ledelim
-- we get the second word of the copied text ( the value of the identifier )
put word 2 of rtext into rtext
Else
put empty into rtext
end if
return rtext
end wordafter
In the example above:
put wordafter(ttest,"uptime-used","=") into field cdebug
It should return into cdebug the text "8m31s" but actually returns "download-used=12180371". In fact, the line
put word 2 of rtext into rtext is using space as delimiter, when I forced to use "=", or I think so.
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 9:05 am
by dbm
I think that the problem is here:
"By default, items are delimited by commas. You can change the default comma to create your own chunk type by setting the itemDelimiter property to any character. For example:
on mouseUp
set the itemDelimiter to ":"
answer item 7 of field "text"
end mouseUp
An item can contain characters or words, but not lines.
Items can be contained in a line, but not in a word or character."
In my text, from the point of view of LC, the 2 itmes are a word and setdelimiter is not applied, then?
If I convert the text before, to have an space before and after each "=" then It should work, but I don't like very much this solution 'cause it will have problems returning data from a pair of fields like
identifier="text value"
By now I changed my function and it returns de desired value when the pair of fields are in the form identifier=value
Code: Select all
function wordafter letext leword ledelim
local pos, rtext
put empty into pos
put empty into rtext
put wordoffset(leword,letext) into pos
if pos is not 0 then
put word pos to -1 of letext into rtext
replace ledelim with space & ledelim & space in rtext
put word 3 of rtext into rtext
Else
put empty into rtext
end if
return rtext
end wordafter
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 9:21 am
by Dixie
The function below will work, using the text that you supplied... as long as the returned value is one whole string, which it is in all cases except :-
actual-profile="test profile" ...
Code: Select all
on mouseUp
put fld 1 into theString
put "download-used" into theValue
put "=" into theDelim
put wordAfter( theString,theValue,theDelim)
end mouseUp
function wordAfter theString, theValue theDelim
set the itemDel to theDelim
put itemOffset(theValue,theString) into theItemNo
put item theItemNo + 1 of theString into temp
set the itemDel to space
return item 1 of temp of theString
end wordAfter
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 9:33 am
by dbm
You're right Dixie, is what I say in my last post. I'm working to find a way to be able to process all possibilities identifier=value and identifier="text value"
Thanks for the optimisation. I learn a lot.
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 12:02 pm
by Thierry
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
Hello dbm,
I've made a two-line-script which extract nicely the key and associated value.
However, it appears that there is one inconsistency in this set of data.
For the key
"last-seen", its value (
feb/09/2015 22:27:27) should be quoted.
Can you modify your data?
Or can you confirm that it will *always* be the last item of your dataSet?
Both cases will set the complexity to none.
Regards,
Thierry
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 5:20 pm
by dbm
Merci monsieur Duez!
Thanks for your answer.
The data is chaotic and effectively in a coherent world the value of "last-seen" should be quoted 'cause is a value composed from 2 alphanumeric strings, but in this case, the data returned by the router for the identifier "last-seen" comes in the format
date time without quotes.
In this case yes, the "last-seen" is the last value pair in the string, but is not the same case always (see example from another answer from router):
0 customer=admin user="user1" nas-port=2147483656 nas-port-type=ethernet nas-port-id="ether1" calling-station-id="00:1D:72:8E:C0:35" user-ip=10.0.1.194 host-ip=127.0.0.1
status=start,stop,interim from-time=feb/09/2015 22:11:07 till-time=feb/09/2015 22:13:49 terminate-cause=user-request uptime=2m42s download=71441 upload=27396
Could you kindly share this two line script? I'm very curious!
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 6:16 pm
by jiml
You might try capturing the key value pairs in an array.
Code: Select all
function getVariables theText
delete line 1 to lineoffset("=",theText) of theText
repeat with x = the number of words of theText down to 1
if word x of theText contains "=" then put cr before word x of theText
end repeat
filter theText without ""
replace quote with empty in theText
split theText with cr and "="
return theText
end getVariables
If you pass the text you shared:
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
you'll end up with an array with these keys:
wireless-enc-algo
uptime-used
password
last-seen
download-used
wireless-psk
location
upload-used
shared-users
wireless-enc-key
each key having its value.
Jim Lambert
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 6:18 pm
by jiml
Slight correction based on your other text examples
Code: Select all
function getVariables theText
if "=" is not in line 1 of theText then delete line 1 to lineoffset("=",theText) of theText
Re: Getting string from X position until the end of text
Posted: Tue Feb 24, 2015 6:30 pm
by jiml
better
Code: Select all
function getVariables theText
repeat with x = the number of words of theText down to 1
if word x of theText contains "=" then
put cr before word x of theText
else
delete word x of theText
end if
end repeat
filter theText without ""
replace quote with empty in theText
split theText with cr and "="
return theText
end getVariables