Welcome to Phrogram Sign in | Join | Help
in Search


Learn and have fun with computer programming!

Substring behaviour I don't understand

Last post 09-29-2006, 9:00 AM by JonS. 10 replies.
Sort Posts: Previous Next
  •  09-27-2006, 12:04 PM 696

    Substring behaviour I don't understand

    Hi all,

    Can anyone explain to me why

    a) This prints "es":

    Print(Substring("testing", 2, 5))

    b) This results in a runtime error saying that Length can't be 0:

    Print(Substring("testing", 2, 10))

    Based on the description in the system method documentation (see below), I would expect a) to print "estin" and b) to print "esting".

     
    Function Substring ( Text AS STRING, StartPos AS INT, Length AS INT ) AS STRING
    Returns the part of Text starting at StartPos, to a maximum of Length letters

     

     

    Filed under: , ,
  •  09-27-2006, 1:48 PM 697 in reply to 696

    Re: Substring behaviour I don't understand

    Hi Gert,

    I just tried both of your Substrings and both gave me what you expected, a) estin and b) esting no warnings or error messages. To eliminate typos I copied your code from your post, no chages made, just entered into a new program window.

     I'm using Program 2.0.2427.25626
     

  •  09-27-2006, 3:23 PM 711 in reply to 697

    Re: Substring behaviour I don't understand

    Great, but this is the KPL Q&A forum :-) and I'm using KPL 1.1.0.25534. Just did the same as you did, copying the code from my post, and I keep getting the wrong results. I wouldn't mind so much that one function is messed up, but according to the docs there isn't much as to alternative string functions that I can use to do the same.

     

  •  09-27-2006, 3:27 PM 712 in reply to 697

    Re: Substring behaviour I don't understand

    Bert is using Phrogram, I think - and SubString does indeed work properly in Phrogram.  Gert is right, though, that there is a bug in KPL.  I seem to remember a workaround was found when we last had this reported and I'm digging around for that now. 

    Only thing I have found so far is this example, which limts itself to a third parameter of 1:

    http://p211.ezboard.com/fkplfrm7.showMessage?topicID=9.topic

    What some quick testing suggests to me is that a final parameter of 1, 2 or 3 will work properly with SubString, but that values higher that that only return 2, and values beyond the end of the string will return a runtime error.

    I'll let you know if I find a better answer for KPL.
     

  •  09-27-2006, 3:32 PM 713 in reply to 712

    Re: Substring behaviour I don't understand

    Thanks for that answer already, Jon. That is indeed what I found too, that it works as long as the Lenght isn't greater than 3.
  •  09-28-2006, 11:33 AM 722 in reply to 713

    Re: Substring behaviour I don't understand

    For now, I'm using this as a substring replacement 

    Function SubStr(Text As String, Start As Int, Length As Int) As String
        Define Result As String
        Define MaxPosition As Int
        Define xx As Int
       
        MaxPosition = Start + length - 1
        If StringLength(Text) < MaxPosition Then
            MaxPosition = StringLength(Text)
        End If
       
        For xx = Start To MaxPosition
            Result = Result + Substring(Text, xx, 1)
        Next
       
        Return Result
       
    End Function

    Must be slow as molasses, unless the combined efforts of Jon and Microsoft lead to great compiler optimization. :-) But it gets the job done. 

    By the way, when researching Substring, I ran into this error in the manual:

    Function ContainsMonkey( Text As String ) As Bool
      If SubString( Text, "Monkey" ) <> 0 Then
        Return True
      Else
        Return False
      End If
    End Function

    "Substring" in that fragment should be "IndexOf". 

  •  09-28-2006, 3:39 PM 727 in reply to 722

    Re: Substring behaviour I don't understand

    Thanks for the workaround function, Gert!  I think that should work well enough - and is an excellent example for the community - that if KPL doesn't do exactly what you want, you can often write KPL code to MAKE it do what you want.

    About your use of KPL - the two main reasons why people should stick with KPL rather than Phrogram at this point are 1) The machines it runs on are Win98 machines or 2) A language file is required (in your case Dutch?) which is only available for KPL.  I'm thinking one or both of those are true for you?

    And thanks as well or pointing out the documentation bug!

     

  •  09-28-2006, 3:57 PM 729 in reply to 727

    Re: Substring behaviour I don't understand

    Actually, I prefer not to use Phrogram because you're not making the full version freeware. When I got interested in KPL, it was freeware and it was being hinted at that it would go open source. It seems that you're moving in another direction with Phrogram. Which is fine, but personally I think it will be the end of KPL/Phrogram's chances of becoming an educational standard and will make it just one of the many useful but not spectacular tools for education.
  •  09-28-2006, 5:10 PM 731 in reply to 729

    Re: Substring behaviour I don't understand

    We always want to hear feedback, even if the feedback isn't agreement.  Thanks for offering it.

    I would ask you to note that the free version of Phrogram is a superset of the free version of KPL, and superior in many ways, even if there are some Phrogram features which are not available unless a license is purchased.  The decision to proceed with optional commercial upgrades was not a decision we made quickly, and it was made fairly publicly, on the forums, in the discussions with academia, including the paper submitted to the ACM's SIGGRAPH conference, and in discussions with lots of people who had expert thought to offer.  The bottom line reason we chose to go this way is that we believe this to be the most likely way that we can keep KPL and Phrogram funded, work actively on improving it, and work actively to spread the word and support the usage of it around the world.  We shall see how things go with that decision - but as we have always been, we remain flexible, should circumstances change so that other ways of achieving those goals look more likely to succeed.  

  •  09-29-2006, 1:23 AM 740 in reply to 731

    Re: Substring behaviour I don't understand

    Now you're doing it again, suggesting that things might change! It still feels to me like you're trying to have your cake and eat it too by not stating openly that Phrogram is and will remain proprietary software that is meant to make money. Apart from all the other great goals for it - I'm not suggesting you're not sincerely interested in improving education. It might even be that this really is the best way to go - I doubt it, but I'm sure you've given it more thought - but you shouldn't be suggesting that at some point it will be open source and cross-platform, because you've obviously chosen not to go that way.

    Can I ask if KPL and Phrogram are copyrighted as names (I guess so) and/or languages (not sure one can even take a patent on a language)? In other words, can others write a compiler for them and present it as such? If not, what guarantee do contributors have that you won't change plans again and make all versions of the compiler/IDE commercial, so that they and others will have to pay (or will have to pay more in the case of Phrogram Standard) to use their own freely donated programs?

    I'm glad you're open to discussion about this. I wouldn't be spending time writing this (and wouldn't have translated the site into Dutch) if I didn't see value in KPL/Phrogram. I'm just sceptical about the road it's taking now.  

  •  09-29-2006, 9:00 AM 744 in reply to 740

    Re: Substring behaviour I don't understand

    This is a big question that deserves a good and well-thought out response - we're discussing it among the team. We're also working at getting the official release finished and out by Monday, so it probably will be after that before we have that good and well-thought out response done. But I will have one. :)
View as RSS news feed in XML