Pink
Pink

Reputation: 161

Regular expression with hash keys and values

I am trying to check if the certain terms appear in the hash key, if they I tag them with the hash values. My code identifies an exact match but fails for terms that are slightly different. Also for the exact match the code tags the matched term with all values instead of the relevant one.

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    for $keys(keys %Drugs)
    {
        if($Drugs{$word})
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
    }
    foreach $Bio(@array)
    {
        print "$Bio\n";
    }

I want the output Sentence:

<tablet>tamoxifen</tablet> <tablet>4-hydroxytamoxifen</tablet> <tablet>tamoxifen-incoded<tablet> <capsule>pre-docotaxel<capsule> Drugs.(Here Drugs didn't match and hence it is left untagged)  

Upvotes: 0

Views: 502

Answers (1)

Amey
Amey

Reputation: 8548

You were checking for an exact match, but based on what you expected you had to regex your $word against the $key

try this - >

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    for $keys(keys %Drugs)
    {
        if($word=~/.*$keys$/i)#Changed this
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
        }
    }
}
foreach $Bio(@array)
  {
     print "$Bio\n";
  }

UPDATE


Based on the new requirements

%Drugs = ('tamoxifen' => tablet,
        'docotaxel' => capsule,
        '5-hydroxycin' => IDK,);

$sentence="tamoxifen 4-hydroxytamoxifen tamoxifen-incoded pre-docotaxel Drugs";
@sentence=split/ /,$sentence;
@array=();
foreach $word(@sentence)
{
    chomp $word;
    my $flag= 0; #using a flag, I am sure there are PLENTY of ways better than this :)
    for $keys(keys %Drugs)
    {

        if($word=~/.*$keys$/i)#Changed this
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
            $flag++;
        }
        elsif($word=~/.*$Drugs{$keys}$/i)
        {
            $term="<$Drugs{$keys}>$word<$Drugs{$keys}>";
            push(@array,$term);
            $flag++;
        }

    }
    push (@array,$word) if $flag==0;
}
foreach $Bio(@array)
  {
     print "$Bio\n";
  }

Upvotes: 2

Related Questions