Hi all,

I'm pretty much a n00b when it comes to perl, so forgive me if my question is, well, a stupid one.

I have written a program that takes a list of words and values as an argument plus a list of files. I have put the wordlist in a hash and the file list in an array. The files in the file list are documents that contain the same words as the wordlist, but not all words are in every document.
If the document does contain a certain word, then all occurences need to be replaced with "ECHO", except for the first occurence. I have checked all variables with print statements and it seems that everything is going okay, but my .out files are exactly the same as the input files. So it seems something is wrong with the way I'm printing the substitutions to the .out file.
This is my program:
#! /usr/bin/perl -w $echo="ECHO"; open (DFFILE,$ARGV[0]) || die "DF-file not found\n"; open (LIST,$ARGV[1]) || die "List not found\n"; while (<DFFILE>) { ($value, $key) = split(/\t/, $_); $lijst{$key} = $value; } @listfiles = <LIST>; #print @listfiles; foreach $key (sort keys %lijst) { # print "The value associated with key $key is $lijst{$key}\n"; if ($lijst{$key}==1) { my $word = $key; for $file (@listfiles) { open (FILE,"$file"); open (OUT,">$file.out"); while ($_=<FILE>) { if ($_ =~ m/^$word$/) { $_ =~ s/$word/$echo/g; $_ =~ s/$echo/$word/; } print OUT $_; } close(FILE); close(OUT); } } } close(DFFILE);
Any help would be greatly appreciated!

Matje

In reply to problem with string substitution output 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.