CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Very new to perl, having trouble editing a file, in which there needs to be conversions, and deletion.

 



neurolou1
New User

Sep 20, 2014, 1:36 PM

Post #1 of 2 (3549 views)
Very new to perl, having trouble editing a file, in which there needs to be conversions, and deletion. Can't Post

Really need help.

I am teaching myself perl and I am confused about some commands.

I am trying to edit a txt file. Here is what I got so far.
---------------------------------------------------------------------------
#!/usr/bin/perl
use warnings; use strict;


open (INFILE, "file.txt")
or die "can't find the file";

my $Trfile = <INFILE>;

#in this part I am eliminating a sentence
while ( my $Trfile =<INFILE>{
$Trfile =~ s/\Accordingly_The_following_must_be_accepted/;

print $Trfile;
}

#from the first while loop, I now want to edit the file more, with the result from the first while loop, it doesnt include the sentence.
while ($Trfile = <INFILE>){
{
$Trfile =~ s/AEIO/OEIA/g;
print $Trfile;
}
# basically editing the file more, with a new function, from the first loop. The values though are not showing up
---------------------------------------------------------------------------

Basically what I am trying to do print the step by step process to show my progress. I am trying to get the values from the first while loop, into the second while loop.
How do you transition values in scalars to other while loop statements, and from there to other while loops?
I am basically trying to use a step by step process for editing this file.
Basically as the file goes through each function a process needs to happen to edit it.

I am so confused
FrownFrownFrownFrown


Laurent_R
Veteran / Moderator

Sep 21, 2014, 8:02 AM

Post #2 of 2 (3486 views)
Re: [neurolou1] Very new to perl, having trouble editing a file, in which there needs to be conversions, and deletion. [In reply to] Can't Post


Code
use warnings; use strict;

Very good, keep on doing that for every program you write.


Code
open (INFILE, "file.txt") 
or die "can't find the file";


This works, but is an outdated syntax. It is better to use lexical file handlers and the so-called 3-argument syntax of open:

Code
my $input_file = "file.txt"; 
open my $INFILE, "<", $input_file or die "can't open $input_file $!";

Of course, if you switch to a lexical filahandler such as "$INPUT", you'll have to change "INPUT" to "$INPUT" in the rest of your program.

The "<" part says that you open the file for reading, and the "$!" variable contains the the error if the file can't be open.


Code
my $Trfile = <INFILE>;

This is possibly wrong, because it reads only the first line of the file, maybe not what you want (or maybe that's what you want, you don't say enough). On the other hand, this is right if you just want to skip the first line of the file.

Code
while ( my $Trfile =<INFILE>{

Syntax error: you need a closing parenthesis after "<INFILE>".


Code
$Trfile =~ s/\Accordingly_The_following_must_be_accepted/;

What are you trying to do here? The syntax is wrong. Assuming you want to remove "Accordingly_The_following_must_be_accepted" from any line where if appears, the syntax would be something like this:

Code
$Trfile =~ s/Accordingly_The_following_must_be_accepted//g;

Now we come to the major defect in your program:

Code
#from the first while loop, I now want to edit the file more, 
#with the result from the first while loop, it doesnt include the sentence.
while ($Trfile = <INFILE>){

This will not work. Your first while loop has read the file up to the end. If you try to read again from the same file handler, you will not get any input. You would need to restart from the beginning; for this you would have to re-open the file or reset the filehandler to the beginnning. But even that would not be a very good idea (inefficient and not very useful). If you need to do several changes in a file, include all of them within the same while loop.

There is a deeper problem, although you did not state it completely explicitly: you basically cannot edit a file this way. What you can do is edit the content of a file and print the result of this editing into another file (or display it to the screen). If you start reading the file a second time, the changes you made in the first pass will not show up, since you will be reading the original file again.


Code
$Trfile =~ s/AEIO/OEIA/g;

This would replace the "AEIO" sequence with the "OEIA" sequence, possibly not what you want. If you want to replace letter per letter, use the tr// operator instead:

Code
$Trfile =~ tr/AO/OA/;

Putting all this together, you might try this:

Code
#!/usr/bin/perl 
use warnings;
use strict;

my $input_file = "file.txt";
open my $INFILE, "<", $input_file or die "can't open $input_file $!";
my $Trfile = <$INFILE>; # assuming you want to skip line 1
while ($Trfile =<$INFILE>) {
$Trfile =~ s/Accordingly_The_following_must_be_accepted//g;
$Trfile =~ tr/AO/OA/;
print $Trfile
}

I hope this helps. Please don't hesitate to ask if there is something you don't understand.


(This post was edited by Laurent_R on Sep 21, 2014, 8:06 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives