use XML::Twig; my $t = XML::Twig->new(PrettyPrint => 'record'); $t->parsefile('adj.xml'); my $root = $t->root; my @pair = $root->children; my $city = 'menlo park'; sub candidate_generator { my ($search_text, @data) = @_; grep { grep { $_->text eq $search_text } $_->children } @data; } my @adj = candidate_generator($city,@pair); my @next_city = map { $_->first_child->text ne $city ? $_->first_child->text : $_->last_child->text } @adj; print "cities next to $city == @next_city"; # -------- no changes up to here. Here is the new stuff: # now we use a slightly different map and grep my @next_to_next = grep { $_->first_child->text ne $city and $_->last_child->text ne $city } map { candidate_generator($_, @pair) } @next_city; map { $_->print } @next_to_next; #### my @next_to_next = grep { $_->first_child->text ne $city and $_->last_child->text ne $city } map { candidate_generator($_, @pair) } @next_city; #### $_->delete for (@adj); my @next_to_next = map { candidate_generator($_, @pair) } @next_city; map { $_->print } @next_to_next; #### if ($file !~ /p(\d{6,7})_(\w+)_(\w+).zip/) { then die "$file violates the companies syntax for patch filenames"; } #### System::Logic::valid_patchfile(File => $file, DieOnErr => 1);