• Print

Author Topic: this wasn't in the wiki!!!  (Read 135 times)

23452

  • Jr. Member
  • **
  • Posts: 72
    • Email
this wasn't in the wiki!!!
« on: November 10, 2012, 03:10:49 PM »
Why does INSTR give odd results when used with strings that have ;'s in them.

   

RhoSigma

  • Jr. Member
  • **
  • Posts: 75
  • 4 * 1.0079 = 4.0026
    • Email
Re: this wasn't in the wiki!!!
« Reply #1 on: November 10, 2012, 03:56:21 PM »
If you really hope for any help, then please provide a code example which will produce the "odd" results you're talking about. The serious programmers on this board don't have the time to explore any possibilities by themselfs.
Download archives of my QB64 stuff:
Polygon functions: http://rhosigma-cw.net/down/GfxDemos.zip
Screen savers: http://rhosigma-cw.net/down/ScrSavers.zip
Libraries collection: http://rhosigma-cw.net/down/QB64Library.zip (extract into the QB64 folder, a new sub-folder "QB64Library" is created)

Billbo

  • Sr. Member
  • ****
  • Posts: 286
    • Email
Re: this wasn't in the wiki!!!
« Reply #2 on: November 10, 2012, 04:06:17 PM »
23452,

I did the following one-liner:

  print "This is Bill;'s."

It compiled and ran with no problem.

Never mind. I was reading to fast.

How do you delete a post???

There is no 'Remove' button on my screen,
so I must not be allowed to delete my
own post. The help mentions the 'Remove'
button, also about the administrator having
the say so, or word to that effect. Oh well, it'll
 just have to take up wasted space.

Bill
« Last Edit: November 10, 2012, 04:32:16 PM by Billbo »

23452

  • Jr. Member
  • **
  • Posts: 72
    • Email
Re: this wasn't in the wiki!!!
« Reply #3 on: November 10, 2012, 04:16:27 PM »
This is the code that is giving the problem
Code: [Select]
INPUT math$
equl$ = LEFT$(math$, (INSTR(math$, "=") - 1) - 0)
equr$ = RIGHT$(math$, (INSTR(math$, "=") - 1) - 0)
PRINT equr$
PRINT equl$

It splits strings like "12345=asdf" into "12345" , "asdf". It works fine until I input a string with a ;
for example "123;456=asdf" is split into "56=asdf" and "123;456"

SMcNeill

  • Hero Member
  • *****
  • Posts: 2414
    • Email
Re: this wasn't in the wiki!!!
« Reply #4 on: November 10, 2012, 04:36:16 PM »
It's working like it should.  You're just getting more letters than you want.

Add this one line to see how it's working for you:
Code: [Select]
INPUT math$
equl$ = LEFT$(math$, (INSTR(math$, "=") - 1) - 0)
equr$ = RIGHT$(math$, (INSTR(math$, "=") - 1) - 0)
PRINT ((INSTR(math$, "=") - 1) -0)
PRINT equr$
PRINT equl$

Your code:
strings like "12345=asdf"...   Takes 6-1, or 5 letters to form the 2 strings...    "=asdf" and "12345" 
strings like "123;456=asdf".... Takes 8-1, or 7 letters to form the 2 strings... "56=asdf" and "123;456"

It's working like you told it to.  You just told it to work wrong.  ;)

Try this instead:
Code: [Select]
INPUT math$
x = INSTR(math$, "=")
equl$ = LEFT$(math$, x - 1)
equr$ = RIGHT$(math$, LEN(math$) - x)
PRINT equr$
PRINT equl$
« Last Edit: November 10, 2012, 04:46:23 PM by SMcNeill »
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

23452

  • Jr. Member
  • **
  • Posts: 72
    • Email
Re: this wasn't in the wiki!!!
« Reply #5 on: November 10, 2012, 04:58:15 PM »
T_T
this is why one should not program at midnight. I had saved the one that was working under the wrong name.
« Last Edit: November 10, 2012, 05:15:52 PM by 23452 »

SMcNeill

  • Hero Member
  • *****
  • Posts: 2414
    • Email
Re: this wasn't in the wiki!!!
« Reply #6 on: November 10, 2012, 05:18:58 PM »
Quote from: 23452 on November 10, 2012, 04:58:15 PM
Your code does work, but it still does not answer why strings with ;'s were gave different results. It worked in every other case.

All you done was change what instr was called, why does it work any different?
   

It only worked because you were having the string size match.

"12345=asdf"   is a 10 character string.   The = is at the 6th spot.   You were taking the left 5 five characters and making a string, and the right 5 characters and making a string.

left 5 =   "12345"
right 5 = "=asdf"

When you tossed the ; into the equation, you changed the overall length of the strings and the location of the = sign

"123;456=asdf"  is a 12 character string.  The = is at the 8th spot.  You were then taking 7 characters and making both strings.

left 7 = "123;456"
right 7 = "56=asdf"

Look at your code for yourself.   INSTR(math$, "=") - 1) - 0) gives you a set constant, telling you where the = is located at in the string.

Since 123;456 = asdf has more characters on the left than the right, does it really make sense that the words returned would be the same size?   1123;456 should be 7 characters.   asdf should be 4.   You were telling it to make both strings the same size.

See the problem?

Try your original code with: "12345=x"   You'll see it has the same issue.   5 characters from the left does not equal 5 characters from the right.

The problem isn't with the ; at all.  It's with how you're counting the characters in your string.   
http://bit.ly/TextImage -- Library of QB64 code to manipulate text and images, as a BM library.
http://bit.ly/Color32 -- A set of color CONST for use in 32 bit mode, as a BI library.

http://bit.ly/DataToDrive - A set of routines to quickly and easily get data to and from the disk.  BI and BM files

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
Re: this wasn't in the wiki!!!
« Reply #7 on: November 15, 2012, 10:05:34 AM »
I'm not sure of your goal, but here is something I use a lot with dealing with finding things in strings. It could be easily modified to use entire words. If you don't need it ignore it.

Code: [Select]
DIM SHARED splitstring$(5000) ' SplitString Array

z$ =  "SIZE:5,RACE:HUMAN"

CALL split_string (z$, ":")

PRINT splitstring$(1)
PRINT splitstring$(2)
PRINT splitstring$(3)
PRINT splitstring$(4)

END

' Seperate a String Via a Specific Character (character = w$) into an array

SUB split_string (str1ng$, seperator$)
ERASE splitstring$
z$ = str1ng$: w$ = seperator$
a = 0: b = 0: x = 0: y$ = "": z = 0
IF w$ = "" THEN w$ = ";"
IF z$ = "" THEN
ELSE
    IF LEN(z$) = 0 OR LEN(z$) = 1 THEN
    ELSE
        z$ = w$ + z$ + w$
        DO
            a = a + 1
            x$ = RIGHT$(LEFT$(z$, a), 1)
            IF x$ = w$ THEN b = 1: y$ = ""
            WHILE b = 1
                a = a + 1
                x$ = RIGHT$(LEFT$(z$, a), 1)
                IF x$ = w$ THEN
                    a = a - 1
                    x = x + 1 ' Count Up in the Array
                    splitstring$(x) = y$ ' Add Value to the Array
                    b = 0
                ELSE
                    y$ = y$ + x$
                END IF
                IF a = LEN(z$) THEN EXIT WHILE
            WEND
        LOOP UNTIL z = 1 OR a = LEN(z$)
    END IF
END IF
END SUB

  • Print