in reply to replacing while keeping the position of the element in the file

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