http://qs321.pair.com?node_id=105912


in reply to Re: Adjacency List Processing in XML::Twig
in thread Adjacency List Processing in XML::Twig

Changing the nested grep to a grep map would change the semantics of the code.
grep { grep { $_->text eq $search_text } $_->children } @data;
This nested grep returns a list of elements for which any child contains the search text.
grep { $_->text eq $search_text } map { $_->children } @data;
This grep map a list of individual children that contain the search text.

 

With a search for 'menlo park', the nested grep returns two elements; one has the children '<city>menlo park</city>' and '<city>palo alto</city>', and the other has the children '<city>atherton</city>' and '<city>menlo park</city>'.

The grep map, on the other hand, would just return '<city>menlo park</city>' and '<city>menlo park</city>'.