Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Problem with deleting the first n characters on a line

by CyCliC (Novice)
on Jul 18, 2001 at 16:17 UTC ( [id://97622]=perlquestion: print w/replies, xml ) Need Help??

CyCliC has asked for the wisdom of the Perl Monks concerning the following question:

I have a file containing this:

gpu/aps/mcp/mc_server/index.html
gpu/aps/mcp/mca_rtsp/index.html
gpu/oms/subagent/testagent/index.html
sys/netbsd/src/sys/netstreams/index.html

and I use this to delete gpu on the first line:
($str contains "gpu")

open(FOUND, "+<found"); $first_line = <FOUND>; $tmp = length $str; $tmp = $tmp +1; $linetmp = substr($first_line, $tmp); print FOUND $linetmp;
but I get this as a result:

gpu/aps/mcp/mc_server/index.html
gpu/aps/mcp/mca_rtsp/index.html
gpu/oms/subagent/testagent/index.html
sys/netbsd/src/sys/netstreams/index.html
gpu/aps/mcp/mc_server/index.html <-- where did this line come from
aps/mcp/mc_server/index.html

please help!

Replies are listed 'Best First'.
Re: Problem with deleting the first n characters on a line
by nysus (Parson) on Jul 18, 2001 at 16:35 UTC
    I would use a substitution operator to get rid of the leading "gpu" like so:
    $first_line =~ s/^gpu//; print FOUND $first_line;
    See perlop for more about substitution and perlre for more on regular expressions. Also, it doesn't look like you are using strict. Strict forces you to used good coding practices and helps you catch a lot of coding errors. Always, always put "use strict" shortly after the shebang line.

    Update: If you just want to get rid of the first 3 arbitrary characters, use s/^.{3}//;

Re: Problem with deleting the first n characters on a line
by Cubes (Pilgrim) on Jul 18, 2001 at 18:13 UTC
    You're not resetting your file position before writing back into FOUND (and even if you did, I don't think this code would do what you expect because the new string you write out is shorter than the original).

    The quick 'n' easy way: open up a second file for output. Loop through the entire input file, changing lines as necessary before printing them to the output file. At the end, remove the original and rename your output file to the original's filename.

Re: Problem with deleting the first n characters on a line
by CyCliC (Novice) on Jul 18, 2001 at 17:34 UTC
    thanks for your help

    the "use strict" tip was very needed
    but I still have the same problem, the extra line in the found file

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://97622]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-10-05 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (43 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.