string parsing.

BlitzMax Forums/BlitzMax Beginners Area/string parsing.

ckob(Posted 2006) [#1]
Simply put I suck at anything to do with it and everytime I try to figure it out I get unexpected results.

I have a string which returns a file path lets say

c:\whatever\program\somethingelse\

I would like to parse the string so that it strips out everything before the program folder so the string would be.

program\somethingelse\ + whatever sub directories


I know basically I have to search the string for "foldername" then trim everything to the left of it but im unsure how to do it exactly.


H&K(Posted 2006) [#2]
Scan from the right for the first instance of / (or \ to be safe). (Or Find Last)
ie
FindLast:Int( subString:String,startIndex=0 )
Then Right$( str$,n )

(Even if Ive missunderstood the question, its just these commands)


Jesse(Posted 2006) [#3]
see if you can understand this. A more generic code than
H&K. Just an alternative not better.
 
Local a$ = "c:\whatever\program\somethingelse\" ' astring to search
Print a$
Local b% = Instr(a$,"program")   ' search a$ and return location of search word  
Print b						 '
Local c$ = Mid$(a$,b)		 'trim a$ from start of string to character specivied by b  
Print c



ckob(Posted 2006) [#4]
Thanks H&K and thanks Jesse your examples should help me out.


Brucey(Posted 2006) [#5]
Same example as Jesse except using non "retro" code.
Local a$ = "c:\whatever\program\somethingelse\"
Print a$
Local b% = a.find("program")
Print b
Local c$ = a[b..] 
Print c

This uses less function calls, and is therefore (very) slightly more efficient.


H&K(Posted 2006) [#6]
Just an alternative not better

Yours was more useful tho. Im always torn between giving a specific solution or a general solution, but I think in this case yuors was probably better.

@Brusey, obviously yours is just the same, but Jesses is a lot better for the explination of whats happening.
(ie you can see what it does, whereas with yours you have to know. I was going to recomend array resize as well, I actualy had to look Right$ up to check it existed)


Brucey(Posted 2006) [#7]
Well, I didn't think it was worth explaining it all again as it was doing exactly the same thing (on the same lines) in a different way...

Anyhoo, I think the more solutions one can give to a specific problem, the better, and once ckob gets to understand Max more he can start optimizing things in less retro ways...

;-)


Jesse(Posted 2006) [#8]
I don't think its about retro, rather about standards everyone knows. Those commands are known by more than one Basic compiler. And if it makes it easier for someone to not only understand them but be able to grab the code and move it to another compiler and have it work than I think its worth the effort.
I feel I am a better programmers than when we started but when I try to do simething I tend to go back to my roots and compare if there is a similar way of doing it with what I am learning. As we grow in skill we look for shortcuts and better more efficient ways of programming. But for someone who is learning I think the basics standards are as good as any other similar command they can learn.
I don't think anybody is wrong but I feel that for the sake of most BlitzMax "game" programmers, the fastest is the best. For me its not always the case.

cheers :)