
perl_slave
Novice
Dec 5, 2008, 8:39 PM
Post #1 of 4
(379 views)
|
|
Using one file to manipulate another
|
Can't Post
|
|
Hey folks, I've been working on this problem for a few days and it is really got me all bothered. Forgive the lengthy post, but I'm trying to supply as much info as I can. What I'm trying to do is take one file (a CSV) which contains the following: file1.csv ,,01 ,,02 ,,03 ,,04 ... And use against another file containing something like this: file2.txt 01sd 02x 03rwL ... I'm wanting to use file1's content against file2, to get back: sd x rwL ... Here's where I'm at so far:
$filename = 'file1.csv'; open CSV, "<," $filename or die "File not found!\n"; open TXT, "< file2.txt"; undef $/; $incoming = <TXT>; $/ = "\n"; foreach $line ($incoming) { foreach $id (<CSV>) { ($blah1,$blah2,$remove_this) = split /\,/, $id; if ($line =~ /$remove_this/) { $line =~ s/$remove_this//g; print $line; } } } Basically the output does a crazy loop, it will match it and remove it, but it continues to print the contents of file2.txt e.g. output rw 02rd 03rw 04x ... (run content of file2.txt then it does this -) rw rd 03rw 04x ... (writes out the rest of tile2.txt then) rw rd rw x ... And does this through the entire content of file2.txt I've tried doing loop blocks, but it will only do one pass and die. All I want it one iteration of the list, not 30! I know there is a way getting the output I want, after two days I haven't been able to find it . Right now I'm trying to get to STDOUT - for testing purposes, then will dump it to a file. Any thoughts?
|