当前位置: 动力学知识库 > 问答 > 编程问答 >

concatenation - Perl: concatenate variable inside variable?

问题描述:

Inside variable I would like to concatenate other variable:

my %hash = (

DNS_AD_toto => ['10.0.0.1'],

DNS_OP_toto => ['192.168.0.2', '10.0.0.2'],

DNS_ZA_toto => ['192.168.0.3'],

);

foreach my $abc ("AD", "OP"){

print $DNS_${abc}_toto[0];

}

But print $DNS_$a_toto[0] doesn't work because $DNS_ doesn't exist.

So, how to concatenate two variables in only one line?

网友答案:

I can't answer your question, because it's not clear what your question is. I'll point out though - there are several problems with your code which means it probably isn't going to work - fix these first:

  • Turn on use strict; and use warnings;. Fix any errors generated - these should be the first two lines of everything you write.
  • my @DNS_AD_toto = {'10.0.0.1', '10.0.0.2'}; probably isn't doing what you think it's doing.

{} denote anonymous hashes. So what you're creating here is an anonymous hash, and then inserting it into an array. Your array is one element long, and contains a key-value pair. That's almost certainly not what you wanted.

e.g.:

$VAR1 = [
          {
            '10.0.0.1' => '10.0.0.2'
          }
        ];
  • As mentioned in the comments - don't use $a - it's a reserved variable for sort. In fact - don't use any single letter variable names. There's rarely a good reason for it.

  • You're talking about symbolic references - those are nasty. Don't. Use a hash instead. See here for more detail: http://perl.plover.com/varvarname.html

What you probably want is:

my %DNS_toto = (
   'AD' => [ '10.0.0.1', '10.0.0.2' ],
   'OP' => [ '192.168.0.1', '192.168.0.2' ],
);
print Dumper \%DNS_toto;

Which gives:

$VAR1 = {
          'AD' => [
                    '10.0.0.1',
                    '10.0.0.2'
                  ],
          'OP' => [
                    '192.168.0.1',
                    '192.168.0.2'
                  ]
        };

(But I'm still not quite clear what your desired end result it - update your question and I'll update this answer).

Edit: Following question update:

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

my %hash = (
    DNS_AD_toto => ['10.0.0.1'],
    DNS_OP_toto => [ '192.168.0.2', '10.0.0.2' ],
    DNS_ZA_toto => ['192.168.0.3'],
);

foreach my $abc ( "AD", "OP" ) {
    print $hash{ "DNS_" . $abc . "_toto" }[0], "\n";
}

Alternatively:

foreach my $key ( "DNS_AD_toto", "DNS_OP_toto" ) {
    print $hash{$key},"\n";
}

Or:

foreach my $abc ( grep {m/(AD|OP)/} keys %hash ) {
    print $hash{$abc}[0], "\n";
}
网友答案:

Don't try to generate code.

my %DNS_toto = (
    AD => [ '10.0.0.1', '10.0.0.2' ],
    OP => [ '192.168.0.1', '192.168.0.2' ],
);

for my $foo (qw( AD OP )) {
   if ($DNS_toto{$foo}[0] eq '192.168.0.2') {
      ...
   }

   ...
}
网友答案:

Use a hash when you want to manipulate variable names,

use strict;
use warnings;

my %hash = (
  DNS_AD_toto => ['10.0.0.1', '10.0.0.2'],
  DNS_OP_toto => ['192.168.0.1', '192.168.0.2'],
);

foreach my $s ("AD", "OP") {
  my $key = "DNS_${s}_toto";

  if( $hash{$key}[0] eq "192.168.0.2") {
    print 'It is AD'
  } 
  if( $hash{$key}[0] eq "10.0.0.2") {
    print 'It is OP'
  }

} 
分享给朋友:
您可能感兴趣的文章:
随机阅读: