Perl 如何显示示例数据中的哈希值
我现在正在学习perl,我想寻求帮助来回答这个练习。 我的目标是显示PartID 1,2,3的散列值 示例输出仅显示批次、晶圆、程序、版本、测试名称、测试编号、hilimit、lolimit和partid值 样本数据Perl 如何显示示例数据中的哈希值,perl,Perl,我现在正在学习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,
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 arguments\n");
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 "$lotid\n";
}
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";
}
}
}
#/usr/bin/perl
严格使用;
使用Getopt::Long;
我的$file=“”;
GetOptions(“infle=s”=>\$file)或die(“命令行参数中的错误”);
我的$lotid=“”;
打开(数据$file)或死亡“无法打开文件$file”;
while(我的$line=){
#打印“$line”;
如果($line=~/^lot=/){
#打印“$line\n”;
我的($dump,$lotid)=拆分/=/,$line;
打印“$lotid\n”;
}
elsif($line=~/^program=/){
我的($dump,$progid)=拆分/=/,$line;
打印“$progid\n”;
}
elsif($line=~/^wafer=/){
我的($dump,$waferid)=拆分/=/,$line;
打印“$waferid\n”;
}
elsif($line=~/^version=/){
我的($dump,$verid)=拆分/=/,$line;
打印“$verid\n”;
}
elsif($line=~/^testnames/i){
我的($dump,@arr)=拆分/\,/,$line;
每个我的$e(@arr){
打印$e,“\n”;
}
}
elsif($line=~/^testnumbers/i){
我的($dump,@arr1)=拆分/\,/,$line;
兑换我的$e1(@arr1){
打印$e1,“\n”;
}
}
elsif($line=~/^hilimit/i){
我的($dump,@arr2)=拆分/\,/,$line;
兑换我的$e2(@arr2){
打印$e2,“\n”;
}
}
elsif($line=~/^lolimit/i){
我的($dump,@arr3)=拆分/\,/,$line;
付我的$e3(@arr3){
打印$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 arguments\n");
# 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;
}
}
#/usr/bin/perl
严格使用;
#添加了您应始终使用的“警告”
使用警告;
#使用say()代替print()
使用特征“说”;
使用Getopt::Long;
我的$file=“”;
GetOptions(“infle=s”=>\$file)
或死(“命令行参数中的错误\n”);
#对文件句柄使用词法变量。
#使用open()的(更安全的)3参数版本。
#加美元!返回错误消息。
打开(我的$fh,'我想你需要更清楚一点。PartID
行有三个空字段。如何获得“PartID 1,2,3”从这个数据中?它是PartID
行之后三行中的第一个字段吗?在上面的示例数据中,我声明它已经是PartID,,,,,,1,3,0,5,2,4,3,2,3,5,6,3散列的第一个数字(1,2,3)需要显示为输出正如我之前问的,这是后面三行中的第一个数字吗?您需要准确描述如何从输入数据到“PartID 1,2,3”。程序员的工作是将模糊的规范转化为工作代码。你的第一项任务始终是定义你将遵循的算法。是的,接下来三行中的第一个数字是正确的。明白了。谢谢。
#!/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 arguments\n");
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;
}
}