Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am unable to replace all the array1 elements with corresponding array2 elements in a file. Here is my code

PERL
open FILE1, "<Test3.txt" or die " Cannot open to write file!";
my @newlines;
while (<FILE1>)
{
 chomp($_);
 $_ =~ s/@array1/@array2/g;
 push(@newlines,$_);
} 
close FILE1;

However, each array element is getting replaced individually..
I tried using for loop also, but didn't work. Please help.


Thanks in Advance,
Posted
Updated 13-Aug-12 22:50pm
v2
Comments
Uilleam 5-Sep-12 10:01am    
Just a few thoughts:
-you're opening the file to read not write as you appear to think
-what is it exactly that you think that substitution will do? I'm only guessing, but do you want $array1[0] to be replaced by $array2[0], and so on pairwise in the array? If so, that's not how s/// works. You will need to use a loop through your arrays, but since you don't show what didn't work in your attempt I can't help you further with that.

In general, the clearer your question the better the answer will be. As much as I'm happy to help, I don't have the time to code up solutions, make up sample data etc. If you provide a lot more on what your input is, what you tried, what output you got and what output you desire, you'll probably get more help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900