in reply to Re: Search Categories - Most efficient way
in thread Search Categories - Most efficient way

Hello:

I forgot to add that the data of the members are in a DBM file.

ixtangxi|anthony|19|US|male|MA|
lancerlotz|mark|21|US|male|AK|

Heres how my coding looks like:

############## ###retrieve member list opendir(MEMBERS, "$free_path"); @memberz = grep !/\.\.?/, readdir MEMBERS; closedir (MEMBERS); &memhead; ######## @members = sort @memberz; if ($start eq "") { $start = 1; } for $members (@members) { if ($members eq "total.dat") { next; } $c++; # count all items if($c >= $start){ # if $c count is = or greater than where to star +t $d++; # count items that match first test unless($d > $show){ # unless $d count is greater than how many + to show #$action = $INPUT{'action'}; #$membername = $INPUT{'name'}; #$username = $INPUT{'username'}; #$cityx = $INPUT{'city'}; #$genderx = $INPUT{'gender'}; #$countryx = $INPUT{'country'}; #$statex = $INPUT{'state'}; #$agex = $INPUT{'age'}; #$start = $INPUT{'start'}; #$show = $INPUT{'show'}; #coollect info use DB_File; # optional; overrides default tie %cata, "DB_File", "$path/accounts" or die "Can't open FILENAME: $! +\n"; my $acc_data = $cata{$members}; untie(%cata); ($email,$name,$pwd,$size,$creation,$last_log,$size_added,$hf_on, $website_name,$website_des,$numdirs,$www_of,$www_name,$www_bgcolor +, $www_bgimage,$www_text,$www_link,$www_vlink,$onhold,$address,$city +, $state,$zip,$country,$telephone,$gender,$age,$icq,$education, $income,$occupation,$d_o_b,$moderator,$gbook_of,$op_in,$icon_url, $file_types,$ftp_per,$session_str,$num_dirs,$num_files, $custom_1,$custom_2,$custom_3,$custom_4,$custom_5,$custom_6,$custo +m_7, $custom_8,$custom_9,$custom_10,$marital,$signup_ip,$last_ip,$formm +ail_of) = split(/\|/,$acc_data); ($month,$day,$year1,$year2) = split (/\,/,$d_o_b); #end collect info

Can I still use the hash suggestion by dws

Anthony

Replies are listed 'Best First'.
Re: Re: Re: Search Categories - Most efficient way
by jlongino (Parson) on Dec 09, 2001 at 07:14 UTC
    Yes, you can still use the method suggested by dws (or my variant thereof) but you'll have to restructure your user hash like my  %users:
    use strict; my %users = (); my %matches = (); my %search = ( sex => 'F', age => 19, st => 'FL' ); ## load users hash foreach (<DATA>) { chomp; my ($user, $sex, $age, $st) = split /\|/; $users{$user}{sex} = $sex; $users{$user}{age} = $age; $users{$user}{st} = $st; } ## Perform searches foreach my $user (keys %users) { my $match = 1; foreach my $key (keys %search) { $match = 0, last if $users{$user}{$key} ne $search{$key}; } $matches{$user}++ if $match; } ## Print out the matches (if any) foreach (keys %matches) { print "user: '", $_, "' sex: '", $users{$_}{sex}, "' age: '", $users{$_}{age}, "' st: '", $users{$_}{st}, "'\n"; } print "No matches found!\n" if not %matches; __DATA__ JOE|M|18|AL SUE|F|18|MS BOB|M|19|CA EVE|F|20|FL PAM|F|19|FL ABE|M|18|FL NAN|F|19|CA
    Note that this could be very slow if you have a large number of users and/or a large number of criteria to match. Worst case is criteria count * user count iterations. I'm not the greatest at optimizing code, so no doubt there are more efficient ways to do it (TAMEWTDI).

    --Jim