将新变量转换为预先存在的变量perl(getting new variables into a pre

编程入门 行业动态 更新时间:2024-10-23 11:26:35
将新变量转换为预先存在的变量perl(getting new variables into a pre-existing variable perl)

我有一个脚本打印出一串管道分隔的值。 我想要做的是,如果$ f3字段等于某些东西,就像字母C我希望它打印出xout。 但是如果$ f3没有填充任何值,我希望N和G分别打印在$ f5和F7文件中。

#!/usr/bin/perl use strict; use warnings; my ( $system, $f2, $f3, $f4, $f5, $f6, $f7 ) = ""; #$f3="C"; my $xout = "$system|$f2|$f3|$f4|$f5|$f6|$f7|\n"; if ( defined $f3 && $f3 ne '' ) { print $xout; print "\$f3 is defined \n"; } else { my $f5 = "N"; my $f7 = "G"; print $xout; print "the 7th and 8th blocks should have values \n"; }

这是输出

Use of uninitialized value $f2 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f3 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f4 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f5 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f6 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f7 in concatenation (.) or string at ./test_worm_output line 6. ||||||| the 7th and 8th blocks should have values

如果f被取消注释,我得到:

(lots of uninitialized values lines) ||C||||| $f3 is defined

我想要的是如果f没有定义,如果没有价值我需要它打印出来

||||N||G|

最终行看起来像这样(其他字段将有值)但如果填充第三个值,我不能有N或G,如果$ f3为空,我需要N和G.

host1||C||||| host2||C||||| host3||||N||G| host4||C||||| host5||||N||G|

谢谢

I have a script that prints out a string of values which are pipe delimited. waht i want to do is is if $f3 field equals something, like the letter C I want it to print out the xout. However if the $f3 is not populated with any value, I want N and G to be printed out in the $f5 and F7 fileds respectively.

#!/usr/bin/perl use strict; use warnings; my ( $system, $f2, $f3, $f4, $f5, $f6, $f7 ) = ""; #$f3="C"; my $xout = "$system|$f2|$f3|$f4|$f5|$f6|$f7|\n"; if ( defined $f3 && $f3 ne '' ) { print $xout; print "\$f3 is defined \n"; } else { my $f5 = "N"; my $f7 = "G"; print $xout; print "the 7th and 8th blocks should have values \n"; }

This is the output

Use of uninitialized value $f2 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f3 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f4 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f5 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f6 in concatenation (.) or string at ./test_worm_output line 6. Use of uninitialized value $f7 in concatenation (.) or string at ./test_worm_output line 6. ||||||| the 7th and 8th blocks should have values

If the f is uncommented I get :

(lots of uninitialized values lines) ||C||||| $f3 is defined

what i want is if the f not defined, if is has no value I need it to print out

||||N||G|

ultimately the lines will look like this (the other fields will have values ) but if that third values is populated, I can't have the N or G and if $f3 is blank I need the N and G.

host1||C||||| host2||C||||| host3||||N||G| host4||C||||| host5||||N||G|

thank you

最满意答案

在线

my ($system ,$f2,$f3,$f4,$f5,$f6,$f7) = "" ;

你只是初始化列表中的第一个变量$system 。 要初始化列表中的所有变量,您需要在RHS上使用相同数量的值:

my ($system, $f2, $f3, $f4, $f5, $f6, $f7) = ("", "", "", "", "", "", "");

要么

my ($system, $f2, $f3, $f4, $f5, $f6, $f7) = ("") x 7;

但是,每当你发现自己创建编号变量(例如f1 , f2 , f3 )时,你应该考虑“数组”:

my @fields = ("") x 7; if ($fields[2] eq "") { @fields[4, 6] = ("N", "G"); } print join("|", @fields), "\n";

输出:

||||N||G

(当然,这段代码是没有意义的,因为我们明确地将$fields[2]设置为空字符串,然后检查它是否等于......空字符串。我假设你的实际代码更复杂。)

在您的情况下,看起来第一个字段与其他字段不同,因此将数据存储在数组的散列中会更有意义(假设主机名是唯一的):

use strict; use warnings; # Populate the hash my %data; foreach my $host_num (1..5) { my @fields = ("") x 6; $fields[1] = "C" if $host_num == 1 or $host_num == 2 or $host_num == 4; my $host_name = "host" . $host_num; $data{$host_name} = [ @fields ]; } # Print the contents foreach my $host (sort keys %data) { if ($data{$host}[1] eq "") { @{ $data{$host} }[3, 5] = ("N", "G"); } print join("|", $host, @{ $data{$host} }), "\n"; }

输出:

host1||C||||
host2||C||||
host3||||N||G
host4||C||||
host5||||N||G

In the line

my ($system ,$f2,$f3,$f4,$f5,$f6,$f7) = "" ;

you're only initializing the first variable in the list, $system. To initialize all of the variables in the list, you need an equal number of values on the RHS:

my ($system, $f2, $f3, $f4, $f5, $f6, $f7) = ("", "", "", "", "", "", "");

or

my ($system, $f2, $f3, $f4, $f5, $f6, $f7) = ("") x 7;

However, any time you find yourself creating numbered variables (e.g. f1, f2, f3) you should think "array" instead:

my @fields = ("") x 7; if ($fields[2] eq "") { @fields[4, 6] = ("N", "G"); } print join("|", @fields), "\n";

Output:

||||N||G

(Of course, this code is rather pointless since we explicitly set $fields[2] to the empty string and then check if it's equal to...the empty string. I assume your actual code is more complex.)

In your case, it looks like the first field is distinct from the rest, so it would make more sense to store your data in a hash of arrays (assuming the hostnames are unique):

use strict; use warnings; # Populate the hash my %data; foreach my $host_num (1..5) { my @fields = ("") x 6; $fields[1] = "C" if $host_num == 1 or $host_num == 2 or $host_num == 4; my $host_name = "host" . $host_num; $data{$host_name} = [ @fields ]; } # Print the contents foreach my $host (sort keys %data) { if ($data{$host}[1] eq "") { @{ $data{$host} }[3, 5] = ("N", "G"); } print join("|", $host, @{ $data{$host} }), "\n"; }

Output:

host1||C||||
host2||C||||
host3||||N||G
host4||C||||
host5||||N||G

                    
                     
          

更多推荐

本文发布于:2023-07-30 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340338.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   转换为   perl   variables   pre

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!