String/Substring variable length [Solved]

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

antrax13
Posts: 39
Joined: Mon Jun 01, 2015 11:38 am

String/Substring variable length [Solved]

Post by antrax13 » Mon Aug 31, 2015 2:48 pm

Hi all,

I am not sure if this is the proper way of doing it but what I need is following.
I have values like:

Code: Select all

ABC1
ABCD1
ABCDE1
and what I need is to get:

Code: Select all

ABC
ABCD
ABCDE
Is there any function that will remove last character from string or do I have to find out that length of string and then select char 1 to totalLength-1?

Regards
Last edited by antrax13 on Mon Aug 31, 2015 4:51 pm, edited 1 time in total.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String/Substring variable length

Post by Klaus » Mon Aug 31, 2015 3:15 pm

Hi antrax13,

no special function for this! :D
Use a repaet loop like this:

Code: Select all

...
## repeat for each.. is insanely fast, but read-only, so we fill a new variable here:
repeat for each line tLine in YourListHere
  ## Only use first to last - 1 character, 
  ## luckily we can count from "backwards" in Livecode
  put char 1 to -2 of tLine & CR after tNewList
end repeat

## Delete trailing CR
delete char -1 of tNewList

## Now do whatever you need to do with tNewList :D 
...
Best

Klaus

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: String/Substring variable length

Post by zaxos » Mon Aug 31, 2015 3:16 pm

How about that:

Code: Select all

Repeat with x = 1 to the number of lines of myvar
Delete last char of line x in myvar
End repeat
Last edited by zaxos on Mon Aug 31, 2015 3:37 pm, edited 1 time in total.
Knowledge is meant to be shared.

antrax13
Posts: 39
Joined: Mon Jun 01, 2015 11:38 am

Re: String/Substring variable length

Post by antrax13 » Mon Aug 31, 2015 3:28 pm

Klaus wrote:Hi antrax13,

no special function for this! :D
Use a repaet loop like this:

Code: Select all

...
## repeat for each.. is insanely fast, but read-only, so we fill a new variable here:
repeat for each line tLine in YourListHere
  ## Only use first to last - 1 character, 
  ## luckily we can count from "backwards" in Livecode
  put char 1 to -2 of tLine & CR after tNewList
end repeat

## Delete trailing CR
delete char -1 of tNewList

## Now do whatever you need to do with tNewList :D 
...
Best

Klaus
Would your approach be faster than mine?

Code: Select all

put char 1 to ( the length of tField-1) of tField into withoutNumber

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String/Substring variable length

Post by Klaus » Mon Aug 31, 2015 3:42 pm

antrax13 wrote:Would your approach be faster than mine?
I have no idea! 8)

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

Re: String/Substring variable length

Post by dunbarx » Mon Aug 31, 2015 3:45 pm

I would have predicted this.

With a sample of 1,000,000 lines, and a "repeat for each" loop with the variable tLine:

delete the last char of tLine = 11 ticks
delete char -1 of tLine = 13 ticks
put char 1 to the length of tLine - 1 of tLine = 14 ticks

Craig Newman

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: String/Substring variable length

Post by zaxos » Mon Aug 31, 2015 3:46 pm

Its funny how I always get ignored over the moderators, embrace the power ppl, embrace the power.
Knowledge is meant to be shared.

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

Re: String/Substring variable length

Post by dunbarx » Mon Aug 31, 2015 3:50 pm

Zaxos.

What did you mean, "ignored".

I used your example as one of the timed tests. Though I made all loops the "repeat for" style, as opposed to "repeat with".

Craig

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: String/Substring variable length

Post by zaxos » Mon Aug 31, 2015 3:55 pm

Not talking about you dunbarx, nor for any moderator, I think its obvious who this goes about, thank you for your tests tho :)
Knowledge is meant to be shared.

antrax13
Posts: 39
Joined: Mon Jun 01, 2015 11:38 am

Re: String/Substring variable length

Post by antrax13 » Mon Aug 31, 2015 4:51 pm

Thanks all.

Special thank you for zaxos :wink:

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String/Substring variable length [Solved]

Post by Klaus » Mon Aug 31, 2015 4:52 pm

Hi zaxos,

you are "speaking in tongues"! :D


Best

Klaus

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: String/Substring variable length [Solved]

Post by zaxos » Mon Aug 31, 2015 6:03 pm

antrax13:
Thanks all.

Special thank you for zaxos :wink:
Thank you for thanking me! thats all i needed

Kalus:
Hi zaxos,

you are "speaking in tongues"! :D


Best

Klaus
I had to google that since i'm not familar with that expression, wiki came up with:
Glossolalia or (speaking in tongues) is the fluid vocalizing of speech-like syllables that lack any readily comprehended meaning, in some cases as part of religious practice. Some consider it as a part of a sacred language. It is a common practice amongst Pentecostal and Charismatic Christianity.
I still dont understand what that means! I'm not even sure if i should be offended :P, i do know thought that my text in not always easily understandable if thats what you mean, if on the other hand you mean that my words are meaningless then my answer to that is that we are not robots, people can and should be polite, it costs nothing, so stop being so judgmental and help me out here.

Craig, could you share with me how you tested this? thank you :)
Knowledge is meant to be shared.

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: String/Substring variable length [Solved]

Post by zaxos » Mon Aug 31, 2015 7:37 pm

Craig? i thought it is "put the milliseconds" at the end of the handler but this gives me 1441046154985... I guess thats a bit too much.
nvm. the code below gave me 13 ticks, so i guess it worked :D

Code: Select all

on mouseUp
   local theVar
   local tTime
   put the ticks into tTime
   repeat 1000000
      put "test" into theVar
   end repeat
   put the ticks-tTime
end mouseUp
Last edited by zaxos on Mon Aug 31, 2015 9:17 pm, edited 1 time in total.
Knowledge is meant to be shared.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String/Substring variable length [Solved]

Post by Klaus » Mon Aug 31, 2015 8:32 pm

Hi zaxos,
zaxos wrote:I still dont understand what that means! I'm not even sure if i should be offended :P, i do know thought that my text in not always easily understandable if thats what you mean,
no offence!
I just meant: The meaning of your talking is mysterious! :-)
I other words, I have no idea what you mean with:
Not talking about you dunbarx, nor for any moderator, I think its obvious who this goes about...
Best

Klaus

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: String/Substring variable length [Solved]

Post by zaxos » Mon Aug 31, 2015 9:11 pm

I see, so maybe i was unclear
This was obviously meant for dunbarx:
Not talking about you dunbarx
Now this was actualy for you:
nor for any moderator
unless i'm wrong, you are a moderator of this forum(?)

And the last part of which i thought was obvious was meant for antrax13 who was kind enough to saw me i was wrong at the end:
I think its obvious who this goes about...
Knowledge is meant to be shared.

Post Reply