Perl,在主例程和子例程中移动文件句柄
脚本的目的是从ASCII文件中的某些数据表中grep一些值 我修改了文件 . 现在它几乎不起作用了。我想知道这样移动文件句柄是否合适 用法仍然是一样的Perl,在主例程和子例程中移动文件句柄,perl,filehandle,Perl,Filehandle,脚本的目的是从ASCII文件中的某些数据表中grep一些值 我修改了文件 . 现在它几乎不起作用了。我想知道这样移动文件句柄是否合适 用法仍然是一样的 myscript.pl targetfolder/*> result.csv F是我的文件句柄 我传递给子例程的参数是标量$\ucode>,它由if条件使用。当我想在子程序中向下移动时,如果1..4不起作用,那么我重复$a=几次以实现向下移动文件句柄 但我认为这不是在我的主代码和子例程中移动相同文件句柄的正确方法。我不确定它是否真的会贯
myscript.pl targetfolder/*> result.csv
F
是我的文件句柄
我传递给子例程的参数是标量$\ucode>,它由if
条件使用。当我想在子程序中向下移动时,如果1..4
不起作用,那么我重复$a=代码>几次以实现向下移动文件句柄
但我认为这不是在我的主代码和子例程中移动相同文件句柄的正确方法。我不确定它是否真的会贯穿每一行。我需要你的建议
myscript.pl
#报告条
使用警告;
严格使用;
##打印标题
Tfms2();
##打印标题
打印“\n”;
@ff=;
每小时$ff(@ff){
开放式(F$ff);
@fswf=@fschuck=@fsxpos=@fsypos=@fsdev=@csnom=“”;
@cswf=@cschuck=@csxpos=@csypos=@csnom=“”;#有没有有效的方法?
而(){
Mfms2();
Mfms3();
}
$r=1;
虽然($r你忘了告诉我们这个程序应该做什么,所以很难提供任何帮助。在你之前的问题中可能有更多的信息,但我们并没有阅读这里的每个问题,你甚至没有包含到你之前问题的链接
您的问题的答案是,您可以使用移动到文件中的任意位置。您可能会发现,查看哪些位置可以告诉您当前在文件中的位置也很有用
但我不认为这些信息对您特别有帮助,因为您似乎对自己正在尝试做的事情感到困惑。如果您要更详细地解释您的任务,那么我强烈怀疑我们可以帮助您(我还怀疑,帮助很大程度上涉及从头重写您的代码)。但在您提供详细信息之前,我们所能做的就是指出您的代码中一些更明显的问题
- 您应该始终在代码中包含
use strict
和use warnings
- 我不认为
@ff=
做了你认为它做的事。我认为你想要@ff=@ARGV
。即使这样,你也觉得你在毫无意义地复制@ARGV
- 有一个数组(
@ff
)和一个标量($ff
)具有相同的名称在某个时候会让人感到困惑。而且,更一般地说,您需要花更多的精力来清楚地命名变量
- 请使用三参数版本的
open()
以及词法文件句柄。您还应始终检查open()
-open my$fh中的返回代码,"上一次,我要求您将来自己修复代码缩进。如果您不想发布可读代码,为什么我们要尽力帮助您?我传递给子程序的参数在此代码中是标量,您不会在任何地方传递任何内容到子程序。请使用strict
和warnings
pragmas关于你在这里发布的代码。@seedof:我看到你已经编辑了你的代码,添加了use strict
和use warnings
。因为你没有做任何其他修复,所以use strict
现在意味着你的代码甚至无法编译。请小心点,你的程序甚至无法编译!
#Report strip
use warnings;
use strict;
##Print the title
Tfms2();
##Print the title
print "\n";
@ff = <@ARGV>;
foreach $ff ( @ff ) {
open (F, $ff);
@fswf = @fschuck = @fsxpos = @fsypos = @fsdev = @csnom = "";
@cswf = @cschuck = @csxpos = @csypos = @csnom = ""; # is there an efficient way?
while (<F>) {
Mfms2();
Mfms3();
}
$r = 1;
while ( $r <= $#fswf ) { # because @fsws is the largest array
Cfms3();
print "\n";
$r++;
}
close (F);
}
##==========================================================================================
##Subs
##==========================================================================================
##FS II
sub Tfms2 {
print "FS_Wafer,FS_ChuckID,FS_pos_X,FS_pos_Y,FS_deviation,CS_Wafer,CS_ChuckID,CS_pos_X,CS_pos_Y,CS_NofWafer_Ident_Spot";
}
sub Mfms2 {
if ( /^F\sM\sSTATISTICS\sII$/ ) {
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$r = 1;
@b = "";
while ( $a !~ /\+\-/ ) {
chomp $a;
@b = split / *\| */, $a;
$fswf[$r] = $b[1];
$fschuck[$r] = $b[2];
$fsxpos[$r] = $b[3];
$fsypos[$r] = $b[4];
$fsdev[$r] = $b[5];
$r++;
$a = (<F>);
@b = "";
}
}
}
##FS III
sub Mfms3 {
if ( /^F\sM\sSTATISTICS\sIII$/ ) {
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$a = (<F>);
$r = 1;
@b = "";
while ( $a !~ /\+\-/ ) {
chomp $a;
@b = split / *\| */, $a;
$cswf[$r] = $b[1];
$cschuck[$r] = $b[2];
$csxpos[$r] = $b[3];
$csypos[$r] = $b[4];
$csnom[$r] = $b[5];
$r++;
$a = (<F>);
@b = "";
}
}
}
sub Cfms3 {
print "$fswf[$r],$fschuck[$r],$fsxpos[$r],$fsypos[$r],$fsdev[$r],";
print "$cswf[$r],$cschuck[$r],$csxpos[$r],$csypos[$r],$csnom[$r],";
}
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
@ARGV or die "Usage: $0 file [file...]\n";
# Define two list of keys for the different types
my %cols = (
II => [qw(wf chuck xpos ypos dev)],
III => [qw(wf chuck xpos ypos nom)],
);
my @data; # To store the parsed data
my $cur_type; # Keep track of the current type of record
while (<>) {
# Look for a record header and determine which type of
# record we're dealing with (I think 'II' or 'III').
if (/^F M STATISTICS (III?)$/) {
$cur_type = $1;
next;
}
# Skip lines that are just headers/footers
next if /\+[-=]/;
# Skip lines that don't include data
next unless /\d/;
chomp;
# Remove the start and end of the line
s/^\|\s+//;
s/\s+\|$//;
# Store the data in a hash with the correct keys
# for this type of record
my %rec;
@rec{@{$cols{$cur_type}}} = split /\s+\|\s+/;
# Store a reference to our hash in @data
push @data, \%rec;
}
# Dump the contents of @data
say Dumper \@data;