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);
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...

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

  1. Detect the element is an ARRAY... in perl
  2. Acknowledge the number of elements in each hash
  3. Avoid having the two ARRAY references within a hash element


In reply to [Perl 5.26][Linux LEAP] Array/List/Hash misunderstanding by soundlord

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.