Ramprasad A Padmanabhan wrote:
>
> I know this is more of an algorithm question but please bear with me.
>
>
> In my program I am checking wether a emailid exists in a list
> I have in the complete_list a string like
> $complete_string="<[EMAIL PROTECTED]> <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> ... 
> <[EMAIL PROTECTED]>";
>
> #  that is a string of emailids sorted
>
> Now I want to find out if another ID  "<[EMAIL PROTECTED]>" exists in this list
>
> How is it possible to optimize the search  given that the complete list
> string is sorted in order
>
> The alternative I am considering is
>    1) Create a hash array with each of the ids as keys and just use the
> exits function
>
> like
> my %hash=();
> while($complete_list=~/(\<.*?\>)/g){ $hash{$1}=1 };
> ....
> if(exists($hash{$emailid})) {
> # FOUND id in string
> }
>
> Is this the best way
>
> Is there no way directly to use the string

Hi Ram.

If you want to check many different mail addresses against the list then it
will be best to build a hash. You could assign a slice, like this:

  my %hash;
  @hash{ $complete_string =~ /(\<.*?\>)/g } = ();

  if (exists $hash{$emailid}) {
    :
  }

but beware that the values for each element will be
'undef' if you do this, so that you have to use 'exists'
instead of 'defined'.

If you're just testing a single mail address then you'd probably
be better off doing

  if (index($complete_string, $emailid) >= 0) {
    :
  }

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to