在 perl 中访问/修改数组

发布于 2025-01-07 23:22:24 字数 1578 浏览 0 评论 0原文

当我们将一个元素推送到数组时,有什么方法可以将其推送到特定的列。

我正在尝试做这样的事情。

push (@array, $val ); .......$val should always go to first column.
push (@array, $val2); .......$val2 should go to second column
push (@array, $val3);........$val3 should go to third 

我尝试给出 \t 但没有得到正确的结果。

elsif ($line =~/RELATION/){
push (@mystuff, "$line" .",");
$line = &getline;
}

我的示例 txt 文件如下所示

SEVERITY Warning
NODE OTHER "awssystem"
APPLICATION "AWS"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
ACK  "<$MSG_NODE>:hello"
TEXT "Test one two three"
AUTOACTION "echo \"It has to ack after AA\" > /tmp/banack" ACTIONNODE IP 0.0.0.0  "<$OPC_MGMTSV>" ANNOTATE ACK
                      OPACTION "echo `hostname`" ANNOTATE
                      TROUBLETICKET
                      HELPTEXT "Hello what is this"

SEVERITY Warning
NODE OTHER "awssystem"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
MSGKEYRELATION ACK  "<$MSG_NODE>:hello"

我的文本文件中有许多类似的条目。我试图仅捕获严重性、应用程序、msggrp 和对象,在上面的输出应用程序中缺少应用程序,因此如果找不到应用程序,我只需要添加一个空白。

我的代码如下所示:

while ($#myarr > 0 )
-------
---
elsif ($line =~/SEVERITY/){
push (@mystuff, "$line" .",");
$line = &getline;
}

我希望我的输出 shld 看起来像这样

SEVERITY Warning APPLICATION "AWS" MSGGRP "OpC"
SEVERITY Warning                   MSGGRP "OpC"

但我的输出看起来像这样

SEVERITY Warning APPLICATION "AWS" MSGGRP "OpC"
SEVERITY Warning MSGGRP "OpC"

Is there any way that when we push an element to an array, can we push it to specific columns.

Im trying to do something like this.

push (@array, $val ); .......$val should always go to first column.
push (@array, $val2); .......$val2 should go to second column
push (@array, $val3);........$val3 should go to third 

I tried giving \t but didnt get right results.

elsif ($line =~/RELATION/){
push (@mystuff, "$line" .",");
$line = &getline;
}

My sample txt file looks like this

SEVERITY Warning
NODE OTHER "awssystem"
APPLICATION "AWS"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
ACK  "<$MSG_NODE>:hello"
TEXT "Test one two three"
AUTOACTION "echo \"It has to ack after AA\" > /tmp/banack" ACTIONNODE IP 0.0.0.0  "<$OPC_MGMTSV>" ANNOTATE ACK
                      OPACTION "echo `hostname`" ANNOTATE
                      TROUBLETICKET
                      HELPTEXT "Hello what is this"

SEVERITY Warning
NODE OTHER "awssystem"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
MSGKEYRELATION ACK  "<$MSG_NODE>:hello"

I have many similar entries like this in my text file. I'm trying to capture only Severity, application, msggrp and object, in the above output application is missing so i just need to put a whitepace if it can't find an application.

My code looks like this:

while ($#myarr > 0 )
-------
---
elsif ($line =~/SEVERITY/){
push (@mystuff, "$line" .",");
$line = &getline;
}

I want my ouput shld look line this

SEVERITY Warning APPLICATION "AWS" MSGGRP "OpC"
SEVERITY Warning                   MSGGRP "OpC"

But my output looks like this

SEVERITY Warning APPLICATION "AWS" MSGGRP "OpC"
SEVERITY Warning MSGGRP "OpC"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2025-01-14 23:22:24

怎么样:

#!/usr/bin/perl
use strict;
use warnings;

my %res;
my @res;
while(<DATA>) {
    chomp;
    if (/(SEVERITY)/) {
        push @res,{%res} if $.>1;
        %res = map{$_ => ''}qw/APPLICATION MSGGRP/;
        $res{$1} = $_;
    } elsif (/(APPLICATION|MSGGRP)/) {
        $res{$1} = $_;
    }
}
push @res,{%res};

foreach my $res(@res) {
format STDOUT = 
@<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<
$res->{SEVERITY}, $res->{APPLICATION}, $res->{MSGGRP}
.
write;
}

__DATA__
SEVERITY Warning
NODE OTHER "awssystem"
APPLICATION "AWS"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
ACK  "<$MSG_NODE>:hello"
TEXT "Test one two three"
AUTOACTION "echo \"It has to ack after AA\" > /tmp/banack" ACTIONNODE IP 0.0.0.0  "<$OPC_MGMTSV>" ANNOTATE ACK
                      OPACTION "echo `hostname`" ANNOTATE
                      TROUBLETICKET
                      HELPTEXT "Hello what is this"

SEVERITY Warning
NODE OTHER "awssystem"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
MSGKEYRELATION ACK  "<$MSG_NODE>:hello"

输出:

SEVERITY Warning      APPLICATION "AWS"     MSGGRP "OpC"
SEVERITY Warning                            MSGGRP "OpC"

How about:

#!/usr/bin/perl
use strict;
use warnings;

my %res;
my @res;
while(<DATA>) {
    chomp;
    if (/(SEVERITY)/) {
        push @res,{%res} if $.>1;
        %res = map{$_ => ''}qw/APPLICATION MSGGRP/;
        $res{$1} = $_;
    } elsif (/(APPLICATION|MSGGRP)/) {
        $res{$1} = $_;
    }
}
push @res,{%res};

foreach my $res(@res) {
format STDOUT = 
@<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<
$res->{SEVERITY}, $res->{APPLICATION}, $res->{MSGGRP}
.
write;
}

__DATA__
SEVERITY Warning
NODE OTHER "awssystem"
APPLICATION "AWS"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
ACK  "<$MSG_NODE>:hello"
TEXT "Test one two three"
AUTOACTION "echo \"It has to ack after AA\" > /tmp/banack" ACTIONNODE IP 0.0.0.0  "<$OPC_MGMTSV>" ANNOTATE ACK
                      OPACTION "echo `hostname`" ANNOTATE
                      TROUBLETICKET
                      HELPTEXT "Hello what is this"

SEVERITY Warning
NODE OTHER "awssystem"
MSGGRP "OpC"
OBJECT "Audit"
MSGKEY "<$MSG_NODE>:hello"
MSGKEYRELATION ACK  "<$MSG_NODE>:hello"

output:

SEVERITY Warning      APPLICATION "AWS"     MSGGRP "OpC"
SEVERITY Warning                            MSGGRP "OpC"
溺渁∝ 2025-01-14 23:22:24

“推送到列”是什么意思?

您可以将值存储在数组中的某个位置,

$array[0] = $val ;  # $val is 1. element
$array[1] = $val2 ; # $val2 is 2. element
$array[2] = $val3 ; # ...

相同。

push @array , $val , $val2 , $val3 ;

这与之前假设 @array 为空

What do you mean "push to column"?

You can store the value at a certain position in an array

$array[0] = $val ;  # $val is 1. element
$array[1] = $val2 ; # $val2 is 2. element
$array[2] = $val3 ; # ...

This is the same as

push @array , $val , $val2 , $val3 ;

assuming the @array was empty before.

甜嗑 2025-01-14 23:22:24

IIUC:

push(@array, $val, $val2, $val3)

IIUC:

push(@array, $val, $val2, $val3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文