Mark Goland wrote:
>
> I have a tring which contains 60 to 80 keywords, I need to know what position a
> keyword is in. I need an efficint way to extra position of key words, here is
> how I am currently finding positions,
>
> $string="type,model,color,date";
>
> # this is how I am currently getting position, which is nto very efficiant
> @keys=split ",",$string;
> foreach( @keys ){
>  $position++;
>  last if m/model/ig;
> }

Hi Mark.

If you build a hash where the keywords are the keys and their positions are
the values then you can just do a simple hash lookup:

  use strict;
  use warnings;

  my $string = "type,model,color,date";

  my @keys = split /,/, $string;

  my %position;
  @[EMAIL PROTECTED] = 1 .. @keys;

  print $position{'model'};

It's important that your hash is static though, it's going to be a lot slower
to rebuild it each time you need it.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to