Le 18/02/2016 08:24, $Bill a écrit :
> 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__
> 

Your code won't work on my real world environment because $ip not allays
matchs '$groups'. It could be any word btw. So I ended up with this :

print($vars{$table->{ips}[$_]}
   ? $vars{$table->{ips}[$_]}
   : $table->{ips}[0]);


Thank you for your feedbacks!

-- 
Vincent Lequertier
skysymbol.github.io

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