Click here to Skip to main content
Sign Up to vote bad
good
See more: Perl
Hi,
 
I am unable to replace all the array1 elements with corresponding array2 elements in a file. Here is my code
 
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 13 Aug '12 - 20:36
Edited 13 Aug '12 - 22:50

Comments
Uilleam - 5 Sep '12 - 10:01
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,028
1 OriginalGriff 6,010
2 CPallini 3,432
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid