如何使用来自一个perl脚本的文本输出作为另一个perl脚本的输入?我似乎可以将它作为两个单独的脚本运行,但不能作为一个



我有一个脚本,可以重新格式化输入文件并创建输出文件。当我尝试读取脚本第二部分的输出文件时,它不起作用。然而,如果我把脚本分成两部分,它会很好地工作,并为我提供所需的输出。我不是程序员,很惊讶我能走到这一步——我已经花了好几天的时间试图解决这个问题。

我运行它的命令是这样的(顺便说一句,temp.txt只是一个暴力的变通方法,可以去掉最后一个逗号来获得我的最终输出文件-找不到其他解决方案):

c:perlbinperl merge.pl F146.sel temp.txt F146H.txt

输入如下(来自另一个软件包)("F146.sel"):

/ Selected holes from the .Mag_F146_Trimmed.gdb database.
"L12260"
"L12270"
"L12280"
"L12290"

输出如下(文本的mods:删除引号,插入逗号,连接成一行,删除最后一个逗号)"F146H.txt":

L12260,L12270,L12280,L12290 

然后,我想在脚本的下一部分中使用它作为输入,它基本上将这个输出插入到一行代码中,我可以在另一个软件包(我的"merge.gs"文件)中使用。如果我把脚本分成两部分,这就是我得到的输出,但如果我把它作为一部分来做,它只会给我一个空白(见下文)。

CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="L12260,L12270,L12280,L12290"
GX             mergline.gx

下面是我的"merge.pl"。我做错了什么?

(事实上,问题可能是-我没有做错什么,因为这可能是你一段时间以来看到的最迟钝的代码。事实上,我打赌你们中的一些人可以用10-15行代码完成整个操作,而不是我的90行代码。)。提前谢谢。)

# this reformats the SEL file to remove the first line and replace the " with nothing
$file = shift ;
$temp = shift ;
$linesH = shift ;
#open (Profiles, ">.\scripts\P2.gs")||die "couldn't open output .gs file";
open my $in,  '<', $file      or die "Can't read old file: Inappropriate I/O control operation";
open my $out, '>', $temp or die "Can't write new file: Inappropriate I/O control operation"; 
my $firstLine = 1;                                                                               
while( <$in> )                                                                                   
{                                                                                                
if($firstLine)                                                                               
{                                                                                            
$firstLine = 0;                                                                          
}                                                                                            
else{                                                                                        
s/"L/L/g; # replace "L with L                                                                 
s/"/,/g; # replace " with, 
s|s+||; # concatenates it all into one line 
print $out $_;  
}
} 
close $out;

open (part1, "${temp}")||die "Couldn't open selection file";
open (part2, ">${linesH}")||die "Couldn't open selection file";
printitChomp();
sub printitChomp
{
print part2 <<ENDGS;
ENDGS
}
while ($temp = <part1> )
{
print $temp;    
printit();
}
sub printit 
{$string = substr (${temp}, 0,-1);
print part2 <<ENDGS;
$string
ENDGS
}                                                                                      
####Theoretically this creates the merge script from the output 
####file from the previous loop. However it only seems to work 
####if I split this into 2 perl scripts.
open (MergeScript, ">MergeScript.gs")||die "couldn't open output .gs file";
printitMerge();
open (SEL, "${linesH}")||die "Couldn't open selection file";  
sub printitMerge
#open .sel file                                                 
{
print MergeScript <<ENDGS;
ENDGS
}
#iterate over required files
while ( $line = <SEL> ){    
chomp $line;            
print STDOUT $line;     
printitLines();              
}                           
sub printitLines 
{
print MergeScript <<ENDGS;
CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="${line}"
GX             mergline.gx
ENDGS
}

所以我认为您真正缺少的只是close(part2);允许它作为SEL重新打开。。

#!/usr/bin/env perl
use strict;
use warnings;
# this reformats the SEL file to remove the first line and replace the " with nothing
my $file = shift;
my $temp = shift;
my $linesH = shift;
open my $in,  '<', $file      or die "Can't read old file: Inappropriate I/O control operation";
open my $out, '>', $temp or die "Can't write new file: Inappropriate I/O control operation"; 
my $firstLine = 1;                                                                               
while (my $line = <$in>){
print "LINE: $linen";
if ($firstLine){                                                                                            
$firstLine = 0;                                                                          
} else {
$line =~ s/"L/L/g; # replace "L with L
$line =~ s/"/,/g;  # replace " with,
$line =~ s/s+//g; # concatenates it all into one line
print $out $line;  
}
} 
close $out;

open (part1, $temp) || die "Couldn't open selection file";
open (part2, ">", $linesH) || die "Couldn't open selection file";
while (my $temp_line = <part1>){
print "TEMPLINE: $temp_linen";    
my $string = substr($temp_line, 0, -1);
print part2 <<ENDGS;
$string
ENDGS
}
close(part2);
#### this creates the merge script from the output 
#### file from the previous loop.
open (MergeScript, ">MergeScript.gs")||die "couldn't open output .gs file";
open (SEL, $linesH) || die "Couldn't open selection file";  
#iterate over required files
while ( my $sel_line = <SEL> ){
chomp $sel_line;            
print STDOUT $sel_line;     
print MergeScript <<"ENDGS";
CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="$sel_line"
GX             mergline.gx
ENDGS
}

还有一种替代方法…

#!/usr/bin/env perl
use strict;
use warnings;
my $file = shift;
open my $in,  '<', $file or die "Can't read old file: Inappropriate I/O control operation";
my @lines = <$in>;            # read in all the lines
shift @lines;                 # discard the first line
my $line = join(',', @lines); # join the lines with commas
$line =~ s/[rn"]+//g;       # remove the quotes and newlines
# print the line into the mergescript
open (MergeScript, ">MergeScript.gs")||die "couldn't open output .gs file";
print MergeScript <<"ENDGS";
CURRENT        Database,"RAD_F146.gdb"
SETINI         MERGLINE.OUT="DALL"
SETINI         MERGLINE.LINES="$line"
GX             mergline.gx
ENDGS

最新更新