Click here to Skip to main content
12,394,552 members (73,616 online)
Rate this:
 
Please Sign up or sign in to vote.
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:36pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100