Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl行/分隔符文件格式语法_Perl_Syntax - Fatal编程技术网

Perl行/分隔符文件格式语法

Perl行/分隔符文件格式语法,perl,syntax,Perl,Syntax,我试图弄清楚如何使用用perl编写的代码,但对perl语法不太熟悉。我想知道是否有人能告诉我@metafilecache文件的格式是什么?代码无法读取文件中的samplerate,但我不确定如何将其格式化错误。以下是我认为合适的代码摘录: my $tnet = $ARGV[0]; my $tsta = $ARGV[1]; my $stadir = $ARGV[2]; if ( ! -d "$targetdir" ) { mkdir "$targetdir" || die "Cannot

我试图弄清楚如何使用用perl编写的代码,但对perl语法不太熟悉。我想知道是否有人能告诉我@metafilecache文件的格式是什么?代码无法读取文件中的samplerate,但我不确定如何将其格式化错误。以下是我认为合适的代码摘录:

my $tnet = $ARGV[0];
my $tsta = $ARGV[1];
my $stadir = $ARGV[2]; 

if ( ! -d "$targetdir" ) {
  mkdir "$targetdir" || die "Cannot create $targetdir: $?\n";
}

die "Cannot find PDF bin base dir: $pdfbinbase\n" if ( ! -d "$pdfbinbase" );

my %targetdays = ();
my %targetchan = ();

# Collect target files in the $pdfbinbase dir, limited by $changlob
  foreach my $nsldir (glob("$pdfbinbase/{CHRYS}/$tnet.$tsta.*")) {
   next if ( ! -d "$nsldir" ); # Limit to directories

   # Extract location ID from directory name
   my ($net,$sta,$loc) = $nsldir =~ /\/(\w+)\.(\w+)\.([\w-]+)$/;

   if ( $net ne $tnet ) {
     print "Target network ($tnet) != network ($net)\n";
     next;
   }
   if ( $sta ne $tsta ) {
     print "Target station ($tsta) != station ($sta)\n";
     next;
   }

   foreach my $chandir (glob("$nsldir/$changlob")) {
     next if ( ! -d "$chandir" ); # Limit to directories

   # Extract channel code from directory name
     my ($chan) = $chandir =~ /.*\/([\w\d]+)$/;

     foreach my $yeardir (glob("$chandir/Y*")) {
       next if ( ! -d "$yeardir" ); # Limit to directories

       # Extract year from directory name
       my ($year) = $yeardir =~ /^.*\/Y(\d{4,4})$/;

       foreach my $daybin (glob("$yeardir/D*.bin")) {
         next if ( ! -f "$daybin" ); # Limit to regular files

         my ($day) = $daybin =~ /^.*\/D(\d{3,3})\.bin$/;

         $targetdays{"$loc.$chan.$year.$day"} = $daybin;
         $targetchan{"$loc.$chan"} = 1;
       }
     }
   }
 }
     if ( $verbose > 1 ) {
        print "Target days from PDF bin files:\n";
        my $count = 0;
        foreach my $tday (sort keys %targetdays) {
          print "Target day: $tday => $targetdays{$tday}\n";
          $count++;
        }
      print "Targets: $count\n";
     }

 # Remove targets that have already been calculated by checking
 # results files against targets.
 foreach my $tchan ( keys %targetchan ) {
   my ($loc,$chan) = split (/\./, $tchan);
   # Generate target file name
   my $targetfile = undef;
   if ( $loc ne "--" ) { $targetfile = "$targetdir/$prefix-$loc.$chan"; }
     else { $targetfile = "$targetdir/$prefix-$chan"; }

     print "\nChecking target file for previous results: $targetfile\n"  
     if        ( $verbose );

   next if ( ! -f "$targetfile" );

     # Open result file and remove any targets that are included
     open IN, "$targetfile" || next;
     foreach my $line (<IN>) {
       next if ( $line =~ /^YEAR\.DAY/ );
         my ($year,$day) = $line =~ /^(\d+).(\d+)/;

         # Delete this target
         delete $targetdays{"$loc.$chan.$year.$day"};
       }
   close IN;
  }

  if ( $verbose > 1 ) {
    print "Remaining target days:\n";
    my $count = 0;
    foreach my $tday (sort keys %targetdays) {
      print "Target day: $tday => $targetdays{$tday}\n";
      $count++;
    }
    print "Remaining Targets: $count\n";
  }
 my %targetfiles = ();

 # Calculate and store PDF mode for each target day
 TARGET: foreach my $tday (sort keys %targetdays) {
 my ($loc,$chan,$year,$day) = split (/\./, $tday);

 my %power = ();
 my %count = ();
 my @period = ();

 # Determine sampling rate
 my $samprate = GetSampRate ($tnet,$tsta,$loc,$chan);
 print "Samplerate for $tnet $tsta $loc $chan is: $samprate\n" if (     
 $verbose );
 if ( ! defined $samprate ) {
   if ( ($tsta eq "ECSD") || ($tsta eq "SFJ") || ($tsta eq "CASEE") ||     
     ($tsta eq "JSC") ){
     next;
   }
   else {
     print "Cannot determine sample rate for channel 
     $tnet.$tsta.$loc.$chan\n";
     next;
   }
 }
my$tnet=$ARGV[0];
my$tsta=$ARGV[1];
my$stadir=$ARGV[2];
如果(!-d“$targetdir”){
mkdir“$targetdir”| | die”无法创建$targetdir:$?\n”;
}
如果(!-d“$pdfbinbase”),“无法找到PDF bin基本目录:$pdfbinbase\n”;
我的%targetdays=();
我的%targetchan=();
#在$pdfbinbase目录中收集目标文件,受$changlob限制
foreach my$nsldir(glob($pdfbinbase/{CHRYS}/$tnet.$tsta.*)){
下一个if(!-d“$nsldir”)#限制到目录
#从目录名中提取位置ID
我的($net,$sta,$loc)=$nsldir=~/\/(\w+)\(\w+)([\w-]+)$/;
如果($ne$tnet净额){
打印“目标网络($tnet)!=网络($net)\n”;
下一个
}
如果($sta ne$tsta){
打印“目标站($tsta)!=站($sta)\n”;
下一个
}
每个我的$chandir(glob($nsldir/$changlob))){
下一个if(!-d“$chandir”)#限制到目录
#从目录名中提取通道代码
my($chan)=$chandir=~/.\/([\w\d]+)$/;
foreach my$yeardir(全局($chandir/Y*)){
下一个if(!-d“$yeardir”)#限制到目录
#从目录名中提取年份
我的($year)=$yeardir=~/^.*\/Y(\d{4,4})$/;
每个我的$daybin(glob($yeardir/D*.bin))){
下一个if(!-f“$daybin”)#限制为常规文件
我的($day)=$daybin=~/^.*\/D(\D{3,3})\.bin$/;
$targetdays{“$loc.$chan.$year.$day”}=$daybin;
$targetchan{“$loc.$chan”}=1;
}
}
}
}
如果($verbose>1){
打印“PDF bin文件中的目标天数:\n”;
我的$count=0;
foreach my$tday(排序关键字%targetdays){
打印“目标日期:$tday=>$targetdays{$tday}\n”;
$count++;
}
打印“目标:$count\n”;
}
#删除通过选中已计算的目标
#针对目标的结果文件。
foreach my$tchan(钥匙%targetchan){
我的($loc,$chan)=分割(/\./,$tchan);
#生成目标文件名
my$targetfile=undf;
如果($loc ne”->”{$targetfile=“$targetdir/$prefix-$loc.$chan”;}
else{$targetfile=“$targetdir/$prefix-$chan”;}
打印“\n检查目标文件是否有以前的结果:$targetfile\n”
如果($详细);
下一个if(!-f“$targetfile”);
#打开结果文件并删除包含的所有目标
在“$targetfile”| |下一步中打开;
foreach我的$line(){
下一个if($line=~/^YEAR\.DAY/);
我的($year,$day)=$line=~/^(\d+)(\d+)/;
#删除此目标
删除$targetdays{“$loc.$chan.$year.$day”};
}
接近;
}
如果($verbose>1){
打印“剩余目标天数:\n”;
我的$count=0;
foreach my$tday(排序关键字%targetdays){
打印“目标日期:$tday=>$targetdays{$tday}\n”;
$count++;
}
打印“剩余目标:$count\n”;
}
我的%targetfiles=();
#计算并存储每个目标日的PDF模式
目标:foreach my$tday(排序键%targetdays){
我的($loc,$chan,$year,$day)=分割(/\./,$tday);
我的%功率=();
我的%count=();
我的@period=();
#确定采样率
我的$samprate=GetSampRate($tnet、$tsta、$loc、$chan);
如果(
$verbose);
如果(!定义为$samprate){
如果($tsta eq“ECSD”)| |($tsta eq“SFJ”)| |($tsta eq“CASE”)|
($tsta eq“JSC”)){
下一个
}
否则{
打印“无法确定通道的采样率
$tnet.$tsta.$loc.$chan\n“;
下一个
}
}
这是子例程GetSampRate:

sub GetSampRate {    # GetSampRate (net,sta,loc,chan)

  my $net  = shift;
  my $sta  = shift;
  my $loc  = shift;
  my $chan = shift;

  my $samprate = undef;

  # Generate source name: Net_Sta_Loc_Chan
  my $srcname = "${net}_${sta}_";
  $srcname   .= ($loc eq "--") ? "_" : "${loc}_";
  $srcname   .= "$chan";

  if ( $#metafilecache < 0 ) {

    my $metafile = "$stadir/metadata.txt";

    if ( ! -f "$metafile" ) {
      print "GetSampRate(): Cannot find metadata file: $metafile\n";
      return undef;
    }

    # Open metadata file
    if ( ! (open MF, "<$metafile") ) {
      print "GetSampRate(): Cannot open: $metafile\n";
      return undef;
    }

    # Read all lines in the metafilecache
    @metafilecache = <MF>;

    close MF;
  }

  # Read all lines starting with srcname into @lines
  my @lines = grep { /^$srcname/ } @metafilecache;

  # Find maximum of sample rates for this channel
  foreach my $line ( @lines ) {
    my @fields = split(/\t/, $line);
    my $rate   = $fields[7];

    $samprate = $rate if (!defined $samprate || $rate > $samprate);
  }

  return $samprate;
}
sub-GetSampRate{#GetSampRate(net、sta、loc、chan)
我的$net=shift;
我的$sta=班次;
我的$loc=班次;
我的$chan=班次;
我的$samprate=未定义;
#生成源名称:Net\u Sta\u Loc\u Chan
my$srcname=“${net}{sta}}”;
$srcname.=($loc eq“-”)吗?“${loc}”;
$srcname.=“$chan”;
如果($#metafilecache<0){
my$metafile=“$stadir/metadata.txt”;
if(!-f“$metafile”){
打印“GetSampRate():找不到元数据文件:$metafile\n”;
返回未定义;
}
#打开元数据文件

如果(!(open MF,“显示的代码非常笨拙

  • 就这个范围而言,该文件名为
    $stadir/metadata.txt
    ,我无法帮助
    $stadir
    ,因为它要么是未定义的,要么是在别处设置的全局值——这不是一个好的设计思想

  • 然后,
    @metafilecache=
    将整个文件加载到数组
    @metafilecache
    ,在每个元素的末尾留下一个尾随的换行符

  • 然后,
    my@lines=grep{/^$srcname/}@metafilecache
    将以
    $srcname
    中的字符串开头的所有行复制到
    @lines
    中。这是另一个不应使用的全局变量

  • 以下
    for
    循环将tab(
    “\t”
    “\x09”
    )分隔符上的行拆分,并将
    $rate
    设置为第八个值(
    $fields[7]
    )。
    $samprate
    的最新值大于当前存储的最大值时,将在每次迭代中更新


我希望这对我们有所帮助

您向我们展示了一个应该用4个值调用的子例程(
GetSampRate(net,sta,loc,chan)
)。它是否正常工作将取决于脚本中设置传递给它的变量的部分。查找调用
GetSampRate
的行,并查看在何处定义传递的内容。根据您显示的内容,我们无法提供帮助