The issue is related to the fact that when I try to print out the content of the hash I got strange behaviour... it displays the datas in the hash plus the references (pointers) to something I consider as already processed...use strict; use warnings; use experimental 'smartmatch'; use Data::Dump 'dump'; my @ipaddresses = (''); my $targetfile="/etc/hosts"; my $ipentry=""; #my @domains = (''); my @domains; my %hashEntries; open(my $descripteur,'<:encoding(UTF-8)', $targetfile) or die "$target +file not found :{"; while(my $ligne = <$descripteur>) # tant que nous ne somme +s pas arrivés à la fin du fichier { chomp $ligne; # + on enlève le caractère '\n' de fin de ligne if($ligne =~ /^([0-9]{1,3}\.){3}[0-9]{1,3}/) { ($ipentry,@domains)=split('\s{1,}|\t',$ligne); + # on "splitte" à un ou plusieurs espaces ou à une + tabulation if(!($ipentry ~~ @ipaddresses)) { print "IP trouvée --> $ipentry \n"; $hashEntries{$ipentry} = [ "@domains" ]; push(@ipaddresses,$ipentry); } else { my @temp=$hashEntries{$ipentry}; print "[Add] Nombre d'éléments: @temp\n"; push(@temp,[@domains]); $hashEntries{$ipentry} = [@temp]; + # contre intuitif... un "add" ou " ++=" ou quelque chose qui pourrait noter qu'on ajoute à ce qui existe +déjà... print "[After] Nombre d'éléments: @temp\n"; } } } print dump(%hashEntries)."\n"; foreach my $key (keys %hashEntries) { my @temp=$hashEntries{$key}; #print "Elements: ".@temp."\t"; print "[After no dump] Test 2 $key ->\t"; foreach my $element (@temp) { if($element ~~ /ARRAY/) { print "Array détecté...\t"; foreach my $autreelement (@$element) { print "[@$autreelement]\n"; } } print "[@$element]\n"; } } close($descripteur);
After no dump Test 2 127.0.0.6 -> Array détecté... www.examendecembre.uaa12.test <-- HORRAY I can do stuff on it
test.com test2.com test3.com <-- HORRAY I can do stuff on it
ARRAY(0x560b271bdff0) ARRAY(0x560b271e18f8) <-- ???????????????????????????
The meaning of this script is to edit the /etc/hosts in order to let, for each IPv4 address, only one line with all domain names related to this IP instead of having a real mess in /etc/hosts.
For example:
127.0.0.3 onehost
...
127.0.0.6 onehost
...
127.0.0.5 oneotherhost
...
127.0.0.6 manyotherhosts host1 host2 host3
Would become...
127.0.0.6 onehost manyotherhosts host1 host2 host3
...once this script was launched.
Questions: how to
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |