title: Perl: Number every instance of a word in a text file
body: <p>I need to edit a text file that is formatted something like:</p>
  
  <pre><code>&gt; Word text text text text
  Text
  Text
  &gt; Word text text text text
  Text
  Text
  </code></pre>
  
  <p>And make it look like:</p>
  
  <pre><code>&gt;Word1 text text text text
  Text
  Text
  &gt;Word2 text text text text
  Text
  Text
  </code></pre>
  
  <p>Basically, I need to modify every instance of the string " Word" and turn it into "Word" followed by a number corresponding to the number of instances the string has appeared in the text file so far. I'm new to perl and have no idea what I'm doing. Here's what I have:</p>
  
  <pre><code>$file = "test.txt";
  
  my %count;
  my $word = " Word";
  
  #Open and read data
  open (FILE, "&lt;$file") or die "Cannot open $file: $!\n";
  @lines= &lt;FILE&gt;;
  foreach my $word{
      my $count++;
  }
  close FILE;
  
  my $counter = my $count + 1;
  my $curr_ct = my $counter - my $count;
  
  #Open same file for editing now
  open (STDOUT, "&gt;$file") or die "Cannot open $file: $!\n";
  
  
  while (my $count &gt; 0){
      s/ Word/Word$curr_ct/
      my $count--;
      my $curr_ct = my $counter - my $count;
      print;
  }
  
  close STDOUT;
  </code></pre>