
KevinR
Veteran

Mar 3, 2005, 7:37 AM
Post #16 of 18
(6531 views)
|
Re: [viclaster] Need help from you PERL masters
[In reply to]
|
Can't Post
|
|
OK, I had a few minutes last night to go over the script. For me it makes things easier to see actual data, I'm not as good as others at working with theoretical data. Although I must admit I am not sure how practical your script is or what the real purpose is. The random generation of 10,000 account numbers could potentially include many duplicates, it depends on how many account numbers you have in the accounts.txt file. If you are looking for the frequency of matches the code would have tobe changed, if you are just looking for a single match then this will work OK I think. I changed the subroutine searchAccounts method after I realized how inefficient the method was I had posted previously. Anyway, see how this flies:
#!perl use strict; use warnings; # This is supposed to pull the array out of the sub routine below at the load data comments my @accounts = &loadAccounts(); # Generate a random list of accounts to search for # This is supposed to generate 10000 different account numbers my @search_accounts = &randomAccounts(); # Search for accounts my @foundIndeces = &searchAccounts( \@accounts, \@search_accounts ); my $num = int @foundIndeces; # Display elapsed time $^T stores the time the progam started print 'Time to find accounts: ', time - $^T, " seconds\n"; print "Total number found: $num\n"; print "Account Numbers Found:\n"; # print all found accounts print "$_\n" for (sort @foundIndeces); ##################################################################### # searchAccounts: searches for account numbers in an array # Parameters: accounts (array of account numbers) # search_array (of account numbers to look for accounts) # Returns: array of indeces into accounts of found account numbers ##################################################################### #I used a hash (%search_accounts) here now to find matches sub searchAccounts { my ($accounts,$search_accounts) = @_; my %search_accounts = (); my %found_accounts = (); $search_accounts{$_}=1 for (@{$search_accounts});#build the lookup hash foreach (@{$accounts}) { $found_accounts{$_}=1 if ( exists $search_accounts{$_}); } return (keys %found_accounts); #we only need the keys } ##################################################################### # loadData: # # ##################################################################### sub loadAccounts { open(FILE,"accounts.txt") or die "$!"; my @accountsarray = <FILE>; close(FILE); my @temp = map {(split(/\s+/))[0]} @accountsarray; return @temp; } ##################################################################### # randomAccounts generates 10000 random account numbers # Parameters: none # Returns: Array of 10000 random account numbers ##################################################################### sub randomAccounts { my @search_accounts = (); for (my $i = 0; $i < 10000; $i++) { $search_accounts[$i] = $accounts[int(rand $#accounts) + 1]; #using split to get the account numbers instead of a regexp $search_accounts[$i] = (split(/\s+/,$search_accounts[$i]))[0]; chomp($search_accounts[$i]); } return @search_accounts; } -------------------------------------------------
|