在perl中有“printf中缺少参数在…”吗?

在perl中有“printf中缺少参数在…”吗?,perl,Perl,这是一篇文章的转载。虽然我接受了答案,但我在印刷方面仍然有问题。我最初的帖子姿势不好,因为我应该包括一个数据样本。下面我举一个我想重写的数据的例子。我有几个文件,每个文件的数据格式和数据长度略有不同。我给出以下两个文件的数据样本。我必须删除非数字字符,然后只打印数据。 数据有一个标题,我想按原样打印 Date;Time;Time zone;Wind Speed;Wind Direction;Battery Voltage;Temperature;Relative Humidity;Baromet

这是一篇文章的转载。虽然我接受了答案,但我在印刷方面仍然有问题。我最初的帖子姿势不好,因为我应该包括一个数据样本。下面我举一个我想重写的数据的例子。我有几个文件,每个文件的数据格式和数据长度略有不同。我给出以下两个文件的数据样本。我必须删除非数字字符,然后只打印数据。 数据有一个标题,我想按原样打印

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,北极熊和其他人

我将非常感谢任何进一步的帮助

#!/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

向我们展示不起作用的代码。从您的问题看,数据应该如何打印出来并不明显。第一个标题很容易解析,但对于第二个标题,我们重复了与温度相关的第3列时间。第一个和第二个文件中的时间具有不同的格式-输出中应使用哪种格式-00:15或23:45:00?你是说所有单位和状态字段都可以省略吗?第一个和第二个文件具有不同的数据列。由于数据集不同,它们不能合并到一个表中,应作为不同的实体打印。您的问题是,需要对最终输出进行重大澄清-是打印到终端还是打印到文件中?通过一些代码向我们展示您对问题的看法,以展示您在解决方案方面所做的一些努力。感谢所有这些评论。我已经提供了代码。我省略了目录的定义,使代码有点简短。情况是,在其各自的目录中有不同的站点,并且每天都有一个文件,其中包含当天的日期作为名称的一部分。要按原样打印的第一行,无需编辑。要打印的值只是一个系列,上面没有标题。数据被写入一个文件。每个工作站都有自己的输入和输出文件。我希望这能澄清问题。正如您在上一个问题中所解释的,您的printf格式的说明符比参数多。您需要确保@data对于格式中的每个说明符都有一个元素。那么,回到调试中,看看您是如何构建@data的。非常感谢您@Polar Bear。请您为我重新格式化输出,以便每次的报告都在一行上。我得写一整天的数据。类似于:yyyy-mm-dd hh:mm值。。。价值我不介意变量名。只要有第一行数据就足够了,这样我就可以知道变量是什么。我提供的数据示例来自两个不同的文件,因此它们将被写入两个不同的文件。我真的很感谢你抽出时间来。@ziloremmba-我已经请你澄清你的问题了。理想情况下,你应该在你的问题中添加所需的输出——否则我们无法读懂你的心思。我确实编写了我脑海中的代码,从某种意义上讲,在手头的数据集上,输出是如何显示的——但我的猜测可能与您希望看到的相去甚远。无限感谢@Polar Bear。请原谅,因为我的不精确,让你花了太多的时间。我道歉。您作为输出给出的最后一个表正是我想要的输出方式。但是,我不需要表中的行,因为接下来我想用Fortran读取数据。这远远超出了我所知道的基本Perl。所以,请原谅,如果我可以请你只编译代码的一部分,给我你的最后一个表,包括从一个文件读取。我真的很感激。我希望这次我说得够清楚了。我再次为要求太高而道歉。我设法用我的数据运行了你的代码,并且能够按要求打印。非常感谢您让我提高了Perl知识。
-- 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 |
+------------+-------+-----------+------------+----------------+-----------+-------------+