#14525 - 2001-11-22 04:10 PM
pre-defined string length
|
cadey
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
   
Registered: 1999-08-13
Posts: 8611
|
Hi cadey, welcomeDoing 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
   
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
|
|
|
|
#14530 - 2001-11-22 05:58 PM
Re: pre-defined string length
|
Shawn
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
|
|
|
|
#14533 - 2001-11-22 06:14 PM
Re: pre-defined string length
|
cadey
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
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
|
|
|
|
#14537 - 2001-11-22 07:09 PM
Re: pre-defined string length
|
Shawn
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
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 1047 anonymous users online.
|
|
|