Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.37 KB | None | 0 0
  1. Toegepast op staatsstructuur van België:
  2.  
  3. @ARGV = "regios.csv";
  4. while (<>) {
  5.     chomp;
  6.     ( $regio, $ouder, $population, $area ) = split ";";
  7.     $hash{$regio} = { regio      => $regio,
  8.                       ouder      => $hash{$ouder},
  9.                       kinderen   => [],
  10.                       number     => 0,
  11.                       niveau     => 0,
  12.                       population => $population,
  13.                       area       => $area };
  14.     push @{ $hash{$ouder}->{kinderen} }, $hash{$regio};
  15.     $refouder = $hash{$regio}->{ouder};
  16.     $hash{$regio}{niveau}=$refouder->{niveau}+1;
  17.     next unless $population;
  18.  
  19.     while ($refouder) {
  20.         $refouder->{number}     += 1;
  21.         $refouder->{population} += $population;
  22.         $refouder->{area}       += $area;
  23.         $refouder = $refouder->{ouder};
  24.     }
  25. }
  26.  
  27. $refknoop = $hash{Belgie}; # initialisatie voor controle 1
  28. @refqueue = ($refknoop);   # initialisatie voor controle 2
  29. %hash = ();                # hash niet meer nodig !
  30.  
  31. # controle 1: hierarchielijn vanaf Belgie, met telkens kind met grootste population
  32.  
  33. while ($refknoop) {
  34.     print "knoop:      ", $refknoop->{regio}, "\n";
  35.     print "kinderen:   ", join( " ", map { $_->{regio} }
  36.                                      sort { $a->{regio} cmp $b->{regio} }
  37.                                      @{ $refknoop->{kinderen} } ), "\n";
  38.     print "#gemeenten: ", $refknoop->{number},     "\n";
  39.     print "population: ", $refknoop->{population}, "\n";
  40.     print "area:       ", $refknoop->{area},       "\n";
  41.     print "\n";
  42.     ($refknoop) = sort { $b->{population} <=> $a->{population} } @{ $refknoop->{kinderen} };
  43. }
  44.  
  45. # controle 2: volledige hierarchie vanaf Belgie
  46.  
  47. while ($refknoop=shift @refqueue) {
  48.     printf "%-41s %8d %6d\n",(("    "x($refknoop->{niveau}-1)).$refknoop->{regio}),$refknoop->{population},$refknoop->{area};
  49.     unshift @refqueue,sort { $b->{population} <=> $a->{population} } @{ $refknoop->{kinderen} };
  50. }
  51. Here's part of the family tree from the Bible:
  52.  
  53. %father = ( 'Cain'      => 'Adam',
  54.            'Abel'      => 'Adam',
  55.            'Seth'      => 'Adam',
  56.            'Enoch'     => 'Cain',
  57.            'Irad'      => 'Enoch',
  58.            'Mehujael'  => 'Irad',
  59.            'Methusael' => 'Mehujael',
  60.            'Lamech'    => 'Methusael',
  61.            'Jabal'     => 'Lamech',
  62.            'Jubal'     => 'Lamech',
  63.            'Tubalcain' => 'Lamech',
  64.            'Enos'      => 'Seth' );
  65. This lets us, for instance, easily trace a person's lineage:
  66.  
  67. while (<>) {
  68.     chomp;
  69.     do {
  70.         print "$_ ";        # print the current name
  71.         $_ = $father{$_};   # set $_ to $_'s father
  72.     } while defined;        # until we run out of fathers
  73.     print "\n";
  74. }
  75. We can already ask questions like "Who begat Seth?" by checking the %father hash. By inverting this hash, we invert the relationship. This lets us use reeks 2 vraag 35 to answer questions like "Whom did Lamech beget?"
  76.  
  77. while ( ($k,$v) = each %father ) {
  78.     push( @{ $children{$v} }, $k );
  79. }
  80.  
  81. $" = ', ';                  # separate output with commas
  82. while (<>) {
  83.     chomp;
  84.     if ($children{$_}) {
  85.         @children = @{$children{$_}};
  86.     } else {
  87.         @children = "nobody";
  88.     }
  89.     print "$_ begat @children.\n";
  90. }
  91. Hashes can also represent relationships such as the C language #include s. A includes B if A contains #include B. This code builds the hash (it doesn't look for files in /usr/include as it should, but that's a minor change):
  92.  
  93. foreach $file (@ARGV) {
  94.     local *FH;
  95.     unless (open(FH, " < $file")) {
  96.         warn "Couldn't read $file: $!; skipping.\n";
  97.         next;
  98.     }
  99.  
  100.     while (<FH>) {
  101.         next unless /^\s*#\s*include\s*<([^>]+)>/;
  102.         push(@{$includes{$1}}, $file);
  103.     }
  104.     close FH;
  105. }
  106. This shows which files with include statements are not included in other files:
  107.  
  108. @include_free = ( );                 # list of files that don't include others
  109. @uniq{map { @$_ } values %includes} = undef;
  110. foreach $file (sort keys %uniq) {
  111.         push( @include_free , $file ) unless $includes{$file};
  112. }
  113. The values of %includes are anonymous arrays because a single file can (and often does) include more than one other file. We use map to build up a big list of the included files and remove duplicates using a hash.
  114.  
  115. Referenties
  116.  
  117. reeks 2 vraag 21; the more complex data structures in vraag 12 and vraag 13
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement