我有一个如下所示的日志文件
PROGRAM: Swimming
==>ID
COM-1234
COM-2345
COM-9876
PROGRAM: Running
==>ID
COM-9090
我希望根据程序(游泳,跑步等)对其进行分组,并希望显示如
#!/usr/bin/perl
use Data::Dumper;
$/ = "%%%%";
open( AFILE, "D:\\mine\\out.txt");
while (<AFILE>)
{
@temp = split(/-{20,}/, $_);
}
close (AFILE);
my %hash = @new;
print Dumper(\%hash);
我对Perl很新,我写了下面的部分(不完整)。
<textarea class="form-control" maxlength="{{desc}}" my-maxlength = "{{desc}}" maxlen="maxlen" type="text" ng-model="problem.DESCRIPTION" aria-describedby="qDesc" id="questionDescription" name="questionDescription" ng-required="caseType" lang-check></textarea>
<p ng-show="problem.DESCRIPTION.length > (desc-5)" class="text-danger pull-right" id="qDesc">{{ desc - problem.DESCRIPTION.length}} {{ resource["textcount.sublabel.maximumcharacters"] }</p>
我从perl教程中读到,哈希键值对将采用具有多个值的唯一键但不确定如何使用它。
我能够读取文件并存储到哈希中,不确定如何处理上述格式。非常感谢任何帮助。谢谢。
答案 0 :(得分:3)
我总是喜欢编写这样的程序,所以他们从STDIN读取,因为它使它们更灵活。
我会这样做:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
# Read one "chunk" of data at a time
local $/ = '------------------------------';
# A hash to store our results.
my %program;
# While there's data on STDIN...
while (<>) {
# Remove the end of record marker
chomp;
# Skip any empty records
# (i.e. ones with no printable characters)
next unless /\S/;
# Extract the information that we want with a regex
my ($id, $program) = /ID: (.+?)\n.*Program: (.+?)\n/s;
# Build a hash of arrays containing our data
push @{$program{$program}}, $id;
}
# Now we have all the data we need, so let's display it.
# Keys in %program are the program names
foreach my $p (keys %program) {
say "PROGRAM: $p\n==>ID";
# $program{$p} is a reference to an array of IDs
say "\t$_" for @{$program{$p}};
say '';
}
假设这是在一个名为programs.pl
的程序中且输入数据在programs.txt
中,那么你就像这样运行它:
C:/> programs.pl < programs.txt
答案 1 :(得分:1)
始终将 class ApplicationInterceptorChain implements Interceptor.Chain {
private final int index;
private final Request request;
private final boolean forWebSocket;
ApplicationInterceptorChain(int index, Request request, boolean forWebSocket) {
this.index = index;
this.request = request;
this.forWebSocket = forWebSocket;
}
@Override public Connection connection() {
return null;
}
@Override public Request request() {
return request;
}
@Override public Response proceed(Request request) throws IOException {
// If there's another interceptor in the chain, call that.
if (index < client.interceptors().size()) {
Interceptor.Chain chain = new ApplicationInterceptorChain(index + 1, request, forWebSocket);
Interceptor interceptor = client.interceptors().get(index);
Response interceptedResponse = interceptor.intercept(chain);
if (interceptedResponse == null) {
throw new NullPointerException("application interceptor " + interceptor
+ " returned null");
}
return interceptedResponse;
}
// No more interceptors. Do HTTP.
return getResponse(request, forWebSocket);
}
}
和use warnings;
放在程序的顶部。并始终使用use strict;
open
输出
open my $fh, "<", "D:\\mine\\out.txt";
my %hash;
while (<$fh>){
if(/ID/)
{
my $nxt = <$fh>;
s/.*?ID: //g;
$hash{"$nxt==>ID \n"}.=" $_";
}
}
print %hash;
我在Program: Running
==>ID
COM-9090
Program: Swimming
==>ID
COM-1234
COM-2345
COM-9876
之后的行找到了您的输入文件program
。所以我用过
ID
现在该程序存储在my $nxt = <$fh>;
变量中。
答案 2 :(得分:0)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = ();
open my $IN, "<", "your file name here" or die "Error: $!\n";
while (<$IN>) {
if ($_ =~ m/^\s*-*ID:\s*COM/) {
(my $id) = ($_ =~ m/\s*ID:\s*(.*)/);
my $prog_name = <$IN>;
chomp $prog_name;
$prog_name =~ s/Program/PROGRAM/;
$hash{$prog_name} = [] unless $hash{$prog_name};
push @{$hash{$prog_name}}, $id;
}
}
close $IN;
print Dumper(\%hash);
输出将是:
$VAR1 = {
'PROGRAM: Running' => [
'COM-9090'
],
'PROGRAM: Swimming' => [
'COM-1234',
'COM-2345',
'COM-9876'
]
};
让我们看看这两行:
$hash{$prog_name} = [] unless $hash{$prog_name};
push @{$hash{$prog_name}}, $id;
如果散列未定义,则第一行启动空数组引用作为值。第二行将ID推送到该数组的末尾(无论第一行如何)。
此外,第一行不是强制性的。 Perl知道你的意思,如果你只是写push @{$hash{$prog_name}}, $id;
并解释它,好像你说“转到这个键的值”并创建它,如果它还没有。现在你说该值是一个数组,然后将$id
推送到列表中。