Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:49:49+00:00 2026-06-10T17:49:49+00:00

I have a file handler to a file and I’m looking for matches in

  • 0

I have a file handler to a file and I’m looking for matches in the lines and replacing the match with a new line. Replacing the lines happen in a subroutine.

sub replace{
   seek(FILE,0,0);
   while(my $line= <FILE>){
       if($line =~ m/SOMEMATCH/){
            $line=~ s/SOMEMATCH/REPLACEMENT/;
            print FILE $line;
       }
    } 
}

When I print the file after calling &replace I find that the wrong line was changed so:

Line 1
Line 2
Line 3
SOMEMATCH
Line 4
Line 5

becomes:

Line 1
Line 2
Line 3
SOMEMATCH
REPLACEMENT
Line 5

What’s going on? How do I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:49:51+00:00Added an answer on June 10, 2026 at 5:49 pm

    I don’t think you can read in a text file and replace parts of it – in place – with strings that are longer or shorter and get the results you want. There has to be an overwriting of some data by the longer new string or a “hole” will be left if the string is shorter. If the strings happen to be the same length your logic still has a problem – replacing the string would require you to back up since you are past the string you want to replace after you have read it. Writing a replacement at that point should overwrite the next line since that is where the file “pointer” is currently set to ready to read the next line.

    I think something like this is best done by having a separate output file. Read one, write the other, and if necessary, delete the first and rename the second.

    Or you could read the entire file into an array, make your string replacements, close and reopen the file for over-writing, and write the array back to the file, and close the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a download handler that will download the file when in IE but
I have files served like so: AJAX request handler -> Include file I would
I have this code in a xaml file now in a event handler code
We have jboss logging enabled in our application. Currently we are using the 'Rotating-file-handler'
I have a simple 'file download' generic handler which sets the response contenttype and
I have the following file upload handler: public class FileUploader : IHttpHandler { public
I have a file handle to a serial (COM) port. I need to read
I have a SVG file that I want to extend by adding onclick handlers
I have two HANDLEs and they are created from the same file, in such
I have a ASP.NET MVC3 application that handles time-consuming processes (copying a large file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.