perl - 在 perl 中出现“printf at ... 中缺少参数”?
问题描述
这是为什么我在 perl 中有“printf at ... 中缺少参数”的转贴?. 虽然我接受了答案,但我仍然遇到打印问题。我原来的帖子不是很好,因为我应该包含一个数据样本。我在下面给出一个我想重写的数据的例子。我有几个文件,每个文件的数据格式和数据长度都略有不同。我从两个文件中给出以下数据样本。我必须删除非数字字符,然后只打印数据。数据有一个标题,我想按原样打印。
Date;Time;Time zone;Wind Speed;Wind Direction;Battery Voltage;Temperature;Relative Humidity;Barometric Pressure;Pyranometer 0 - 2000 W/m²;Accumulated Total NRT;Wind Gust
2020-05-12;00:15;CAT;0.81;116.6;6.59;10.5;96.8;883.1;0.0;0.1;2.97
2020-05-12;00:30;CAT;2.18;39.1;6.59;10.3;97.3;883.1;0.0;0.1;4.34
Date Time Time zone Dew point Dew point_Unit Dew point_Status Temperature (MID) Temperature (MID)_Unit Temperature (MID)_Status Temperature (MID) Temperature (MID)_Unit Temperature (MID)_Status Temperature (MID) Temperature (MID)_Unit Temperature (MID)_Status Precipitation (SUM) Precipitation (SUM)_Unit Precipitation (SUM)_Status Relative Humidity (AVG) Relative Humidity (AVG)_Unit Relative Humidity (AVG)_Status
2020/05/11 23:45:00 CAT 12,4 °C OK 17,9 °C OK 17,9 °C OK 17,9 °C OK 0,0 mm OK 100,0 % RH OK
2020/05/12 00:00:00 CAT 12,4 °C OK 17,9 °C OK 17,9 °C OK 17,9 °C OK 0,0 mm OK 100,0 % RH OK
我非常感谢那些在我之前的帖子中帮助我的人 Ikegami、Polar Bear 和其他人的努力。
我将非常感谢任何进一步的帮助。
#!/usr/bin/perl -w
use strict;
use warnings;
use POSIX qw{strftime};
use File::Path;
use File::Copy;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;
my $debug = 1;
my @now = localtime;
my $today = strftime('%Y%m%d', localtime(time -86400));
chdir $AWS_DataDirs or die "chdir failed on '$AWS_DataDirs': $! ($^E)";
for (my $jj=0; $jj < @AWS_Dirs; ++$jj)
{
opendir my $in_dir, $AWS_Dirs[$jj] or die "opendir failed on $AWS_Dirs[$jj]: $! ($^E)";
while (my $file=readdir $in_dir) #reading input directory
{
next if -d $file;
next unless $file =~ /$today/;
if($file =~ /$today/)
{
open (IN, "< $AWS_Dirs[$jj]/$file") or die "open '$file': failed $! ($^E)";
open (OUT, "> $Output_dir/$file_out[$jj]") or die "open '$file_out[$jj]': failed $! ($^E)";
# copy the first line unchanged
print OUT scalar(<IN>) for 1..1;
while( <IN> )
{
chomp;
s/\s+/ /g; # strip unneeded spaces before split
s/[\-\:\;\°\*\%]/ /g;
s/\// /g;
s/MISSING/0./g;
s/BAD/0./g;
s/OK/ /g;
s/[A-Za-z]/ /g;
s/,/./g;
my @data = split ' '; # put read data in an array
my $format = "%4d %2d %2d %2d %2d %2d" . " %7.2f" x 12 . "\n";
printf OUT $format, @data; # print data into the file
}
close OUT;
}
}
closedir $in_dir;
}
__END__
解决方案
处理第一类数据文件的演示代码。
注意:增加了一些灵活性以允许选择感兴趣的数据列。
use strict;
use warnings;
use feature 'say';
# Read data
my($columns,$data) = read_data();
# Three ways to output data
say_info($columns,$data);
say_table_v($columns,$data);
say_table_h($columns,$data);
# Select only columns of interest
my $sel_columns = [@{$columns}[0,1,2,3,4,11,6]];
say_info($sel_columns,$data);
say_table_v($sel_columns,$data);
say_table_h($sel_columns,$data);
#
# Read data
#
# NOTE:
# Make change
# $header = <DATA>; -- read from DATA block
# $header = <>; -- read from file or pipe
#
# while(<DATA>) { -- read from DATA block
# while(<>) { -- read from file or pipe
#
sub read_data {
my($header,@columns,@data);
$header = <DATA>;
chomp $header;
@columns = split(';', $header);
# Obtain data
while(<DATA>) {
chomp;
next if /^\s*$/; # skip empty lines
my %hash;
@hash{@columns} = split(';',$_);
push @data, \%hash;
}
return(\@columns,\@data);
}
# Simple output parameters : values
sub say_info {
my $columns = shift;
my $data = shift;
for my $record (@{$data}) {
say '-- Record ' . '-' x 35;
for my $column (@$columns) {
printf " %10s : %s\n", $record->{$column}, $column;
}
}
}
# Table with data placed vertically
sub say_table_v {
my $columns = shift;
my $data = shift;
my %hash;
my $record_count;
for my $record (@{$data}) {
$record_count++;
push @{$hash{$_}}, $record->{$_} for @$columns;
}
# Find widest header title
my $width = 0;
for (@$columns) {
my $len = length($_);
$len-- if /Pyranometer/; # Compensate for m²
$width = $len > $width ? $len : $width;
}
# Form format string
my $format = "| %-${width}s |" . " %-10s |" x $record_count . "\n";
my $h = '+' . '-' x ($width+2) . '+' . '------------+' x $record_count;
my @header = ('Measurement');
push @header, 'Value' while $record_count--;
say $h;
printf $format, @header;
say $h;
printf $format, $_, @{$hash{$_}} for @$columns;
say $h;
}
# Table with data placed horizontally
sub say_table_h {
my $columns = shift;
my $data = shift;
my $format;
my $div;
# Form format string
for my $column (@$columns) {
my $len = length($column);
$len = 10 if $column eq 'Date'; # Date width 10
$len = 5 if $column eq 'Time'; # Time width 5
$len-- if $column =~ /Pyranometer/; # Due m²
if ( $column =~ /Date|Time/ ) {
$format .= sprintf "| %%-%ds ", $len; # Left adjusted
} else {
$format .= sprintf "| %%%ds ", $len; # Right adjusted
}
$div .= '+' . '-' x ($len+2);
}
$format .= "|\n";
$div .= "+\n";
print $div;
printf $format, @$columns;
print $div;
for my $record (@{$data}) {
printf $format, map { $record->{$_} } @$columns;
}
print $div;
}
__DATA__
Date;Time;Time zone;Wind Speed;Wind Direction;Battery Voltage;Temperature;Relative Humidity;Barometric Pressure;Pyranometer 0 - 2000 W/m²;Accumulated Total NRT;Wind Gust
2020-05-12;00:15;CAT;0.81;116.6;6.59;10.5;96.8;883.1;0.0;0.1;2.97
2020-05-12;00:30;CAT;2.18;39.1;6.59;10.3;97.3;883.1;0.0;0.1;4.34
2020-05-12;00:30;CAT;2.18;39.1;6.59;10.3;97.3;883.1;0.0;0.1;4.34
2020-05-12;00:30;CAT;2.18;39.1;6.59;10.3;97.3;883.1;0.0;0.1;4.34
输出
-- Record -----------------------------------
2020-05-12 : Date
00:15 : Time
CAT : Time zone
0.81 : Wind Speed
116.6 : Wind Direction
6.59 : Battery Voltage
10.5 : Temperature
96.8 : Relative Humidity
883.1 : Barometric Pressure
0.0 : Pyranometer 0 - 2000 W/m²
0.1 : Accumulated Total NRT
2.97 : Wind Gust
-- Record -----------------------------------
2020-05-12 : Date
00:30 : Time
CAT : Time zone
2.18 : Wind Speed
39.1 : Wind Direction
6.59 : Battery Voltage
10.3 : Temperature
97.3 : Relative Humidity
883.1 : Barometric Pressure
0.0 : Pyranometer 0 - 2000 W/m²
0.1 : Accumulated Total NRT
4.34 : Wind Gust
-- Record -----------------------------------
2020-05-12 : Date
00:30 : Time
CAT : Time zone
2.18 : Wind Speed
39.1 : Wind Direction
6.59 : Battery Voltage
10.3 : Temperature
97.3 : Relative Humidity
883.1 : Barometric Pressure
0.0 : Pyranometer 0 - 2000 W/m²
0.1 : Accumulated Total NRT
4.34 : Wind Gust
-- Record -----------------------------------
2020-05-12 : Date
00:30 : Time
CAT : Time zone
2.18 : Wind Speed
39.1 : Wind Direction
6.59 : Battery Voltage
10.3 : Temperature
97.3 : Relative Humidity
883.1 : Barometric Pressure
0.0 : Pyranometer 0 - 2000 W/m²
0.1 : Accumulated Total NRT
4.34 : Wind Gust
+---------------------------+------------+------------+------------+------------+
| Measurement | Value | Value | Value | Value |
+---------------------------+------------+------------+------------+------------+
| Date | 2020-05-12 | 2020-05-12 | 2020-05-12 | 2020-05-12 |
| Time | 00:15 | 00:30 | 00:30 | 00:30 |
| Time zone | CAT | CAT | CAT | CAT |
| Wind Speed | 0.81 | 2.18 | 2.18 | 2.18 |
| Wind Direction | 116.6 | 39.1 | 39.1 | 39.1 |
| Battery Voltage | 6.59 | 6.59 | 6.59 | 6.59 |
| Temperature | 10.5 | 10.3 | 10.3 | 10.3 |
| Relative Humidity | 96.8 | 97.3 | 97.3 | 97.3 |
| Barometric Pressure | 883.1 | 883.1 | 883.1 | 883.1 |
| Pyranometer 0 - 2000 W/m² | 0.0 | 0.0 | 0.0 | 0.0 |
| Accumulated Total NRT | 0.1 | 0.1 | 0.1 | 0.1 |
| Wind Gust | 2.97 | 4.34 | 4.34 | 4.34 |
+---------------------------+------------+------------+------------+------------+
+------------+-------+-----------+------------+----------------+-----------------+-------------+-------------------+---------------------+---------------------------+-----------------------+-----------+
| Date | Time | Time zone | Wind Speed | Wind Direction | Battery Voltage | Temperature | Relative Humidity | Barometric Pressure | Pyranometer 0 - 2000 W/m² | Accumulated Total NRT | Wind Gust |
+------------+-------+-----------+------------+----------------+-----------------+-------------+-------------------+---------------------+---------------------------+-----------------------+-----------+
| 2020-05-12 | 00:15 | CAT | 0.81 | 116.6 | 6.59 | 10.5 | 96.8 | 883.1 | 0.0 | 0.1 | 2.97 |
| 2020-05-12 | 00:30 | CAT | 2.18 | 39.1 | 6.59 | 10.3 | 97.3 | 883.1 | 0.0 | 0.1 | 4.34 |
| 2020-05-12 | 00:30 | CAT | 2.18 | 39.1 | 6.59 | 10.3 | 97.3 | 883.1 | 0.0 | 0.1 | 4.34 |
| 2020-05-12 | 00:30 | CAT | 2.18 | 39.1 | 6.59 | 10.3 | 97.3 | 883.1 | 0.0 | 0.1 | 4.34 |
+------------+-------+-----------+------------+----------------+-----------------+-------------+-------------------+---------------------+---------------------------+-----------------------+-----------+
-- Record -----------------------------------
2020-05-12 : Date
00:15 : Time
CAT : Time zone
0.81 : Wind Speed
116.6 : Wind Direction
2.97 : Wind Gust
10.5 : Temperature
-- Record -----------------------------------
2020-05-12 : Date
00:30 : Time
CAT : Time zone
2.18 : Wind Speed
39.1 : Wind Direction
4.34 : Wind Gust
10.3 : Temperature
-- Record -----------------------------------
2020-05-12 : Date
00:30 : Time
CAT : Time zone
2.18 : Wind Speed
39.1 : Wind Direction
4.34 : Wind Gust
10.3 : Temperature
-- Record -----------------------------------
2020-05-12 : Date
00:30 : Time
CAT : Time zone
2.18 : Wind Speed
39.1 : Wind Direction
4.34 : Wind Gust
10.3 : Temperature
+----------------+------------+------------+------------+------------+
| Measurement | Value | Value | Value | Value |
+----------------+------------+------------+------------+------------+
| Date | 2020-05-12 | 2020-05-12 | 2020-05-12 | 2020-05-12 |
| Time | 00:15 | 00:30 | 00:30 | 00:30 |
| Time zone | CAT | CAT | CAT | CAT |
| Wind Speed | 0.81 | 2.18 | 2.18 | 2.18 |
| Wind Direction | 116.6 | 39.1 | 39.1 | 39.1 |
| Wind Gust | 2.97 | 4.34 | 4.34 | 4.34 |
| Temperature | 10.5 | 10.3 | 10.3 | 10.3 |
+----------------+------------+------------+------------+------------+
+------------+-------+-----------+------------+----------------+-----------+-------------+
| Date | Time | Time zone | Wind Speed | Wind Direction | Wind Gust | Temperature |
+------------+-------+-----------+------------+----------------+-----------+-------------+
| 2020-05-12 | 00:15 | CAT | 0.81 | 116.6 | 2.97 | 10.5 |
| 2020-05-12 | 00:30 | CAT | 2.18 | 39.1 | 4.34 | 10.3 |
| 2020-05-12 | 00:30 | CAT | 2.18 | 39.1 | 4.34 | 10.3 |
| 2020-05-12 | 00:30 | CAT | 2.18 | 39.1 | 4.34 | 10.3 |
+------------+-------+-----------+------------+----------------+-----------+-------------+
推荐阅读
- node.js - 如何拥有相同服务的不同 docker 容器?
- angular - Angular 5个独立的body类
- acumatica - 从自定义屏幕打开销售订单
- javascript - 对于 IE 和 Firefox 中的所见即所得编辑器,添加链接按钮无法正常工作
- python - 如果 CSV 行存在于随机样本中,则更新它
- openstack-keystone - devstack - 如何减少 openstack keystone X-Subject-Token 大小
- c++ - 错误:''无法将'uint8* {aka unsigned int*}'转换为'const emxArray_uint8_T*'?'?
- asynchronous - 将独立的自动化作业与詹金斯管道集成
- google-chrome - captive.apple.com/generate_204 来自 Windows 10 的点击
- javascript - 如何从 MySQL 获取 RowDataPacket 对象?