Announcement

Collapse
No announcement yet.

String Initializing problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • String Initializing problem

    Hello, problem that has caused me to lose a handful of hair.

    Here is my code:

    Temp2$="" :' I am *Trying* to make NewWord EMPTY
    for Kount = 1 to 6
    Temp2$ = Temp2 + mid$(OldWord,Kount,1)
    next Kount


    In my case, Temp2$ never initializes to nothing. Its string
    length is -28672! I tried printing Temp2$ to the screen and it
    prints one of the characters from Character #33 to Character #90
    (On an Ascii table).

    Is Temp2$ a reserved word by Powerbasic? I tried using other
    variables, but I end up with the same contents whenever I go to
    "build" my new word. Any suggestions? I tried using Watch on
    debug, and the puzzler is Temp2$ stays empty right up until the
    line after Temp2$ = "" I have no idea where it is getting
    its data- I don't have files open.

    Thanks for any input. Yes, I will post the code, but I would
    rather wait until requested to do so. It is currectly 92 lines,
    not too bad yet, but not finished either.

    Robert

    ------------------

  • #2
    Greetings Robert!

    I played a little with your code:

    Code:
    Temp2$="cisaBrewoP"
    OldWord$="PowerBasic"
    
    Temp2$=""
    
    FOR Kount% = 1 TO 6
      PRINT Temp2$, LEN(Temp2$)
      Temp2$ = Temp2$ + MID$(OldWord$,Kount%,1)
    NEXT Kount%
    ...and met with success, seemingly. Here's the output of the above stated code:

    Code:
                   0 
    P              1 
    Po             2 
    Pow            3 
    Powe           4 
    Power          5
    ...the output shows that the Temp2$ variable was reinitialized and then padded with new information. The only thing that I noticed in your code:

    1. Line One has a colon before your comment. The colon isn't needed.
    2. You've initialized the Temp2$ to ""; but then you use the variable Temp2 in line three.
    3. I'm a fan of using $'s, %'s, &'s and such in my programs. It appears that you arbitrarily do; unless those unstated ones are global variables.


    ------------------
    Don Ewald
    mailto:[email protected][email protected]</A>
    Donnie Ewald
    [email protected]

    Comment


    • #3
      Try using the $ string identifier in all places.
      OldWord is, by default, an integer.
      Slight changes make it work for me in PB 3.2
      To say why it wasn't working for you, I would have to see more code.

      Code:
      OldWord$ = "aBcDeFgHiJkL"
      Temp2$="" ' I am *Trying* to make NewWord EMPTY
      for Kount% = 1 to 6
        Temp2$ = Temp2$ + mid$(OldWord$,Kount%,1)
      next Kount%
      print Temp2$
      regards,
      (Don beat me to the posting.)

      [This message has been edited by Ian Cairns (edited September 10, 2001).]
      :) IRC :)

      Comment


      • #4
        I tried it with version 19 of PB 3.5 for DOS and it won't
        even compile as run in a DOS-VDM in OS/2.

        It, I think, correctly traps the Temp2$ = Temp2 portion of the
        line at the place where there is no string ($) with an error
        421, "String operand expected" here.

        On the extra colon ":" in that first line, you may fault me amd
        chew me out if you like, but I've seen that un-needed colon
        also produce corrupt compiled code where some curious combination
        of text is beyond that remark line apostrophe, long ago and far
        away. That's two years ago or so and I can't recall what the
        other text was in the remark line which caused it. I seem to
        recall that the difference between using "REM" and "'" at that
        point was a factor in evoking the error.

        At any rate removing the un-needed ":" broke the error. This
        issue was one reason I made the decision to never use more
        than one statement on a line in PowerBASIC, although, in
        theory, that shouldn't make any difference, in general in your
        coding style choice ...

        ------------------
        Mike Luther
        [email protected]
        Mike Luther
        [email protected]

        Comment


        • #5
          Originally posted...

          On the extra colon ":" ... but I've seen that un-needed colon
          also produce corrupt compiled code...
          I hope that noone will follow this advice, as it simply does not apply to PowerBASIC in any fashion. In this context, the result is 100.00%, absolutely identical with or without the colon. Please don't be misled into making your programming more difficult than it ought to be.

          Regards,

          Bob Zale
          PowerBASIC Inc.


          ------------------

          Comment


          • #6
            I use colons in my code EXTENSIVELY, and have NEVER had a problem
            with it, either in compile time or runtime, in both PB/DLL 6.0 and
            PB/DOS 3.5.

            Regards,


            ------------------
            Clay C. Clear

            mailto:[email protected][email protected]</A>

            Clay Clear's Software

            Comment


            • #7
              Thanks for the replies guys.

              As for the colon causing the problem, blame my prof. She
              wanted multiple statements on a line, the colon before each
              comment, etc. It is a habit I can break if I need to.

              Would the fact I am using the full Dos window (not just a small
              Dos box) in Windows ME be a clue?

              I am going to get out of here, and CnP the code and compare it
              my own. In my mind (except for the $'s, %'s, &'s) the code is
              close. I will come back and let you know.

              In my program, I had Temp2$ throughout, not Temp2. I mis-typed
              here. My fault there.

              Thanks for the help!

              ------------------

              Comment


              • #8
                To me, it sounds more like you have a memory corruption issue due to overwriting past an array boundary or similar, or you have some variable-class problems.

                I assume that this is part of a {much} larger program right? The first thing you need to do is add the line $ERROR ALL ON to the top of the code and run it again.

                Chances are, the real problem is elsewhere in the code.

                Please try this and let us know what else you discover...

                Thanks!

                PS: I would doubt VERY MUCH that Windows ME has anything to do with this problem!

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Guys- no go for me. This time, I CnP'd the troublesome code for
                  you to see firsthand. Temp2$ *still* gives me a weird contents
                  even after initializing to "". The copied code is at the bottom
                  of this post.

                  Explanation: This is a word unjumbler. It accepts an entry, and
                  mathematically determines how many ways there are to rearrange
                  the given letters.

                  The array PossOrd stores numbers containing ways to arrange
                  letters. For a 4 letter word (Let's use "SKED" which unscrambles
                  to DESK.) The original order is 1234 (1 represents the first
                  letter, 2 represents the second letter, and so on). The program
                  will not accept the number such as 3224, because it makes no use
                  of the letter "S" and uses "K" twice.

                  PossOrd is an array that contains ways the word can be the
                  arranged. Four a 4 letter word, there are 24 ways to arrange
                  it and hence 24 numbers get generated. For the curious, a
                  seven-letter word has 5040 ways it can be arranged, so I have
                  set my array to hold 5100 numbers at the most.

                  I am using the full Dos window in Windows Me, if that is a clue,
                  and using an AMD 850.

                  Now, assume the value of PossOrd(I6) to be 1243, Temp2$ should
                  end up with "SKDE" (which is not valid, but I will check for
                  that later.)

                  Hey guys, none of you have to do this; I will admit I am very
                  stumped and could use someone pointing out my errors. I took
                  out all of the comments to see if that would work, but didn't
                  seem to help any.


                  Immediately after setting Temp2$ to "", the debugger shows the
                  contents of Temp2$ to be horribly long, I think it is in excess
                  of 1200 characters! It begins a pattern of repeating after
                  so many characters.


                  Initial values from earlier in program:
                  PossOrd(I6) = 1243
                  WordLen = 4, determined earlier in program
                  TestWord$ = "SKED"

                  Then the first time through, I get: (Should get, but don't)

                  I6 = 1
                  PossOrd(i6) = 1243
                  Possible$ = "1243"
                  I7 = 1
                  GetCharNo = 1 ' get the first letter in TestWord$
                  Temp2$ = "S"

                  Second time through
                  I6 = 1
                  PossOrd(i6) = 1243
                  Possible$ = "1243"
                  I7 = 2 ' get the second letter in TestWord$
                  GetCharNo = 2
                  Temp2$ = "SK"

                  Third time through
                  I6 = 1
                  PossOrd(i6) = 1243
                  Possible$ = "1243"
                  I7 = 3 ' get the third letter in TestWord$
                  GetCharNo = 3
                  Temp2$ = "SKD"

                  Fourth time through (Last in this example)
                  I6 = 1
                  PossOrd(i6) = 1243
                  Possible$ = "1243"
                  I7 = 4 ' get the first letter in TestWord$
                  GetCharNo = 4
                  Temp2$ = "SKDE"

                  '
                  ' Create New List of possible orders- MOST words will be invalid
                  for i6 = 1 to Pk
                  Temp2$=""
                  Possible$=str$(PossOrd(I6))
                  Possible$=Trim$(Possible$)
                  for i7 = 1 to WordLen
                  GetCharNo=val(mid$(Possible$,i7,1))
                  Temp2$=Temp2$+mid$(TestWord$,ChetCharNo)
                  next i7
                  print Temp2$
                  'gosub ValidWordQ
                  next i6


                  ValidWordQ:
                  'not written yet

                  end


                  ------------------

                  Comment


                  • #10
                    For Lance Edmonds.

                    <Copy from Lance>
                    I assume that this is part of a {much} larger program right?
                    The first thing you need to do is add the line $ERROR ALL ON
                    to the top of the code and run it again.
                    <End copy>

                    That was surprising, but being a Powerbasic newbie, I am not
                    sure what it means. It said:

                    .... If this happens on a DIM or REDIM, an attempt was made to
                    allocation more than 64K of memory without the HUGE option.


                    "HUGE" option? That is totally new to me. Ok, Just tried it and ]
                    it stops at the same point with the same error. Should I just
                    re-write this from scratch?

                    Robert

                    ------------------


                    [This message has been edited by Robert Carneal (edited September 10, 2001).]

                    Comment


                    • #11
                      Robert,
                      1.The line incrementing temp2$ is adding more than 1 character at a time.
                      MID$() requires a variable indicating length, otherwise you get a
                      substring from the extraction point onwards. This can become too large
                      very quickly and might account for the error message.

                      2.You can combine the ltrim$() and str$() operations.

                      A minimal functional example is:

                      Code:
                      DIM possOrd(1:5100)
                      possOrd(1) = 1234
                      possOrd(2) = 1342
                      possOrd(3) = 1432
                      possOrd(4) = 1423
                      WordLen = 4  '  determined earlier IN program
                      TestWord$ = "SKED"
                      Pk = 4
                      
                      FOR i6 = 1 TO Pk
                        Temp2$=""
                        Possible$=LTRIM$(STR$(PossOrd(I6)))
                        FOR i7 = 1 TO WordLen
                          GetCharNo=VAL(MID$(Possible$,i7,1))
                          Temp2$=Temp2$+MID$(TestWord$,GetCharNo,1)
                        NEXT i7
                        PRINT Temp2$
                      NEXT i6
                      regards,

                      ------------------
                      [email protected]
                      :) IRC :)

                      Comment


                      • #12
                        Bob Zale is correct.
                        There is no difference by adding a ":" or not at the end of a
                        line (you have to know what is the end of the line).
                        Power Basic lives upto what it says on the documentations.




                        ------------------

                        Comment


                        • #13
                          Robert,
                          -------------------------------------------------------------
                          original post.....

                          'Create new list of possible orders -- Most words will be invalid

                          FOR I6 = 1 TO PK
                          TEMP2$=""
                          POSSIBLE$=STR$(POSSORD(I6))
                          POSSIBLE$=TRIM$(POSSIBLE$)
                          FOR I7 = 1 TO WORDLEN
                          GETCHARNO=VAL(MID$(POSSIBLE$,I7,1))
                          TEMP2$=TEMP2$+MID$(TESWORD$,CHECHARNO)
                          NEXT I7
                          PRINT TEMP2$
                          NEXT I6
                          -----------------------------------------------------------------
                          Your problem is the value of numeric variables.
                          Try add a line "PRINT PK;WORDLEN;CHECHARNO"
                          before "FOR I6 = 1 TO PK"
                          and
                          add a line "PRINT GETCHARNO;CHECHARNO:PRINT TEMP2$:PRINT LEN(TEMP2$)"
                          before "next I7"

                          Regards

                          ------------------

                          Comment


                          • #14
                            Originally posted by Robert Carneal:
                            For Lance Edmonds.
                            <Copy from Lance>
                            The first thing you need to do is add the line $ERROR ALL ON to the top of the code and run it again.
                            <End copy>

                            That was surprising, but being a Powerbasic newbie, I am not sure what it means. It said:

                            .... If this happens on a DIM or REDIM, an attempt was made to allocation more than 64K of memory without the HUGE option.
                            By this I assume that you got an error? From the description, you got an out of range error (Error #9). This means you are probably trying to access an array past is boundary, as I predicted in my last message.

                            Which line of code did the error actually occur on? That should give you the clue you need.

                            Doris has also given you a good debugging tip here... add a PRINT line into the loop (assuming that is where is fails) to display the values and variables you are working with.

                            "HUGE" option? That is totally new to me. Ok, Just tried it and ]
                            it stops at the same point with the same error.
                            That is not really applicable to your solution I think, since you are dealing with 5100 subscripts or less.

                            Should I just re-write this from scratch?
                            Probably not, since you really only need to debug the existing code a little.

                            In summary, based on the Error 9, and not knowing which line of code is failing, I'd take a stab at a few likely causes of the failure:[list=1][*] The variable "pk" is much larger than the number of subscripts in the PossOrd() array.[*] The typo in the MID$() line refers to a variable "chetcharno" whereas the other lines refer to "getcharno" [/list=a]
                            Finally, there is one shortcoming with the code. You set the size of WORDLEN in the earlier piece of code somewhere, but POSSSIBLE$ may be smaller since you use TRIM$(). You may want to change the FOR loop to the exact length, rather than use WORDLEN.

                            For example:
                            Code:
                            for i6 = 1 to Pk
                              Temp2$=""
                              Possible$=str$(PossOrd(I6))
                              Possible$=Trim$(Possible$)
                              for i7 = 1 to [b]len(Possible$)[/b] ' Was "WORDLEN"
                                GetCharNo=val(mid$(Possible$,i7,1))
                                Temp2$=Temp2$+mid$(TestWord$,[b]GetCharNo[/b]) ' Was "ChetCharNo"
                              next i7
                              print Temp2$
                              'gosub ValidWordQ
                            next i6
                            Anyway, this is mostly speculation, but I hope it helps!

                            PS: you can use $DIM ALL to help with variable name errors such as I mentioned in point #2 above.

                            ------------------
                            Lance
                            PowerBASIC Support
                            mailto:[email protected][email protected]</A>
                            Lance
                            mailto:[email protected]

                            Comment

                            Working...
                            X