Jump to content

Why am I getting 'Type Mismatch'?


You are about to reply to a thread that has been inactive for 99 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi there - this has me puzzled:

I have written a little function to capitalize the first character in a sentence.  I get an Error: Type mismatch on the last but one line 'testitem = capfirstlet (testitem)' but as far as I can see, I have only been dealing with strings. I must be missing something obvious.


string testitem ="phil went shopping";

capfirstlet (string src)
{
 string firstlet = llGetSubString(src, 0, 0);
 firstlet = llToUpper(firstlet);
 src = firstlet+ llGetSubString(src, 1, -1);
}

default
{
    touch_start(integer total_number)
    {
     testitem = capfirstlet (testitem);
     llOwnerSay(testitem);
    }
}

 

Link to post
Share on other sites

You didn't declare the type of variable to be returned by your function.  It should look like

string capfirstlet (string src)
{
 string firstlet = llGetSubString(src, 0, 0);
 firstlet = llToUpper(firstlet);
 string new_src = firstlet+ llGetSubString(src, 1, -1);
 return new_src;
}

 

  • Like 4
Link to post
Share on other sites
13 minutes ago, Wulfie Reanimator said:

What if the first letter isn't a letter?

For example: "12 more things..." or "[brand] product"

I was about to jump in and post an elaborate solution when it occurred to me.... the uppercase of a non-alphabetic character is the same character... so it still works fine.

On a side note, it may act a little goofy with a single character as the src input.

Edited by Phate Shepherd
  • Like 1
Link to post
Share on other sites
13 minutes ago, Phate Shepherd said:

On a side note, it may act a little goofy with a single character as the src input.

yes.  (1, -1) will return "Aa" when the input string is a single char "a" and "AA" when it is "A"

is best not to mix positive and negative string indices in the case of all strings.  In the all strings case then (1, llStringLength(src)); which in the single char case translates to (1, 1) which returns an empty string because (1, 1) points beyond the end of the string

in the (1, -1) case then -1 always points to the last char in the string, which in the single char case points to the only/first/last char. The -1 algorithm then moves from right to left until either a) it meets of the condition of (1, ..) or b) there are no remaining chars preceding the -1 char

 

Link to post
Share on other sites

It will return an empty string for an empty string and the expected value for string length >= 2
So you need a little extra to handle the string length == 1 case.

I take Roligs code for that:

string capfirstlet (string src)
{
 string firstlet = llGetSubString(src, 0, 0);
 firstlet = llToUpper(firstlet);
 if (llStringLength(src)==1) return firstlet;
 string new_src = firstlet+ llGetSubString(src, 1, -1);
 return new_src;
}

If you don't like middle-exits you can always complicate things 😎

Edited by Nova Convair
  • Like 1
Link to post
Share on other sites
1 hour ago, Phate Shepherd said:

I was about to jump in and post an elaborate solution when it occurred to me.... the uppercase of a non-alphabetic character is the same character... so it still works fine.

On a side note, it may act a little goofy with a single character as the src input.

I didn't mean to ask "what would happen" but "what should happen?"

Do you want to capitalize the first character of the string or the first letter in the string?

  • Like 1
Link to post
Share on other sites
2 hours ago, Wulfie Reanimator said:

I didn't mean to ask "what would happen" but "what should happen?"

Do you want to capitalize the first character of the string or the first letter in the string?

Ahh, very good point.

Link to post
Share on other sites

For multiple sentences in a string, like converting a notecard, you could try llParseString2List() with most common end of sentences as the spacers. Then,  say the list as a string.

string song = “mary had a little lamb. a little lamb... yes, a little lamb! you’ve heard this one before?”;
list keepemseparated = llParseString2List( song, [], [“. “, “.  “, “... “, “! “, “!! “, “? “, “:) “, “;) “]);
integer i;

integer j = llGetListLength(keepemseparated) - 1;

while (i < j) {
   string s = llList2String(keepemseparated, i);

   s = llToUpper(llGetSubString(s, 0, 0)) + llGetSubString(s, 1, -1);

   keepemseparated = llListReplaceList(keepemseparated, , i, i);

   i += 2;

}
llOwnerSay((string)keepemseparated);

Wonder if anyone’s made a code-typing-friendly keyboard for iOS yet. And why is it strike-through when I edit?

Edited by DoteDote Edison
Link to post
Share on other sites
You are about to reply to a thread that has been inactive for 99 days.

Please take a moment to consider if this thread is worth bumping.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...