小编Ale*_*exx的帖子

如果两个键相同但值不同,谁能解释如何打印所有键?

在这段代码中,两个键“39”同名但值不同,我想打印两个键

use strict; 
use warnings; 

my %studentnames = ( 
14 => Martha, 
27 =>Vivek, 
31 =>Era, 
16 =>Marty, 
25 =>Jason, 
29 =>Socrates, 
19 =>Uri, 
39 =>Nitin , 
39 =>Plato, 
); 

foreach my $name (sort keys %studentnames) 
{ 
    printf "%-8s %s\n", $name, $studentnames{$name};
} 
Run Code Online (Sandbox Code Playgroud)

我收到错误。

Bareword "Martha" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Vivek" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Era" not allowed while "strict subs" in use at …
Run Code Online (Sandbox Code Playgroud)

string perl hash

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

Perl:将十进制更改为十六进制的正则表达式

我有一个文件,其中许多十进制数以类似的形式给出

Hello
Hey
'37888' =>'A'
'37890' =>'B'
'37642' =>'C'

Run Code Online (Sandbox Code Playgroud)

现在我已经试过了,

while (my $line = <$Log1>) {
    $line =~ s/'(\d+)'/(\hex(\d+))/g);    #Here I am getting error
    print $line;
    };
sub hex{
my $num = @_;
my $n= ("0x"."%x\n", $num);
return $n; 
};   
Run Code Online (Sandbox Code Playgroud)

我认为 (\hex(\d+)) 会起作用。有什么建议怎么做吗?

regex string perl function

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

读取固定宽度数据时保留空白列并添加分隔符

我正在解析一个文件。

文件格式是这样的:


Column1  Column2  Column3  Column4  Column5
1        2        3        4        5
6        7                 8        9
10       11       12                14
         15       16       17       18

Run Code Online (Sandbox Code Playgroud)

一些列是空的。所以我正在读取与上述格式相同的两个文件并合并这两个文件并添加“|” 每列之间,所以它应该是这样的:


Column1 | Column2 | Column3 | Column4 | Column5
1       | 2       | 3       | 4       | 5
6       | 7       |         | 8       | 9
10      | 11      | 12      |         | 14
        | 15      | 16      | 17      | 18

Run Code Online (Sandbox Code Playgroud)

但我越来越像这样。列中的空格被删除。


Column1 | Column2 | Column3 | Column4 | Column5
1       | …
Run Code Online (Sandbox Code Playgroud)

perl text-processing

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

如何在 Perl 中解决此警告

我问这个类型的疑问句的前面,但没有提供完整的代码。

我正在阅读下面的文件并检查每列中存在的最大字宽,然后以正确对齐方式将其写入另一个文件。

id0 id1 id2 batch
0   34  56  70
2   3647    58  72  566
4   39  616 75  98  78 78987 9876 7899 776
89  40  62  76
8   42  64  78
34  455 544 565

Run Code Online (Sandbox Code Playgroud)

我的代码:

unlink "temp1.log";
use warnings;
use strict;
use feature 'say';
my $log1_file = "log1.log";
my $temp1 = "temp1.log";
open(IN1, "<$log1_file" ) or die "Could not open file $log1_file: $!";
my @col_lens;

while (my $line = <IN1>) {
    my @fs = split …
Run Code Online (Sandbox Code Playgroud)

perl

2
推荐指数
2
解决办法
109
查看次数

标签 统计

perl ×4

string ×2

function ×1

hash ×1

regex ×1

text-processing ×1