File one is:
I_pron would_mod like_v to_to learn_v this_pron ._sent OK_ok ?_quest
and the second one is in this format:
I would like to learn this . OK ?
Okay... let me be sure I understand what you're asking...

I'm guessing that your goal is to parse file two, and match each word against a lookup file (file one, which is in word_article format) and output the lookup file version of file two.

So, a more generalized way of asking is: Take a file/sentence, and lookup each token of the file/sentence and output the look-up version of the tokens.

In other words, an instant Perl sentence diagrammer :)

Assuming that's true, here's what I come up with (keeping the basic structure of your code intact... and fully admitting I'm not the uber coder some here are):

$DOC1="1.txt"; $DOC2="2.txt"; # Create an associative lookup array from file 1 open (DOC1,$DOC1); while (<DOC1>) { chomp($_); $ind = (split /_/)[0]; $lookup{$ind} = $_; } close (DOC1); open (DOC2,$DOC2); @lines=<DOC2>; close (DOC2); $outln = ''; foreach $line (@lines) { @words = split(/ /, $line); foreach $word (@words) { chomp($word); # Get rid of stray carriage return $outln = $outln . " " . $lookup{$word}; } } print "$outln\n";
That close to what you're after?

Trek


In reply to Re: replacing while keeping the position of the element in the file by TrekNoid
in thread replacing while keeping the position of the element in the file by dra2pac

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.