Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Field Spacing

by vanuatu10 (Initiate)
on May 24, 2002 at 17:38 UTC ( #169158=perlquestion: print w/replies, xml ) Need Help??
vanuatu10 has asked for the wisdom of the Perl Monks concerning the following question:

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,
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. updated by boo_radley : formatting, code tags

Replies are listed 'Best First'.
Re: Field Spacing
by talexb (Canon) on May 24, 2002 at 17:56 UTC
    Check out the built-in function printf and the meaning of the format statement "%30s". From a Unix prompt you could also try man printf which talks about the C equivalent function -- it's quite similar to Perl's.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: Field Spacing
by mr_mischief (Monsignor) on May 24, 2002 at 19:51 UTC
    Take a look at this:
    $data = sprintf('%-30s,%-30s,%-30s", foo, bar, baz); print $data, "\n";
    Also take a look at sprintf and pack.

    Christopher E. Stith
    use coffee;

    Reparented from doomed dup - dvergin 2002-05-24

Re: Field Spacing
by vladb (Vicar) on May 24, 2002 at 17:56 UTC
    So, from what I can take, you need to get this

    Ent                           , Name                      ,
    SCO11                         , RUN                       ,

    from this ...

    Ent, Name,
    SCO11, RUN,
    Ent, Name,
    NA932, TAKE,


    Note: heh, I had to actually do a 'View Source' on your node since apparently you forgot to wrap your 'needed output' in a pair of <pre> tags ;-).

    UPDATE: And here's the solution (to make my post worth at least 2 cents ;-):
    use strict; # 30 characters max! my $max_size = 30; my @fields; while (<DATA>) { chomp; @fields = map { $_ . " "x(30 - length($_)) } split /,/; print join(",", @fields) . ",\n"; } __DATA__ Ent, Name, SCO11, RUN, Ent, Name, NA932, TAKE,
    And the output:
    Ent , Name , SCO11 , RUN , Ent , Name , NA932 , TAKE

    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}
Re: Field Spacing
by krujos (Curate) on May 24, 2002 at 18:23 UTC
    I am not sure excactly how your data will be, but you should consider sing a format for this. It is a lot less hastle than writing "code" to do your spacing for you.
Re: Field Spacing
by greywolf (Priest) on May 24, 2002 at 18:15 UTC
    This is simplified but you can make it work for your needs. Some of the other suggestions might be slicker but this should do the trick.
    my $size = 30; my $field = 'fred'; my $counter = length($field); while ($counter < $size) { $field .= ' '; $counter++; }
    mr greywolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://169158]
Approved by xtype
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-05-26 19:14 GMT
Find Nodes?
    Voting Booth?