Re: Right justify
Posted in 1999
Topics: General Discussion
Bill Zega wrote: > > Thanks for repsponding but... I need the function to be generic for variable > length strings (MAX char(100)). Example: > > define g_8 char(8), > g_50 char(50) > > let g_8 = rightshift("123456 ") > # Will return " 1234" (four spaces,"1234") > # I need it to return " 123456" (two spaces,"123456") > let g_50 = rightshift("123456 ") > # Will also return " 1234" (four spaces,"1234") > # I need it to return " 123456" (forty-four spaces,"123456") > > Paul Roberts wrote in message <7f7pa0$3bo1@webint.na.informix.com>... > >In article <7f7hm9$l83$1@news.monmouth.com>, > > >Like this, maybe? > > > > function rightshift(a_string) > > > > define a_string, ret_val char(10) > > define i smallint > > > > let i = length(a_string) > > let ret_val = (10-i) spaces, a_string clipped > > return ret_val > > > > end function { rightshift } In that case, you have to tell the function how wide the output should be: LET g_50 = rightshift("123456 ", 50) function rs(str, len) define str char(100), len integer define i integer define ret char(100) let i = length(str) let ret = (len - i) spaces, str clipped return ret end function Untested - nowhere near a compiler. The version I use is rjust() and is written in C. You should check for invalid lengths of both the string and the field width, and (probably) null strings. -- Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net) Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN #include <disclaimer.h>
I was hoping to perform this function without passing the length. I guess what I really need is pointers so I can pass the address of the variable, not the contents of the variable. I am new to Informix 4gl but it looks like there is no access to pointers, true? Jonathan Leffler wrote in message >In that case, you have to tell the function how wide the output >should be: > >LET g_50 = rightshift("123456 ", 50) > >function rs(str, len) > define str char(100), len integer > define i integer > define ret char(100) > let i = length(str) > let ret = (len - i) spaces, str clipped > return ret >end function > >Untested - nowhere near a compiler. The version I use is rjust() >and is written in C. You should check for invalid lengths of both >the string and the field width, and (probably) null strings. > >-- >Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net) >Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN >#include <disclaimer.h> >