On 2/17/2016 03:15, Vincent Lequertier wrote:


I'd get rid of the '$'s in front of '$group1' etc to avoid the '$ip =~ 
s/^\$//;' below.

How can I have the following output?

<table1>,"10.100.29.0/24"
<table2>,10.100.27.52
<table2>,10.100.27.53
<table2>,10.100.27.54
<table3>,10.100.27.55
<table3>,10.100.27.56
<table3>,10.100.27.57

My version:

my %vars = (
  'group1' => '10.100.27.52',
  'group2' => '10.100.27.53',
  'group3' => '10.100.27.54',
  'group4' => '10.100.27.55',
  'group5' => '10.100.27.56',
  'group6' => '10.100.27.57',
);

my @tables = (
  {
    'ips' => [
      '"10.100.29.0/24"',
    ],
    'tablename' => '<table1>',
  },
  {
    'ips' => [
      '$group1',
      '$group2',
      '$group3',
    ],
    'tablename' => '<table2>',
  },
  {
    'ips' => [
      '$group4',
      '$group5',
      '$group6',
    ],
    'tablename' => '<table3>',
  }
);
# print (Data::Dumper->Dump([\%vars, \@tables], [qw(%vars @tables)]));

# loop over elements of @table
for (my $ii = 0; $ii < @tables; ++$ii) {
        # do for each ip in element
        foreach my $ip (@{$tables[$ii]{'ips'}}) {       # critical code here
                if ($ip =~ /^\$group/i) { $ip =~ s/^\$//; $ip = $vars{$ip}; }
                printf "%s,%s\n", $tables[$ii]{'tablename'}, $ip;
        }
}

__END__

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to