Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: Scipi on December 02, 2010, 08:27:07 pm

Title: Help with sub(
Post by: Scipi on December 02, 2010, 08:27:07 pm
I'm writing a small program in TI-BASIC to calculate derivatives, The way I have it so far is that when the final element inputed is to the 1st power, the result is written as x^0. Example: 5x^4+3x^1 would result in 20x^3+3x^0.

I'm trying to cut out that last bit but I keep getting an invalid dim error.

Here's the code I wrote to parse put that bit.
Code: [Select]
:sub(Str2,length(Str2)-2,length(Str2)->X
:if x="^0"
:sub(Str2,1,length(Str2)-2)->Str2
Title: Re: Help with sub(
Post by: Yeong on December 02, 2010, 08:33:46 pm
isn't last index is supposed to be 1 lower?
:sub(Str2,length(Str2)-2,length(Str2)-1->X
like this
I believe string value starts with 0.
Title: Re: Help with sub(
Post by: FinaleTI on December 02, 2010, 08:34:33 pm
I'm writing a small program in TI-BASIC to calculate derivatives, The way I have it so far is that when the final element inputed is to the 1st power, the result is written as x^0. Example: 5x^4+3x^1 would result in 20x^3+3x^0.

I'm trying to cut out that last bit but I keep getting an invalid dim error.

Here's the code I wrote to parse put that bit.
Code: [Select]
:sub(Str2,length(Str2)-2,length(Str2)->X
:if x="^0"
:sub(Str2,1,length(Str2)-2)->Str2

For one, you can't store a string to X or any real var, and two, that first sub string tries to copy a sub string from two spaces before the end of the string that is the length of the whole string.
sub() works like: sub(String,start position,length of sub string), not sub(String,start position,end position).

Try this code and see if it works:
Code: [Select]
sub(Str2,length(Str2)-1,2
If Ans="^0
sub(Str2,1,length(Str2)-2 -> Str2

Edit: To clarify this:
isn't last index is supposed to be 1 lower?
:sub(Str2,length(Str2)-2,length(Str2)-1->X
like this
I believe string value starts with 0.
It actually starts at 1.
Title: Re: Help with sub(
Post by: yunhua98 on December 02, 2010, 08:36:36 pm
first of all, you can't store a substring into a variable like "X", and second, when you have length(Str2)-2,length(Str2), that means however many tokens in Str2 minus two is the token you start from, but you want to go the length of Str2!  so you get Invalid Dim.

tl:dr:  syntax of Sub is "sub([string],[token # to start from],[# of tokens])"

EDIT:  super ninja'd  :P
Title: Re: Help with sub(
Post by: Yeong on December 02, 2010, 08:37:02 pm
Oops. Ignore mine. I think I was confused with java.
Title: Re: Help with sub(
Post by: Scipi on December 02, 2010, 08:47:59 pm
Quote
EDIT:  super ninja'd

Try Uber ninja'd  :D I just got ninja'd three times over!

Thank you everyone. I just started working with the sub( function yesterday so I'm not too familiar with them.
Title: Re: Help with sub(
Post by: jnesselr on December 02, 2010, 08:54:39 pm
Quote
EDIT:  super ninja'd

Try Uber ninja'd  :D I just got ninja'd three times over!

Thank you everyone. I just started working with the sub( function yesterday so I'm not too familiar with them.
It's all good. Personally, I wish it could return null strings, and had that last item be the end index instead of length, but whatever.  I think it works well when used correctly.