in reply to RFC: Text::Grap

If you test a particular array for the existance of many values, it might be faster/easier to understand to use the Schwartzian Transform. For example:

my $matches = 0; foreach my $item (@items) { $matches++ if grep( $item eq $_, @list ); } print "All match!" if $matches == @items;

becomes:

my %st = map { $_ => undef } @list; my $matches = 0; foreach my $item (@items) { $matches++ if exists $st{$item}; } print "All match!" if $matches == @items;

The larger @items gets (relative to @list), the better the latter performs relative to the former. I don't know where the 0-point lies -- that is, I don't know at what point it becomes faster to use the latter form -- so use of Benchmark and Devel::DProf (and friends) would be essential.

It would be interesting (though decidedly niche) to see a module where the %st hash would be automatically maintained every time a particular array was updated. I'd probably use overload to create an interface like:

use List::AutoST; my $list = List::AutoST->new(); # these appear to behave like normal push @list, $some, $values, $here; $list[1] = $value2; # but then... print "Ok!" if $list->has('this value'); # or (also via overload) print "Ok!" if exists $list{'this value'};

I don't know about performance differences for that, but it would make an interesting and eas(y|ier) approach.

<radiant.matrix>
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet