skp has asked for the wisdom of the Perl Monks concerning the following question:
Monks, I have a need to search and replace portions of array elements from elements of another array.
I am attempting to split both arrays and splice if there is a match. The ideal output would look like this for the first match of element of @dp2 and 4th of @stu2. I also need to keep intact any non matching elements of @stu.
103_dsu 1 35 nbu103 Disk 6 -1
@stu = (103_dsu 1 35 nbu103,104_dsu 1 35 nbu104,204_tape 1 1 ACS1,101_ +dsu 1 35 nbu101); @dp = (nbu103 Disk 6 -1,nbu104 Disk 6 -1,nbu101 Disk 6 -1); foreach $var (@stu) { foreach $var2 (@dp) { @stu2 = split(' ',$var); $s1 = $stu2[3]; @dp2 = split(' ',$var2); $s2 = @dp2[0]; if ($s1 eq $s2) { splice (@stu2,3,1,$var2); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Search and Replace Elements from 1 array to another.
by Riales (Hermit) on Apr 03, 2012 at 00:04 UTC | |
by skp (Initiate) on Apr 03, 2012 at 14:58 UTC | |
by Riales (Hermit) on Apr 03, 2012 at 17:46 UTC |