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:
Copy and change contents of one file to another

 



madonion55
New User

Jan 15, 2013, 8:27 PM

Post #1 of 8 (952 views)
Copy and change contents of one file to another Can't Post

Hi, I'm relatively new to PERL, so forgive me if this question is a little basic. I'm working on a project where the user supplies a file. I wish to copy the contents of this file to a different file, while searching for specific variables and changing the value of these variables. Also, if a variable I'm searching for doesn't appear in the original file, I would like to add it to the copied file. So basically if File1 contains:

$Var1 = 1;
$Var2 = 2;

I would like to copy this to File2, changing the values of Var1 and Var2, while adding the line $Var3 = 6, so that File2 contains:

$Var1 = 4;
$Var2 = 5;
$Var3 = 6;

Thanks for you help


7stud
Enthusiast

Jan 15, 2013, 11:38 PM

Post #2 of 8 (937 views)
Re: [madonion55] Copy and change contents of one file to another [In reply to] Can't Post

Post the code you have so far, and state what problems you are encountering?


(This post was edited by 7stud on Jan 15, 2013, 11:39 PM)


madonion55
New User

Jan 16, 2013, 10:33 AM

Post #3 of 8 (924 views)
Re: [7stud] Copy and change contents of one file to another [In reply to] Can't Post

Sorry, what I have right now is:

open(FILE, $Filename1) || die "File not found";
my @lines = <FILE>;
close(FILE);

my @newlines;
foreach(@lines) {
$_ =~ s/Var1/Variable1/;
print $_;
push(@newlines,$_);
}
open(FILE, $Filename2) || die "File not found";
print FILE @newlines;
close(FILE);

Here I'm simply reading the contents of Filename1, finding the string $Var1, and replacing it with the string $Variable1. This is not exactly what I want to do though. I would like to replace the value of $Var1 after the equals sign, but I don't know how to do that. I would also like to scan Filename1 for each variable, and if it is not in Filename1, add it to Filename2 directly below an anchor point, say Var0.

I appreciate your help!


7stud
Enthusiast

Jan 16, 2013, 12:37 PM

Post #4 of 8 (913 views)
Re: [madonion55] Copy and change contents of one file to another [In reply to] Can't Post

In modern perl, you don't use bareword filehandles.
In modern perl, you use the 3-arg form of open.
In modern perl, you output the system error message if open() fails.
In modern perl, you start every program with the equivalent of the first three lines below.


Code
use strict; 
use warnings;
use 5.012;

my %new_values_for = (
Var1 => ['Variable1', 10],
Var2 => ['Variable2', 20],
Var3 => ['Variable3', 30],
);

open my $OUTFILE, ">", 'results.txt'
or die "Couldn't open results.txt: $!";

for my $line (<DATA>) { #The DATA filehandle reads any lines after __DATA__

if ($line =~ /(\w+) \s* = /xms) {
my $var_name = $1;

#Check if there is an entry in the hash for the current var name:
if (my $values = $new_values_for{$var_name}) {
my($new_var_name, $new_value) = @{$values};

say $OUTFILE "$new_var_name = $new_value";

delete $new_values_for{$var_name};
}
else { #Variable found, but no new values are specified
print $OUTFILE $line;
}
}
}


for my $key (keys %new_values_for) {
my($var_name, $value) = @{$new_values_for{$key}};
say $OUTFILE "$var_name = $value";
}

close $OUTFILE;



__DATA__
Var1 = 1
junk here
Var2 = 2

Note that the output from that last loop will be in random order.


Code
$ cat results.txt 

Variable1 = 10
Variable2 = 20
Variable3 = 30



(This post was edited by 7stud on Jan 16, 2013, 12:57 PM)


madonion55
New User

Jan 16, 2013, 1:40 PM

Post #5 of 8 (895 views)
Re: [7stud] Copy and change contents of one file to another [In reply to] Can't Post

I appreciate your quick replies. I tried copying and pasting your code to test it out, but it turns out I only have Perl V5.8.4 available. I tried your script with this version anyway, but I got an error saying it can't locate object method "say" via package "FileHandle." Is there a way around this?


7stud
Enthusiast

Jan 16, 2013, 4:17 PM

Post #6 of 8 (888 views)
Re: [madonion55] Copy and change contents of one file to another [In reply to] Can't Post

Yes, use print() instead. say('hello') is equivalent to print("hello\n").

Also get rid of the line:

use 5.012;


(This post was edited by 7stud on Jan 16, 2013, 4:21 PM)


madonion55
New User

Jan 16, 2013, 7:00 PM

Post #7 of 8 (880 views)
Re: [7stud] Copy and change contents of one file to another [In reply to] Can't Post

Thanks 7stud, this is mostly working. However, this code seems to be deleting all the text in the original file, resulting only in

Variable1 = 10
Variable2 = 20
Variable3 = 30

but I would like to retain everything that isn't modified such that the result.txt is

__DATA__
Variable1 = 10
Variable2 = 20
junk here
Variable3 = 30

I saw the line $ cat results.txt, which seems like it may to concatenating, but I wasn't sure where this goes. How do I retain all data that isn't being modified?


7stud
Enthusiast

Jan 16, 2013, 11:38 PM

Post #8 of 8 (869 views)
Re: [madonion55] Copy and change contents of one file to another [In reply to] Can't Post

You should be able to think about how the if-statement works and modify the code to do what you want.

 
 


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

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