itemdelimiter?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 58
- Joined: Sat Jun 15, 2019 10:09 am
itemdelimiter?
Hi Anyone
I have a minor problem that I can't seem to work out?:
The background is fairly unimportant, but basically I get make an API call and get various bits of data back. One bit is a long string with the date/time etc... (2019-08-11T20:17+0000). All I want to do is select the 20.17 bit, because I don't need the rest.
The bit I'm stuck on is how to this without the use of a field or anything?
so the string comes back, I put it in a variable named t_time, I then want to chop the two ends of and save it as say t_time2.
It seems obvious to use the itemDelim function to select for aftert the "T" and before the "+", but I can't work out the syntax.
I tried (badly) to use a command to get the variable and then set the itemDelim, but no joy.
As ever, any help very much appreciated.
Kind regards
Paul
I have a minor problem that I can't seem to work out?:
The background is fairly unimportant, but basically I get make an API call and get various bits of data back. One bit is a long string with the date/time etc... (2019-08-11T20:17+0000). All I want to do is select the 20.17 bit, because I don't need the rest.
The bit I'm stuck on is how to this without the use of a field or anything?
so the string comes back, I put it in a variable named t_time, I then want to chop the two ends of and save it as say t_time2.
It seems obvious to use the itemDelim function to select for aftert the "T" and before the "+", but I can't work out the syntax.
I tried (badly) to use a command to get the variable and then set the itemDelim, but no joy.
As ever, any help very much appreciated.
Kind regards
Paul
Re: itemdelimiter?
Hi Paul,
maybe you mean something like this?
Best
Klaus
maybe you mean something like this?
Code: Select all
...
put "(2019-08-11T20:17+0000)" into t_time
set itemdel to "T"
put item 2 of t_time into your_time
set itemdel to "+"
delete item 2 of your_time
## -> your_time = 20:17
## You get the picture :-)
...
Klaus
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: itemdelimiter?
What Klaus said, clean and direct, and which is how I would do it.
Just to complicate things, and not overuse the poor itemDel, you could also:
I only offer this because i have nothing better to do, and to show another way to parse a string. The "offset" family of functions is almost as useful as the itemDel.
Craig
Just to complicate things, and not overuse the poor itemDel, you could also:
Code: Select all
on mouseup
get "(2019-08-11T20:17+0000)"
delete char offset("+",it) to the number of chars of it of it
delete char 1 to offset("T",it) of it
answer it
end mouseup
Craig
Re: itemdelimiter?
OK, for the sake of the poor itemdel and even a tad shorter:
Code: Select all
...
put "(2019-08-11T20:17+0000)" into t_time
set itemdel to ":"
put char -2 to -1 of item 1 of t_time & ":" & char 1 to 2 of item 2 of t_time into your_time
...
Re: itemdelimiter?
Klaus's solution maybe a tad cleaned up:
Code: Select all
put "(2019-08-11T20:17+0000)" into t_time
set the itemDelimiter to "T"
put character 1 to 5 of item 2 of t_time into field 1
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: itemdelimiter?
Paul.
Well, you did ask for it, you know.
The point of these shenanigans is that LiveCode can be twisted in many ways, This is a good thing. If you are really industrious, examine just a bit what these handlers are doing. There are a handful of cute lessons sprinkled throughout.
Craig
Well, you did ask for it, you know.
The point of these shenanigans is that LiveCode can be twisted in many ways, This is a good thing. If you are really industrious, examine just a bit what these handlers are doing. There are a handful of cute lessons sprinkled throughout.
Craig
Re: itemdelimiter?
Hi,
is there a special reason to use itemdel at all?
Why not just grab "char 12 to 17 of twhatEver"?
Have fun!
is there a special reason to use itemdel at all?
Why not just grab "char 12 to 17 of twhatEver"?
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: itemdelimiter?
Certainly, if the number of chars preceding the string of interest is always the same. Even someone as lazy as I would feel uncomfortable with that. I have made such assumptions on my own string parsing travails, and lived to regret it.is there a special reason to use itemdel at all?
Why not just grab "char 12 to 17 of twhatEver"?
Craig
Re: itemdelimiter?
Much as Craig says, although the format shouldn't change on that system, I believe it could be drawn from the system settings much like Lc's date is.
However, the time indicator ("T") is almost certainly going to be in there no matter the system preferences. The same goes for the colon Klaus used, ditto for Craig's solution of marking the ("+") as well as the ("T") , pretty sure the "+" is GMT.
This means that the date could be in several formats, including a 2 digit year, depending on how the system settings are set.Dictionary wrote:if you specify the system date, the times returned by the date function are formatted according to the user's system preferences.
However, the time indicator ("T") is almost certainly going to be in there no matter the system preferences. The same goes for the colon Klaus used, ditto for Craig's solution of marking the ("+") as well as the ("T") , pretty sure the "+" is GMT.
Re: itemdelimiter?
Hi,
this should work for most cases:
Have fun!
this should work for most cases:
Code: Select all
on mouseUp
ask "Gimmi a date string:" with "2019-08-11T20:17+0000"
if it is empty then
exit mouseUp
else
put it into myStrg
end if
get getTimeFromString(myStrg) -- see function below
if it begins with "error" then
answer error quote & myStrg & quote & " isn't a string that I can handle"
exit mouseUp
else
put "String is " & myStrg & CR & "Date is " & it
end if
end mouseUp
function getTimeFromString theDateStrg
put offset(":", theDateStrg) into myOffset
if myOffset = 0 then return "error" -- there's no ":", so complain
put char myOffset -2 to myOffset +2 of theDateStrg into myDate
repeat for each char C in myDate
if (C is a number) OR (C = ":") then -- test if it's a date
next repeat
else
return "error" -- if not, complain
end if
end repeat
return myDate
end getTimeFromString
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: itemdelimiter?
AxWald.
You must be wary of typos. "Gimmi" is always spelled "gimmie".
Craig
You must be wary of typos. "Gimmi" is always spelled "gimmie".
Craig
Re: itemdelimiter?
Not in the Queen's English. It should be "gimme". Of course you Americans must have changed it so as not to mistakenly pronounce it "gim" or even try to Frenchify it and say "zhimm".
Mind you I expect Richmond would have another flavour. But not flavor.
Mind you I expect Richmond would have another flavour. But not flavor.
Re: itemdelimiter?
Well you can kick it out of my boot and colour me surprised Now I need to take a lift to fetch my spanner...
-
- Posts: 58
- Joined: Sat Jun 15, 2019 10:09 am
Re: itemdelimiter?
Wow, thanks "anyone" I so should have started using this forum earlier, I might have seen daylight occasionally then!
Kind regards
Paul
Kind regards
Paul
Re: itemdelimiter?
Well Paul,
Long as you can stand the bad jokes and informal back and forth, your welcome here anytime
Long as you can stand the bad jokes and informal back and forth, your welcome here anytime