如何显示示例数据中的哈希值



我现在正在学习perl,我想请帮忙回答这个练习。 我的目标是显示 PartID 1,2,3 的哈希值 样品输出仅显示批次、晶圆、程序、版本、测试名称、测试编号、Hilimit、LOLIMIT 和 partid 值。

示例数据

lot=lot123
wafer=1
program=prgtest
version=1
Testnames,T1,T2,T3
Testnumbers,1,2,3
Hilimit,5,6,7
Lolimit,1,2,3
PartID,,,,
1,3,0,5
2,4,3,2
3,5,6,3

这是我的代码:

#!/usr/bin/perl
use strict;
use Getopt::Long;
my $file = "";
GetOptions ("infile=s" => $file ) or die("Error in command line argumentsn");
my $lotid = "";
open(DATA, $file) or die "Couldn't open file $file";
while(my $line = <DATA>) {
#print "$line";
if ( $line =~ /^lot=/ ) {
#print "$line n";
my ($dump, $lotid) = split /=/, $line;
print "$lotidn";
}
elsif ($line =~ /^program=/ ) {
my ($dump, $progid) = split /=/, $line;
print "$progid n";  
}
elsif ($line =~ /^wafer=/ ) {
my ($dump, $waferid) = split /=/, $line;
print "$waferid n";  
}
elsif ($line =~ /^version=/ ) {
my ($dump, $verid) = split /=/, $line;
print "$verid n";  
}
elsif ($line =~ /^testnames/i) {
my ($dump, @arr) = split /,/, $line;
foreach my $e (@arr) {
print $e, "n";
}
}
elsif ($line =~ /^testnumbers/i) {
my ($dump, @arr1) = split /,/, $line;
foreach my $e1 (@arr1) {
print $e1, "n";
}
}
elsif ($line =~ /^hilimit/i) {
my ($dump, @arr2) = split /,/, $line;
foreach my $e2 (@arr2) {
print $e2, "n";
}
}
elsif ($line =~ /^lolimit/i) {
my ($dump, @arr3) = split /,/, $line;
foreach my $e3 (@arr3) {
print $e3, "n";
}
}
}

请帮助添加到我的代码中以显示 Partid 1,2,3 哈希。

所以我稍微重写了你的代码,以使用一些更现代的Perl习语(以及一些注释来解释我所做的(。我添加的位靠近底部。

#!/usr/bin/perl
use strict;
# Added 'warnings' which you should always use
use warnings;
# Use say() instead of print()
use feature 'say';
use Getopt::Long;
my $file = "";
GetOptions ("infile=s" => $file)
or die ("Error in command line argumentsn");
# Use a lexical variable for a filehandle.
# Use the (safer) 3-argument version of open().
# Add $! to the error message.
open(my $fh, '<', $file) or die "Couldn't open file $file: $!";
# Read each record into $_ - which makes the following code simpler
while (<$fh>) {
# Match on $_
if ( /^lot=/ ) {
# Use "undef" instead of a $dump variable.
# split() works on $_ by default.
my (undef, $lotid) = split /=/;
# Use say() instead of print() - less punctuation :-)
say $lotid;
}
elsif ( /^program=/ ) {
my (undef, $progid) = split /=/;
say $progid;  
}
elsif ( /^wafer=/ ) {
my (undef, $waferid) = split /=/;
say $waferid;  
}
elsif ( /^version=/ ) {
my (undef, $verid) = split /=/;
say $verid;  
}
elsif ( /^testnames/i) {
my (undef, @arr) = split /,/;
# Changed all of these similar pieces of code
# to use the same variable names. As they are
# defined in different code blocks, they are 
# completely separate variables.
foreach my $e (@arr) {
say $e;
}
}
elsif ( /^testnumbers/i) {
my (undef, @arr) = split /,/;
foreach my $e (@arr) {
say $e;
}
}
elsif ( /^hilimit/i) {
my (undef, @arr) = split /,/;
foreach my $e (@arr) {
say $e;
}
}
elsif ( /^lolimit/i) {
my (undef, @arr) = split /,/;
foreach my $e (@arr) {
say $e;
}
}
# And here's the new bit.
# If we're on the "partid" line, then read the next
# three lines, split each one and print the first
# element from the list returned by split().
elsif ( /^partid/i) {
say +(split /,/, <$fh>)[0] for 1 .. 3;
}
}

更新:顺便说一下,此代码中的任何地方都没有哈希:-(

更新2:我刚刚意识到你只有三种不同的方法来处理数据。因此,您可以通过使用稍微复杂的正则表达式来大大简化代码。

#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Getopt::Long;
my $file = "";
GetOptions ("infile=s" => $file)
or die ("Error in command line argumentsn");
open(my $fh, '<', $file) or die "Couldn't open file $file: $!";
while (<$fh>) {
# Single value - just print it.
if ( /^(?:lot|program|wafer|version)=/ ) {
my (undef, $value) = split /=/;
say $value;
}
# List of values - split and print.
elsif ( /^(?:testnames|testnumbers|hilimit|lolimit)/i) {
my (undef, @arr) = split /,/;
foreach my $e (@arr) {
say $e;
}
}
# Extract values from following lines.
elsif ( /^partid/i) {
say +(split /,/, <$fh>)[0] for 1 .. 3;
}
}

最新更新