Click here to Skip to main content
12,949,858 members (73,453 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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>)
 $_ =~ s/@array1/@array2/g;
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
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
OriginalGriff 5,749
CHill60 3,380
Maciej Los 2,953
Jochen Arndt 1,935
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web01 | 2.8.170524.1 | Last Updated 14 Aug 2012
Copyright © CodeProject, 1999-2017
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