Page 1 of 2

itemdelimiter?

Posted: Sun Aug 11, 2019 12:56 pm
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

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 1:13 pm
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

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 2:07 pm
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

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 2:27 pm
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

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 3:50 pm
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
tTimeExample.png
Clean up in aisle 3....

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 4:27 pm
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

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 6:01 pm
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!

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 8:03 pm
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

Re: itemdelimiter?

Posted: Sun Aug 11, 2019 9:16 pm
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.

Re: itemdelimiter?

Posted: Mon Aug 12, 2019 1:19 pm
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!

Re: itemdelimiter?

Posted: Mon Aug 12, 2019 4:24 pm
by dunbarx
AxWald.

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

Craig

Re: itemdelimiter?

Posted: Mon Aug 12, 2019 5:02 pm
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:

Re: itemdelimiter?

Posted: Mon Aug 12, 2019 10:12 pm
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...

Re: itemdelimiter?

Posted: Mon Aug 12, 2019 10:25 pm
by paulalsmith1000
Wow, thanks "anyone" I so should have started using this forum earlier, I might have seen daylight occasionally then!

Kind regards

Paul

Re: itemdelimiter?

Posted: Mon Aug 12, 2019 10:48 pm
by bogs
Well Paul,

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