All entries

Towns and islands

28 Oct 2013
Эта же задача по-русски: здесь.

Jasper has a module named RegionsData, which stores some geographic data.

use strict;
use warnings;

package RegionsData;

our %REGIONS = (
    192 => {
        name => 'Havnor Great Port',
        type => 6,
        parents => [ 0, 225, 3, 10658 ],
        childs => [  ],
    },
    10658 => {
        name => 'Havnor',
        type => 5,
        parents => [ 0, 225, 3 ],
        childs => [ 10668, 192, 10664 ],
    },
    10668 => {
        name => 'South Port',
        type => 6,
        parents => [ 0, 225, 3, 10658 ],
        childs => [  ],
    },
    11450 => {
        name => 'Roke',
        type => 5,
        parents => [ 0, 225, 73 ],
        childs => [  ],
    },
    20538 => {
        name => 'Lorbanery',
        type => 5,
        parents => [ 0, 187, 20525 ],
        childs => [  ],
    },
    # ...
);

1;

He needs to filter the regions according to some rule:

use strict;
use warnings;

use RegionsData;

my @leaf_islands;
for my $id (keys %RegionData::REGIONS){
    my $reg = $RegionData::REGIONS{$id};
    if( $reg->{type} == 5 && @{$reg->{childs}} == 0 ){
        push @leaf_islands, $reg->{name};
    }
}

print join ",", @leaf_islands; 

Alas! There is no warnings during execution, but the result list is empty. Why?

The source code is downloadable: RegionsData.pm, islands.pl.

Hint

Show

The hash %REGIONS definetely contains required records: Lorbanery, Roke.

Hint-2

Show

Well, %RegionsData::REGIONS contains them.

Solution

Show

The program uses the name %RegionData::REGIONS, although it should be %RegionsData::REGIONS. Symbol table RegionData:: (just as any other) is avalable regardless of whether the corresponding package is loaded. And since the program uses the wrong name twice, perl doesn’t produce a warning Name used only once: possible typo.

So, use strict and use warnings are strong, but even with them perl is much more tolerant to ‘unknown’ identifiers than languages with static typing. So keep an eye on your identifiers.