Page 1 of 1 1
Topic Options
#14525 - 2001-11-22 04:10 PM pre-defined string length
cadey Offline
Lurker

Registered: 2001-11-22
Posts: 2
Loc: England
I would like to know if I can define a string variable so that whatever is assigned to it, it still has a fixed length.
thanks.

Top
#14526 - 2001-11-22 04:29 PM Re: pre-defined string length
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Hi cadey, welcome

Doing this is not a simple thing ... if your running KiXtart version 4.0, might want to consider making a UDF out of this ...

Set $variable to a string and any length and this script will convert it into a fixed-length variable of (in this case) 20 chars ... if the variable is too big, it will be trunctated ... if the variable is too small - it will be paddded with spaces (from the $fixedmask string) ... anyone got a better way of doing this ?

code:

break on


$variable = "kixtart"


$fixedmask = " " ; set this to be a string of spaces of fixed length


$fixedvariable = substr($variable,1,len($fixedmask))+substr($fixedmask,1,len($fixedmask)-len($variable))


?"fixedvariable=$fixedvariable len=" len($fixedvariable)


exit 1



-Shawn

[ 22 November 2001: Message edited by: Shawn ]

Top
#14527 - 2001-11-22 04:38 PM Re: pre-defined string length
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Here's a quicky UDF:

Anyone got a better name for this UDF ?

code:

break on


$variable = "kixtart"


$fixed = PadRight($Variable,50)


?"fixed = $fixed" + "*"
?"len = " len($fixed)


exit 1


function padright($string,$count)
dim $i,$padding
for $i = 0 to $count
$padding = $padding + " "
next
$padright = substr($string,1,$count)+substr($padding,1,$count-len($string))
endfunction


-Shawn

[ 22 November 2001: Message edited by: Shawn ]

Top
#14528 - 2001-11-22 05:25 PM Re: pre-defined string length
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
I've seen a function like that in BBx (ancient language). It was called FILL

That function also had an optional filler...

FILL($string, length , [Filler])

So you could fill it with zero's or underscores or ...

_________________________
The Code is out there

Top
#14529 - 2001-11-22 05:32 PM Re: pre-defined string length
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Then Fill() it is !!!

Or how about FillRight() and FillLeft() ?

-Shawn

Top
#14530 - 2001-11-22 05:58 PM Re: pre-defined string length
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Anybody feel like giving this a spin ? The syntax is :

$string = fillright("kixtart",10)

or:

$string = fillright("kixtart",50,"*")

hmmm ... think maybe i like padleft() and padright better ... fillleft() has too many L's in it ?


code:

function FillRight($string,$length,optional $filler)

dim $i,$padding

$fillright = ""

if vartypename($string) <> "string"
return
endif

if vartypename($length) <> "long"
return
else
if $length < 0
return
endif
endif

if $filler and vartypename($filler) = "string"
$filler = substr($filler,1,1)
else
$filler = " "
endif

for $i = 0 to $length
$padding = $padding + $filler
next

$fillright = substr($string,1,$length) + substr($padding,1,$length-len($string))

endfunction


-Shawn

[ 22 November 2001: Message edited by: Shawn ]

Top
#14531 - 2001-11-22 06:10 PM Re: pre-defined string length
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
I'm trying to post something, but the BB is working against me

I constantly get the message:

Sorry, we do not permit this HTML tag: Parenthesis in HTML tag

_________________________
The Code is out there

Top
#14532 - 2001-11-22 06:14 PM Re: pre-defined string length
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Roger ... Henri has disabled posts that contains HTML tags ... we used to be able to but no more ... the only way i've found to do it is to break up your tags into seperate strings and stitch them together using string math, like this :

$html = "<" + "BODY" + ">" + ; all the rest

it's awefull but it's the only way i've found so far !

-Shawn

[ 22 November 2001: Message edited by: Shawn ]

Top
#14533 - 2001-11-22 06:14 PM Re: pre-defined string length
cadey Offline
Lurker

Registered: 2001-11-22
Posts: 2
Loc: England
Thanks Shawn, I've used the UDF FillRight idea with the $filler variable, and this is working great.
All power to this board! What a resource!

Top
#14534 - 2001-11-22 06:23 PM Re: pre-defined string length
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
So, according to the Function Rules , they would look something like this:

code:

Function RFill($_string, $_count, Optional $_filler)

Dim $_string

if $_filler='' $_filler=' ' endif

while len($_string )LESS THAN SIGNval($_count) $_string=$_string+$_filler loop

$RFill = substr($_string,1,$_count)

Endfunction


Function LFill($_string, $_count, Optional $_filler)

Dim $_string

if $_filler='' $_filler=' ' endif

while len($_string )LESS THAN SIGNval($_count) $_string=$_filler+$_string loop

$LFill = substr($_string,len($_string)-$_count+1,$_count)

Endfunction


So you could do stuff like this:

code:

$test=RFill("MyName",30)
? $test ' = ' len($test)

$test=LFill("2342",30,"0")
? $test ' = ' len($test)

$test=RFill("Me!",30,"WithApples")
? $test ' = ' len($test)


[ 22 November 2001: Message edited by: DrillSergeant ]

_________________________
The Code is out there

Top
#14535 - 2001-11-22 06:26 PM Re: pre-defined string length
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands

I wasn't allowed to put a LESS THAN (Start HTML code) sign in the post without a GREATER THAN (End HTML code)

I think this is not very handy

Please notice the LESS THAN SIGN remark in the Code and replace this by the real sign (that I can't post here)

[ 22 November 2001: Message edited by: DrillSergeant ]

_________________________
The Code is out there

Top
#14536 - 2001-11-22 06:50 PM Re: pre-defined string length
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
The wonderful world of new and improved BB !

Nearly all in < tags > is stripped !

btw... why add the rulez of udf to function arguments ??? Afaik they are completely internal to the udf ...

greetz
J.

_________________________



Top
#14537 - 2001-11-22 07:09 PM Re: pre-defined string length
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Roger,

Brilliant - your UDF's are (IMHO) clean and eloquent ...

Couple of things:

1) The Function Rules apply mostly to global vars so I wouldn't get too fussy about that. IMHO - function parms and locally dimmed one's can be called anything because they're invisible anyway - plus (IMHO) cluttering one's script with over-zealous naming conventions tends to distract (me) more than help (me) ... MHO

2) I really like your UDF names RFILL() and LFILL() - that's the ticket my friend ...

[rant on]

If i was you - i would slap a UDF banner on your script and post them to the UDF forum ASAP !!! That forum is going nowhere fast and unless either you, me or someone else bites the bullet - it's going to linger and die ... if you don't, I will

These UDF's (we three) just developed are good .. too good to let slip into the BBS bit-bucket ... and it's also testimony to the great things that can develop when a thread is allowed to grow and evolve !!!

[rant off]

-Shawn

[ 22 November 2001: Message edited by: Shawn ]

Top
#14538 - 2001-11-22 08:06 PM Re: pre-defined string length
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Hi Shawn,

Thanx for the compliments

I tried to post the next message, but the CODE part gave me that depressing HTML message again

Maybe you can post it, and explain to me one more time how I can post stuff again

TO Henry: Can the HTML/UBB code be taken up as an option, so that the poster can turn them off or on if they want it?

Ok, this is what i wanted to post:

Syntax: RFILL ("String", Length , "Filler")

Parameters:

String
The string that you want to be filled

Length
The length to wich the string must be filled

Filler (Optional)
The character(s) with wich to fill up the string,
if this value is omitted a SPACE will be used as the filler

Returns: The input string filled to the specified length

See Also: LFILL

Examples:

$fill = RFILL("MyName",30); Adds 24 spaces to 'MyName'

$fill = RFILL("",30,"X"); Creates a string of thirty x's.

$fill = RFILL("ThisBox",40,"WithApples"); Adds 'WithApples' to 'ThisBox' untill the length (40) is reached.

Code:

code:
code here

_________________________
The Code is out there

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1808 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.077 seconds in which 0.037 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org