简体   繁体   English

如何从Perl中的数组中删除元素?

[英]How do I remove elements from an array in Perl?

I am doing the configuration file using Config::Simple 我正在使用Config::Simple做配置文件

The configuration file ie new.conf 配置文件,即new.conf

[Dialer External]
pass=pass2
user=user2

[Dialer Onboard]
pass=pass1
user=user1

[Dialer Onboard1]
pass=pass1
user=user1

[Dialer Onboard2]
pass=pass1
user=user1

I am reading the configuration file and here is my code 我正在读取配置文件,这是我的代码

#!/usr/bin/perl
use Config::Simple;
use Data::Dumper;

$cfg = new Config::Simple(syntax => 'ini');
$cfg->read('new.conf');
$cfg = Config::Simple->import_from('new.conf', \%Config);
my @arr = ( keys %Config );
print "@arr";

The hash reference ie %Config i am assigning to the array @arr the output would be 哈希引用,即%Config我正在分配给数组@arr ,输出将是

Dialer Onboard1.pass 
Dialer Onboard.pass 
Dialer Onboard.user 
Dialer Onboard2.pass 
Dialer Onboard2.user 
Dialer External.user 
Dialer External.pass 
Dialer Onboard1.user

Till here is correct. 直到这里是正确的。 Now i want to remove some element and assign it to new array that should be something like this 现在我想删除一些元素并将其分配给应该像这样的新数组

Dialer Onboard1
Dialer Onboard 
Dialer Onboard 
Dialer Onboard2 
Dialer Onboard2 
Dialer External 
Dialer External 
Dialer Onboard1

so that after the dot ( . ) i don't want any data. 这样在点( . )之后,我不需要任何数据。 For this i am trying to use the grep function.Here is my code for that 为此,我试图使用grep函数。这是我的代码

@arr = grep { !/./ } @arr;
my @result;
for (@arr) {
    if (/./) {
        push @result, $_;
    } 
}

But this is not working for me or it may be wrong approach. 但这对我不起作用,否则可能是错误的方法。 I am not able to identify where i am going wrong. 我无法确定我要去哪里。 And finally i want to remove the duplicate keys from that and that gives me output something like this. 最后,我想从中删除重复的键,这给了我类似的输出。

Dialer External 
Dialer Onboard 
Dialer Onboard1 
Dialer Onboard2

Please somebody help me and suggest me how to achieve this.Thanks in advance. 请有人帮助我并建议我如何实现这一目标。

grep is for filtering, not for changing the data. grep用于过滤,而不用于更改数据。 To remove everything after a dot, use substitution: 要删除点后的所有内容,请使用替换:

s/\..*// for @arr;

If you want unique elements, use a hash: 如果需要唯一元素,请使用哈希:

my %uniq;
@uniq{@arr} = ();
@arr = keys %uniq;

Or, use uniq from List::MoreUtils : 或者,使用List :: MoreUtils中的 uniq

use List::MoreUtils qw{ uniq };
@arr = uniq(@arr);

You have a fix that gives you what you want. 您有一个修复程序可以满足您的需求。 But I'm slightly surprised that no-one noticed what you actually wanted to do. 但是,没有人注意到您实际上想要做什么,我对此感到有些惊讶。

The reason why you are getting more complicated keys (and more keys) than you wanted is because you are using a generalised config parser to read a config file in a format that the parser isn't really suited for. 之所以会得到比您想要的更复杂的键(和更多的键),是因为您使用的是通用的配置解析器,以一种解析器不真正适合的格式读取配置文件。 Config::Simple assumes a pretty flat key/value structure, whereas the INI-style config that you actually want is more naturally represented as a two-level hash. Config :: Simple假定键/值结构相当平坦,而您实际上想要的INI样式的配置更自然地表示为两级哈希。

So if you use a real INI parser (eg Config::INI ), your life immediately gets a lot easier. 因此,如果您使用真正的INI解析器(例如Config :: INI ),您的生活将立即变得轻松很多。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Config::INI::Reader;

my $cfg = Config::INI::Reader->read_file('new.conf');
my @arr = ( keys %$cfg );

say for @arr;

Config::INI gives you a two-level hash where the top-level keys are the names of the blocks. Config :: INI为您提供两级哈希,其中顶级键是块的名称。 And that's what you are looking for. 这就是您想要的。 So simply calling keys on that hash gives you what you want. 因此,只需在该哈希上调用keys即可满足您的需求。

Using the right tool often simplifies things. 使用正确的工具通常可以简化事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM