in reply to Re: Equal line spacing in records of a file
in thread Equal line spacing in records of a file

Is there a way to give equal field spacing of thirty characters in each field of data in front of the comma?
For example: (30 - field data) = additonal spacing Needed Output: Ent , Name , SCO11 , RUN , Current Data: Ent, Name, SCO11, RUN, Ent, Name, NA932, TAKE, I wrote the following code for equal spacing: $counter = 0; foreach(@newstuff){ $counter++; if(m/Ent/){ $counter=0;} if(m/Payor/){ if($counter > $hold){ $hold = $counter; } } } $counter2 = 0; foreach(@newstuff){ $counter2++; if(m/Ent/){ $counter2=0;} if(m/Payor/){ if($counter2 < $hold){ $temp = $counter2; for($temp;$temp < $hold; $temp++){ #for($j=0;$j < $hold; $j++){ s/Payor/\nPayor/; } } } }
How can I make the file have equal field spacing? There has be be a was to count the contents of each line in the array and add, assuming staticval = 30, spaces before the commas of each field minus the charater in each field. In example, "Ent," would turn out to be staticval minus the number of characters found in Ent. This would be Ent followed by twenty-seven spaces and a comma.

Edit kudra, 2002-05-25 Added code tags

Replies are listed 'Best First'.
•Re: Re: Re: Equal line spacing in records of a file
by merlyn (Sage) on May 24, 2002 at 14:32 UTC
Re3: Equal line spacing in records of a file
by dada (Chaplain) on May 24, 2002 at 14:36 UTC
    1. please use <code>..</code> tags when blanks are significant.
    2. sure there's a way: printf("%-30s,", $field);
    cheers,
    Aldo
    __END__ $_=q,just perl,,s, , another ,,s,$, hacker,,print;
Re: Re: Re: Equal line spacing in records of a file
by mrbbking (Hermit) on May 24, 2002 at 15:01 UTC
    print pack('A30', $thing_that_should_be_30_chars_long); # or print pack('A30' x scalar(@stuff_to_pad), @stuff_to_pad);