小编Rob*_*arl的帖子

如何使用Perl整理和汇总文件中的记录?

我有一个以下格式的文本文件:

211B1 CUSTOMER|UPDATE|  
211B2 CUSTOMER|UPDATE|  
211B3 CUSTOMER|UPDATE|  
211B4 CUSTOMER|UPDATE|  
211B5 CUSTOMER|UPDATE|  
567FR CUSTOMER|DELETE|  
647GI CUSTOMER|DELETE|  
Run Code Online (Sandbox Code Playgroud)

我想要一个处理文本文件的脚本并报告以下内容:

  • 为客户找到的CUSTOMER栏目的"更新":211B1,211B2,211B3,211B4,211B5
  • 找到Acct's的CUSTOMER列的"DELETE":5675FR,6470GI

我可以编写简单的解决方案,但这对我来说似乎有点复杂,并希望得到帮助或指导.

unix perl awk sed

1
推荐指数
1
解决办法
412
查看次数

在循环中命名Arraylists - Java

我需要在while循环中创建一个Arraylist,其名称也基于循环中的变量.这就是我所拥有的:

while(myScanner.hasNextInt()){    

    int truster = myScanner.nextInt();
    int trustee = myScanner.nextInt();
    int i = 1;
    String j = Integer.toString(i);
    String listname = truster + j;

    if(listname.isEmpty()) {
        ArrayList listname = new ArrayList();
    } else {}
    listname.add(truster);

    i++;
}
Run Code Online (Sandbox Code Playgroud)

变量truster在扫描时会出现多次,因此if语句试图检查arraylist是否已经存在.不过,我想我可能已经做到了这一点.

谢谢你的帮助!

java loops arraylist

1
推荐指数
1
解决办法
1337
查看次数

如何在Perl系统调用中传递参数?

我通过使用system命令在Perl程序中传递变量来调用Perl中的R函数.

  #!/usr/bin/perl
  $file1= "Test1.txt"
  $file2= "Test2.txt"
  $val="Rscript Test.R ".$file1." ".$file2;
  print($val,"\n");
  system('Rscript Test.R', $file1, $file2);
Run Code Online (Sandbox Code Playgroud)

但它不会调用R脚本并传递file1和file2值.我怎样才能解决这个问题?

perl r system-calls

1
推荐指数
1
解决办法
1985
查看次数

Perl中的波浪号(〜)目录

当我创建并检查带有波形符号的目录是否存在时,我在Perl脚本中发现了一个轻微的不当行为,如果我使用完整/home/user路径则不会发生这种行为.当我第一次运行此脚本时,它会创建新目录.当我第二次运行它时,它无法识别目录的存在,并尝试第二次创建它:

#!/usr/bin/perl
use strict;

my $outdir = '~/test';
my $cmd = "mkdir $outdir";
unless (-d $outdir) {
  0 == system($cmd) or die "Error creating outdir $outdir\n $?";
}

1;


[~] $ rm test/ -rf
[~] $ perl dir.pl
[~] $ perl dir.pl
mkdir: cannot create directory `/home/avilella/test': File exists
Error creating outdir ~/test
 256 at dir.pl line 7.
Run Code Online (Sandbox Code Playgroud)

如何可靠地处理~在Perl 中使用波形符号的目录?

directory perl

1
推荐指数
3
解决办法
1556
查看次数

在 cygwin 上安装 Perl 模块(make 失败)

我需要Digest::SHA1一个我想在 cygwin 上运行的 Perl 脚本。但是,我必须使用 CPAN 对其进行配置。一切似乎都很好,直到我跑了cpan[1]> install Digest::SHA1。事情继续,然后我得到以下错误:

/bin/sh: gcc-4: command not found
Makefile:327: 目标‘SHA1.o’的配方失败
make: * [SHA1.o] Error 127
GAAS/Digest-SHA1-2.13.tar.gz
/usr/bin /make -- NOT OK
'YAML' 未安装,不会存储持久状态
运行 make test
不能在没有成功的情况下进行测试make 运行 make install
Make 返回了错误状态,安装似乎不可能
在此命令期间失败:
GAAS/Digest-SHA1- 2.13.tar.gz : 不

看来我必须安装gcc4。make 错误不是因为它的路径,因为它设置正确(o conf make在 CPAN 返回/usr/bin/make并且我已经make安装了二进制文件。)。不幸的是,cygwin 不提供 gcc4 包(它被标记为过时的包)。有什么解决方法可以解决这个问题吗?或者有没有一种方法可以在cygwin下编译gcc4,因为简单地gcc-4更改gccMakefile不起作用。

perl cygwin module makefile

1
推荐指数
1
解决办法
5386
查看次数

使用用户输入名称重命名文件

我想将我的临时文件"file3.c"重命名为用户输入文件名.从File :: copy使用重命名或移动命令不会重命名它.

use strict;
use warnings;
use File::Copy;

#input header file
print "Input file1:\n";
$input = <>;
open(FILE1, $input) || die "couldn't open the file!";

open(FILE3, '>>file3.c') || die "couldn't open the file!";
...
#some work on file3.c
...

close(FILE1); 
close(FILE3);

#renaming prepended temporary file name to original file name
rename("file3.c", "$input");
Run Code Online (Sandbox Code Playgroud)

OUTPUT没有重命名

我该如何重命名?

perl perl-module

1
推荐指数
1
解决办法
128
查看次数

Perl command for same behaviour as readlink?

What is the equivalent Perl command to the GNU coreutils command readlink -f?

If any component of the file name except the last one is missing or unavailable, readlink produces no output and exits with a nonzero exit code. A trailing slash is ignored.

perl gnu-coreutils

1
推荐指数
1
解决办法
339
查看次数

将utf7字符串转换为iso8859-1

如何将utf7字符串转换为iso-8859-1格式?我尝试了以下但它打印错误的结果:

use Encode qw(encode decode);
$data ='t+AOQ-m+AOQ- on mit+AOQ- on';
$data = encode("iso-8859-1", decode("utf7", $data));
print $data; #result Tämä on mitä on
Run Code Online (Sandbox Code Playgroud)

这打印t?m? on mit? on,但它应该打印Tämä on mitä on

perl

1
推荐指数
1
解决办法
157
查看次数

wordlist.pl第11行的perl语法错误,靠近")$ fh"

my $fn= "words.txt";
open ($fn), $file;
if (! -e "$fh") $fh="STDIN";
while (<$fn>){;
    my $total_words = @words; #All word count
    my %count;
    $count{$_}++ for @words; # Here are the counts
    my $uniq_words  = scalar keys %count; # Number of uniq words
}
# Print sorted by frequency

print "$_\t$count{$_}" for (sort { $count{$b} <=> $count{$a} } keys %count);

close FILE;
exit 0
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

Scalar found where operator expected at wordlist.pl line 8, near ") $fh"
        (Missing operator before $fh?)
syntax …
Run Code Online (Sandbox Code Playgroud)

syntax perl scalar operator-keyword

1
推荐指数
1
解决办法
320
查看次数

通过在Perl中传递参数来通过eval调用另一个函数

我编写了以下代码,用于将参数传递给sample.pl中的eval函数,并在另一个Perl文件sample1.pl中调用该函数.

sample1.pl:

use strict;
use warnings;
require 'sample.pl';
use subs "hello";
my $main2 = hello();
sub hello
 {
   print "Hello World!\n";
    our $a=10;
    our $b=20;
    my $str="sample.pl";
    my $xc=eval "sub{$str($a,$b)}";
 }
Run Code Online (Sandbox Code Playgroud)

Sample.pl

use strict;
use warnings;
our $a;
our $b;
use subs "hello_world";
my $sdf=hello_world();
sub hello_world($a,$b)
 { 
    print "Hello World!\n";
    our $c=$a+$b;   
    print "This is the called function from sample !\n";
    print "C: " .$c;

 } 1;
Run Code Online (Sandbox Code Playgroud)

我得到输出为:

Illegal character in prototype for main::hello_world : $a,$b at D:/workspace/SamplePerl_project/sample.pl line …
Run Code Online (Sandbox Code Playgroud)

perl dynamic

1
推荐指数
1
解决办法
1720
查看次数