Page 1 of 2

How to get only the number in a string?.

Posted: Sat Feb 22, 2014 5:08 pm
by snop21
Hello Livecoders,

I have a string consist of number and letters

"sample(123)"

I will just ask is it possible to get only the numbers on that string? only the 123.

Thank you.

-snop21

Re: How to get only the number in a string?.

Posted: Sat Feb 22, 2014 5:17 pm
by Klaus
Hi Snop21,

you could check for the ASCII value 0-9 = 48-57!

Here a quick li'l function :D

Code: Select all

function numbers_from_string tString
  put empty into tRetValue
  repeat for each char tChar in tString
    if chartonum(tChar) > 47 AND chartonum(tChar) < 58 then
      put tChar after tRetValue
    end if
  end repeat
  return tRetValue
end numbers_from_string
...
answer numbers_from_string("sdsdsd12dddd44")
## -> 1244
...

Best

Klaus

Re: How to get only the number in a string?.

Posted: Sat Feb 22, 2014 5:26 pm
by snop21
klaus,


WOW... That's magic...! :))


Thank you..

-snop21

Re: How to get only the number in a string?.

Posted: Sat Feb 22, 2014 5:36 pm
by Klaus
snop21 wrote:WOW... That's magic...! :))
No, Livecode! :D

You could also use:

Code: Select all

...
if isnumber(tChar) then
  put tChar after tRetValue
end if 
...

Re: How to get only the number in a string?.

Posted: Sat Feb 22, 2014 5:56 pm
by snop21
Klaus,

I wish I could be good as like you.. :))


-snop21

Re: How to get only the number in a string?.

Posted: Sat Feb 22, 2014 6:05 pm
by Klaus
Hi snop21,

practice, practice, practice! 8)

Well, I started learning Metacard, the grandfather of Livecode, in 1999 the "hard way",
means without any previous programming experience and without internet access for any
kind of help! Only try & error and that was a #$%&ยง good training! :D

Let's talk about this again in a couple of years!


Best

Klaus

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 4:39 am
by snop21
Good Day Klaus,

Yah..! You are exactly right.. we were so lucky today, we can ask help anytime we want because of the Internet Era today.. :))

I have followup question.

Is it possible to get only the 1st 4 character?. quiet complicated..hehe gonna put sample.. hehe

the string is =====> "(12) Boy90"

I want to get the number of the 1st 4 charcater of the string... So I will be getting ony the 12.

Regards

-snop21

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 5:28 am
by dunbarx
Hi.

True. You have access to an astonishing help resource. That is a big problem.

This happens with many new users. The learning curve for LC is not zero. In fact, though relatively short, it requires much effort and frustration. It just seems simple, as if the ability to read will make the necessary skills just come. They will not.

Your queries have to be homework for you. You have seen how to distinguish numbers within a string. You simply must learn how to work text, using the tools LC provides. Unless you do, you will very soon ask questions to which you will not get ready answers. Your questions, though hardly beyond the helper community's ability to answer, will nonetheless go beyond the scope of what they will be willing to provide. At that time your learning will grind to a halt.

Search the dictionary as best you can. Read and work the tutorials and lessons. Skim the user guide. Do write back often. But write back with problems you are having, code that will not work, and results you cannot understand. There is unlimited help here for that sort of thing. I, for one, cannot wait.

Craig Newman

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 10:08 am
by Thierry
snop21 wrote: the string is =====> "(12) Boy90"
I will be getting ony the 12.
Hi Snop,

after reading LiveCode User guide.pdf,
chapter: 6.3 Regular Expressions,
you might find interesting this solution:

Code: Select all

put "(12) Boy90" into myString
get matchText( mystring, "^\((\d+)\)", myNumber)
answer mynumber
Regards,

Thierry

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 10:15 am
by snop21
Good Day Guys,

dunbarx,

--- Thank you for reminding me that I have to work also with myself alone not just waiting for somebody to solve my problem..! :))

---> I got it solved. I attached the script.. :))

Thierry,

Thanks.! But there's a another way to do it.. not that complicated.. hehe See attached file.. :))

Regards

-snop21

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 10:39 am
by Thierry
snop21 wrote: Thierry,
Thanks.! But there's a another way to do it.. not that complicated..
Come on, nothing complicated here. :)
Just has to learn the basics of regex.
If you don't like it, well that's Ok, but then it's a different story :roll:

And Yes, there is always many ways to do things, as with most of languages.

Regards,

Thierry

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 10:51 am
by snop21
Thierry,

Hehe.. :)) I am newbie of this PL.. that's why most everything of livecode for me is complicated.. haha :)) I think I am improving.

Yah.. I remember one of my Prof. in Programming in College says "There a lot way of killing a chicken".

-Regards

-snop21

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 11:01 am
by Thierry
snop21 wrote: Hehe.. :))
Yah.. I remember one of my Prof. in Programming in College says "There a lot way of killing a chicken".
poor chicken :cry:

you're not English, as they are used to say:
there is more than one way to skin a cat
and in my country:
more than one way to cook a rabbit
So,who is right: the chicken, the cat or the rabbit?

my 5 minutes crazyness an good luck with Livecode :)

Thierry

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 11:08 am
by snop21
I am from the Philippines.. :) I am sorry if my english is not that good.. haha :lol:

Thanks Thierry!.. See you in my next post.. haha :D

Re: How to get only the number in a string?.

Posted: Mon Feb 24, 2014 11:25 am
by Thierry
snop21 wrote:I am from the Philippines.. :)
Oh, a nice place I missed to visit..
I am sorry if my english is not that good..
Well, wasn't what I meant; I was intrigued only with the chicken :)
and my English is limited too.

Thierry