在 perl 中访问/修改数组
当我们将一个元素推送到数组时,有什么方法可以将其推送到特定的列。
我正在尝试做这样的事情。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
输出:
How about:
output:
“推送到列”是什么意思?
您可以将值存储在数组中的某个位置,
相同。
这与之前假设
@array
为空What do you mean "push to column"?
You can store the value at a certain position in an array
This is the same as
assuming the
@array
was empty before.IIUC:
push(@array, $val, $val2, $val3)
IIUC:
push(@array, $val, $val2, $val3)