Best way to have a formatted output with Perl

前端 未结 4 932
鱼传尺愫
鱼传尺愫 2020-12-15 00:24

I want to output strings into eight columns, but I want to keep the spacing the same. I don\'t want to do it in HTML, but I am not sure how to do it normally. Example:

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 01:11

    You could use Perl's format. This is probably the "complicated" method that you don't understand, most likely because it gives you many options (left|center|right justification/padding, leading 0's, etc).

    Perldoc Example:

    Example:
       format STDOUT =
       @<<<<<<   @||||||   @>>>>>>
       "left",   "middle", "right"
       .
    Output:
       left      middle    right
    

    Here's another tutorial.


    Working Example: (Codepad)

    #!/usr/bin/perl -w    
    
    use strict; 
    
       sub main{    
          my @arr = (['something1','something2','something3','something4','something5','something6','something7','something8']
                    ,['else1'     ,'else2'     ,'else3'     ,'else4'     ,'else5'     ,'else6'     ,'else7'     ,'else8'     ]
                    ,['another1'  ,'another2'  ,'another3'  ,'another4'  ,'another5'  ,'another6'  ,'another7'  ,'another8'  ]
                    );
          
          for my $row (@arr) {
             format STDOUT =
    @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  @<<<<<<<<<<<<  
             @$row
    .
             write;
          }
    
       }    
           
       main();   
    

提交回复
热议问题