Hash::Search - Search and return hash keys using regular expressions
use Hash::Search; my $hs = new Hash::Search; my %hashlist = ( "one" => "orange", "two" => "banana", "three" => "apple", "four" => "pear", "five" => "pineapple" ); $hs->hash_search("e\$", %hashlist); my %hashresult = $hs->hash_search_resultdata; my $hashresult_count = $hs->hash_search_resultcount; print $hashresult_count . " result(s) found: "; foreach my $hash_key (keys %hashresult){ print $hash_key . " "; }
This module allows a search to be made on a hash based on a regular expressions pattern based on the name of the key and returns the results as a seperate hash. It also keeps a count of how many matches have been made.
Before Hash::Search can be used, an instance of Hash::Search must be created.
use Hash::Search; $hs = new Hash::Search;
$hs->hash_search("p\$", %hash);
The hash_search subroutine requires two parameters, one is a regular expression used for searching the names of keys and another for the hash itself.
If a blank expression has been specified or an empty hash is given then a warning (carp) message is written and the subroutine promptly returns.
hash_search can also be used in a if statement so that it can process information depending if hash_search finds matches or not.
%results = $hs->hash_search_resultdata;
The value returned from hash_search_resultdata will be a hash. If no matches have been found then it will return an empty hash.
$searchresults = $hs->hash_search_resultcount; print "Matches found: " . $searchresults; print "Number of results returned: " . $hs->hash_search_resultcount;
The value returned from hash_search_resultcount will be a scalar value.
use CGI qw(:standard); use Hash::Search; my $q = new CGI; my $hs = new Hash::Search; my %formdata = $q->Vars; $hs->hash_search("^prefix_", %formdata); my %resultdata = $hs->hash_search_resultdata;
use CGI::Lite; use Hash::Search; my $cgi = new CGI::Lite; my $hs = new Hash::Search; my %formdata = $cgi->parse_form_data; $hs->hash_search("^prefix_", %formdata); my %resultdata = $hs->hash_search_resultdata;
If an invalid regular expression is given to hash_search then the script will not run and will give the line number at the point where the regular expression is used in this module as there doesn't appear to be a relatively straight forward way of checking if the regular expression itself is valid or not prior to it being used.
This is currently a perl module that is at the alpha development stage and as such things in this module can change which may break your scripts in the future.
Carp, CGI and CGI::Lite
Steve Brokenshire, <sbrokenshire@xestia.co.uk>
Copyright (C) 2009 by Steve Brokenshire
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Hash::Search, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Hash::Search
CPAN shell
perl -MCPAN -e shell install Hash::Search
For more information on module installation, please visit the detailed CPAN module installation guide.