forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


String manipulation - Padleft & Padright

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Visual DialogScript 3 Source Code
View previous topic :: View next topic  
Author Message
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Wed Sep 04, 2002 8:24 am    Post subject: String manipulation - Padleft & Padright Reply with quote

While I was at it, I thought that I might as well do a couple of padding routines also Wink

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
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Wed Sep 04, 2002 9:27 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Wed Sep 04, 2002 10:12 am    Post subject: Reply with quote

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. Wink

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
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Wed Sep 04, 2002 10:21 am    Post subject: Reply with quote

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. Wink


Hiya Dread, Smile

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... Confused

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Wed Sep 04, 2002 10:34 am    Post subject: Reply with quote

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. Smile

Dread

_________________
~~ Alcohol and calculus don't mix... Don't drink and derive! ~~

String.DLL * advanced string processing
Back to top
View user's profile Send private message
Mac
Professional Member
Professional Member


Joined: 08 Jul 2000
Posts: 1585
Location: Oklahoma USA

PostPosted: Wed Sep 04, 2002 10:41 am    Post subject: Reply with quote

LOL Dread, Smile

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. Wink

Cheers, Mac Smile

_________________
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
View user's profile Send private message Send e-mail
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Wed Sep 04, 2002 10:51 am    Post subject: Reply with quote

Yeah, Mac!

I like that one better - now it's short AND fast. Cool

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
View user's profile Send private message
Dr. Dread
Professional Member
Professional Member


Joined: 03 Aug 2001
Posts: 1065
Location: Copenhagen, Denmark

PostPosted: Wed Sep 04, 2002 2:16 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Visual DialogScript 3 Source Code All times are GMT
Page 1 of 1

 
Jump to:  
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

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group