itemdelimiter?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

paulalsmith1000
Posts: 58
Joined: Sat Jun 15, 2019 10:09 am

itemdelimiter?

Post by paulalsmith1000 »

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
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: itemdelimiter?

Post by Klaus »

Hi Paul,

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

Klaus
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10510
Joined: Wed May 06, 2009 2:28 pm

Re: itemdelimiter?

Post by dunbarx »

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:

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
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
Klaus
Posts: 14325
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: itemdelimiter?

Post by Klaus »

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
...
:D
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: itemdelimiter?

Post by bogs »

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
Clean up in aisle 3....
Clean up in aisle 3....
Image
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10510
Joined: Wed May 06, 2009 2:28 pm

Re: itemdelimiter?

Post by dunbarx »

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
AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: itemdelimiter?

Post by AxWald »

Hi,

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!
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10510
Joined: Wed May 06, 2009 2:28 pm

Re: itemdelimiter?

Post by dunbarx »

is there a special reason to use itemdel at all?
Why not just grab "char 12 to 17 of twhatEver"?
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.

Craig
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: itemdelimiter?

Post by bogs »

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.
Dictionary wrote:if you specify the system date, the times returned by the date function are formatted according to the user's system preferences.
This means that the date could be in several formats, including a 2 digit year, depending on how the system settings are set.

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.
Image
AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: itemdelimiter?

Post by AxWald »

Hi,

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
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!
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10510
Joined: Wed May 06, 2009 2:28 pm

Re: itemdelimiter?

Post by dunbarx »

AxWald.

You must be wary of typos. "Gimmi" is always spelled "gimmie".

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

Re: itemdelimiter?

Post by SparkOut »

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. :lol:
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: itemdelimiter?

Post by bogs »

Well you can kick it out of my boot and colour me surprised :twisted: Now I need to take a lift to fetch my spanner...
Image
paulalsmith1000
Posts: 58
Joined: Sat Jun 15, 2019 10:09 am

Re: itemdelimiter?

Post by paulalsmith1000 »

Wow, thanks "anyone" I so should have started using this forum earlier, I might have seen daylight occasionally then!

Kind regards

Paul
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: itemdelimiter?

Post by bogs »

Well Paul,

Long as you can stand the bad jokes and informal back and forth, your welcome here anytime :D
Image
Post Reply