简体   繁体   中英

Perl script - tables created in real time in terminal output

My script prints data on separate lines in terminal output, it looks like this:

Name: Paul Surname: Doe Age:34
Name: Paulo Surname: Doeson Age:28 
Name: Paulinho Surname: Docsone Age:5
Name: Paulmen Surname: Doee Age:12
Name: Paulchen Surname: Dooe Age:31

And I would like all data to be divided into columns and displayed equally in bars, as below:

Name: Paul     Surname: Doe     Age:34
Name: Paulo    Surname: Doeson  Age:28
Name: Paulinho Surname: Docsone Age:5 
Name: Paulmen  Surname: Doee    Age:12 
Name: Paulchen Surname: Dooe    Age:31 

I note that the output can not be buffered or stored. I would like to achieve this in real time while running the script. Are there any tricks to achieve this? Something like specifying the width of a cell in an HTML table?

EDIT
Really EXAMPLE code:

@arr = q(Name: Paul Surname: Doe Age:34, Name: Paulo Surname: Doeson Age:28, Name: Paulinho Surname: Docsone Age:5, Name: Paulmen Surname: Doee Age:12, Name: Paulchen Surname: Dooe Age:31);

foreach (@arr) {
  # here should be our trick to creating columns
  print $_ # should be print element in columns (without buffering). print now, not after loop end. 
}

First thing that comes into my mind are Perl formats . It's built-in and give's you a whole bunch of means for formatting output. Have a look at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM