1

I am doing the configuration file using Config::Simple

The configuration file i.e. 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 i.e.%Config i am assigning to the array @arrthe output would be

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

@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.

2 Answers 2

6

grep is for filtering, not for changing the data. 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:

use List::MoreUtils qw{ uniq };
@arr = uniq(@arr);
Sign up to request clarification or add additional context in comments.

4 Comments

grep can be used for changing data. @arr = grep s/\..*//, @arr would also work in this case
@mob: Modifying $_ in grep is possible, but not recommended.
Is there any built-in functions u have used in first option ?
@abhishek: The first part is a hash slice asignment, the second one is normal array assignment.
0

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.

So if you use a real INI parser (e.g. Config::INI), your life immediately gets a lot easier.

#!/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. And that's what you are looking for. So simply calling keys on that hash gives you what you want.

Using the right tool often simplifies things.

8 Comments

what about values??what if i want to put it both in one array..so that i can access any values with the help of keys..
As I said, what you get in $cfg is a reference to a two-level hash (not an array, I don't think an array would really be helpful here). The values in the first level hash are hash references representing each block in the config file. So, for example, $cfg->{'Dialer Onboard1'}{pass} would give you "pass1".
Useless use of hash element in void context at test2.pl line 15. i am getting this error...
It's not an error, it's a warning. Obviously (or, at least, I thought it was obvious) you need to do something with the result of that expression - store it in a variable, print it out, something like that.
i am not getting any value...can u show me one example..so that i will understand
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.