hi monks I am having problem in parsing a file with another file...
First file contains the name, around 500 (only one column, text file) and another file which contains name as first column and second column contains its synonyms(may be one or two or three or four)....

now what I did, I made a hash of first table with key as name value as name itself. and i read the second file and tried to match the name column with the hash and wrote 2 files, one which matched and another which doesn't match........

the problem is I found that some of the names are matching to the synonyms which I was not able to match through script and were pooled out to not match list............. Is there a way so that when I start matching with hash, program checks the name in the synonyms also and when there is a match it returns the name as well as the synonyms.... here is my code:

#!/usr/bin/perl use strict; use warnings; my %hash; my($name,$val,@nam); my ($cnt1,$cnt2) = 0; open(WRITE1,">match_name"); open(WRITE2,">nomatch_name"); open(DATA1,"<name500") or die "Could not open the relevant file"; while(<DATA1>) { chomp; ($name,$val) = split(/\t/,$_); $hash{$name}=$name; } close(DATA1); open(DATA2,"name_syn") or die "Check file"; while(<DATA2>) { chomp $_; if($_=~/^#/) { next; } @nam = split(/\t/,$_); if (exists $hash{$nam[0]}) { print WRITE1 "$hash{$nam[0]}\t$nam[1]\n"; $cnt1 +=1; } else { print WRITE2 "$hash{$name}\n"; $cnt2 +=1; } } print "$cnt1\t$cnt2\n"; close(WRITE1); close(WRITE2);

when I executed this program I got 422 matches and 78 mismatches but when I looked through file 2 I found that some name matched to the synonym and so they were filtered to 'noname_match' file......how to modiy the code so that it matches the name or synonyms....

thanks...


In reply to hash and matching synonyms by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.