View previous topic :: View next topic |
Author |
Message |
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 8:24 am Post subject: String manipulation - Padleft & Padright |
|
|
While I was at it, I thought that I might as well do a couple of padding routines also
Here ya go:
Code: |
%%padchar = " "
%%outlength = 20
%%string = "some string"
REM ##### Padleft routine #####
%%outstring = %%string
if @greater(%%outlength,@len(%%string))
%%diff = @diff(%%outlength,@len(%%string))
%%inc = 0
repeat
%%inc = @succ(%%inc)
%%outstring = %%padchar%%outstring
until @equal(%%inc,%%diff)
end
REM ##### Padright routine #####
%%outstring2 = %%string
if @greater(%%outlength,@len(%%string))
%%diff = @diff(%%outlength,@len(%%string))
%%inc = 0
repeat
%%inc = @succ(%%inc)
%%outstring2 = %%outstring2%%padchar
until @equal(%%inc,%%diff)
end
info input@TAB()= @CHR(34)%%string@CHR(34)@CR()padleft@TAB()= @CHR(34)%%outstring@CHR(34)@CR()padright@TAB()= @CHR(34)%%outstring2@CHR(34)
|
Greetz
Dr. Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing
Last edited by Dr. Dread on Tue Jul 29, 2003 11:29 am; edited 1 time in total |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Sep 04, 2002 9:27 am Post subject: |
|
|
Slightly shorter code maybe?
Code: |
rem -- desired length --
%%dl = 40
rem -- Pad char --
%%pc = " "
%s = "This is a test"
INFO String:@cr()@cr()@chr(34)%s@chr(34)
rem -- Pad left --
if @greater(%%dl, @len(s))
REPEAT
%s = %%pc%s
UNTIL @equal(@len(%s), %%dl)
end
INFO Pad left:@cr()@cr()@chr(34)%s@chr(34)
rem -- Pad Right --
%s = "This is a test"
if @greater(%%dl, @len(s))
REPEAT
%s = %s%%pc
UNTIL @equal(@len(%s), %%dl)
end
INFO Pad Right:@cr()@cr()@chr(34)%s@chr(34)
|
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
Last edited by Mac on Wed Sep 04, 2002 10:37 am; edited 1 time in total |
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 10:12 am Post subject: |
|
|
Hey Mac!
Yup, yours is slightly shorter! And probably somewhat slower. I think what counts is the REPEAT loop, and you will gain
speed by calling as few functions as possible. Mine uses 2 while yours has 6.
Well, that was not the purpose of this post. I added the possibility of having strings longer than the desired length truncated.
Code: |
%%padchar = " "
%%outlength = 20
%%string = "some string"
REM ##### Padleft routine #####
%%outstring = %%string
if @greater(%%outlength,@len(%%string))
%%inc = 0
%%diff = @diff(%%outlength,@len(%%string))
repeat
%%inc = @succ(%%inc)
%%outstring = %%padchar%%outstring
until @equal(%%inc,%%diff)
REM In the next two lines the string is truncated at the RIGHT to %%outlength
REM if it is longer - REM out those two lines if you want long strings to be kept
else
%%outstring = @substr(%%string,1,%%outlength)
end
REM ##### Padright routine #####
%%outstring2 = %%string
if @greater(%%outlength,@len(%%string))
%%inc = 0
%%diff = @diff(%%outlength,@len(%%string))
repeat
%%inc = @succ(%%inc)
%%outstring2 = %%outstring2%%padchar
until @equal(%%inc,%%diff)
REM In the next two lines the string is truncated at the LEFT to %%outlength
REM if it is longer - REM out those two lines if you want long strings to be kept
else
%%outstring2 = @substr(%%string,@diff(@len(%%string),%%outlength),@len(%%string))
end
info input@TAB()= @CHR(34)%%string@CHR(34)@CR()padleft@TAB()= @CHR(34)%%outstring@CHR(34)@CR()padright@TAB()= @CHR(34)%%outstring2@CHR(34)
|
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing
Last edited by Dr. Dread on Tue Jul 29, 2003 11:29 am; edited 1 time in total |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Sep 04, 2002 10:21 am Post subject: |
|
|
Dr. Dread wrote: | Hey Mac!
Yup, yours is slightly shorter! And probably somewhat slower. I think what counts is the REPEAT loop, and you will gain
speed by calling as few functions as possible. Mine uses 2 while yours has 6.
|
Hiya Dread,
Not sure I agree with ya on this... Since VDS is a scripting
language, the more lines it has (generally), the slower it's
gonna run.
Also not exactly sure how ya found 6 functions in my routine
and only 2 in yours...
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 10:34 am Post subject: |
|
|
Hey Mac!
Well actually I just counted the @-functions in the REPEAT loop. If you wanna pad a 10-char string into a 100-char string, you
will have to loop REPEAT 90 times. So 90 times calling 6 functions (such as @len) will be 540 function calls - I'm pretty sure that
that will be slower than doing 90 times 2 calls + some extra lines that are only processed once for each string.
Of course the desired length of the padded string or the number of strings to be processed must be considerable to really make a
difference.
Should be easy to test, though. Just try padding a string to, say, 20000 chars. But really, I wasn't trying to make this a competition of
some kind ... hope you didn't think so.
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Sep 04, 2002 10:41 am Post subject: |
|
|
LOL Dread,
OK, I did re-arrange my code and put the REPEAT
loop inside the IF/END instead of the other way
around, so it eliminates the IF/END being repeated
and also the need for the @greater() function that
was only used if the string was longer than the desired
length.
And I'm in no way offended, I just love short code.
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 10:51 am Post subject: |
|
|
Yeah, Mac!
I like that one better - now it's short AND fast.
I sometimes do string manips in flatfile databases containing more than 200,000 lines and then small things as this will
be really noticeable.
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 2:16 pm Post subject: |
|
|
Thought of another one which increases the speed by greatly reducing the looping involved
Code: |
%%string = "some string"
REM %C is the pad character
%C = " "
%%outlength = 50
REM Now we make a dummy string holding enough pad chars to fill the string later
%%max = %C%C%C%C%C%C%C%C%C%C%C%C%C%C%C%C%C%C%C%C
repeat
%%max = %%max%%max
until @greater(@len(%%max),%%outlength)
REM ##### Padleft routine #####
if @greater(%%outlength,@len(%%string))
%%outstring = @substr(%%max,1,@diff(%%outlength,@len(%%string)))%%string
REM In the next two lines the string is truncated at the RIGHT to %%outlength
REM if it is longer - REM out those two lines if you want long strings to be kept
else
%%outstring = @substr(%%string,1,%%outlength)
end
REM ##### Padright routine #####
if @greater(%%outlength,@len(%%string))
%%outstring2 = %%string@substr(%%max,1,@diff(%%outlength,@len(%%string)))
REM In the next two lines the string is truncated at the LEFT to %%outlength
REM if it is longer - REM out those two lines if you want long strings to be kept
else
%%outstring2 = @substr(%%string,@diff(@len(%%string),@pred(%%outlength)),@len(%%string))
end
info input@TAB()= @CHR(34)%%string@CHR(34)@CR()padleft@TAB()= @CHR(34)%%outstring@CHR(34)@CR()padright@TAB()= @CHR(34)%%outstring2@CHR(34)
|
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|