CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Using one file to manipulate another

 



perl_slave
Novice

Dec 5, 2008, 8:39 PM

Post #1 of 4 (488 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:

Code
$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 Mad. Right now I'm trying to get to STDOUT - for testing purposes, then will dump it to a file.

Any thoughts?


seema
New User

Dec 5, 2008, 9:35 PM

Post #2 of 4 (485 views)
Re: [perl_slave] Using one file to manipulate another [In reply to] Can't Post

Hello,
I have a solution for ur input and output. There were minor mistakes in ur that program, I have corrected it.

use strict;
use warnings;

my $filename1 = 'file1.csv';
my $filename2 = 'file2.txt';
open CSV, $filename1 or die "$filename1 File can't open! $!";
open TXT, $filename2 or die "$filename2 File can't open! $!";

# if,
# $incoming = <TXT>
# All the lines will be saved in the single variable.
# Then in the first loop itself all the lines will be taken.
# And more to that why you need a scalar to pass in the foreach?
my @incoming = <TXT>;

# for iteration we need to save it in an array.
# if we don't then we can't iterate after it reaches the EOF.
my @compare = <CSV>;

my $line;
my $id;
foreach $line (@incoming) {
foreach $id (@compare) {
# this is must, because the $remove_this will have new line with it.
# so it will never match with the $line.
chomp($id);
my ($blah1,$blah2,$remove_this) = split /\,/, $id;

if ($line =~ /$remove_this/) {
($line =~ s/$remove_this//g);
print $line;
last;
}
}
}


KevinR
Veteran


Dec 5, 2008, 10:40 PM

Post #3 of 4 (481 views)
Re: [perl_slave] Using one file to manipulate another [In reply to] Can't Post

Without addressing the problems with your script, here is an alternative way using a hash to store the values of the CSV file to compare and remove the corresponding values in the other file:


Code
use strict;  
use warnings;
my $filename1 = 'file1.csv';
my $filename2 = 'file2.txt';
open CSV, $filename1 or die "$filename1 File can't open! $!";
my %file1 = map {chomp; my $remove = (split/,/,$_)[2]; $remove => $remove} <CSV>;
close CSV;

open TXT, $filename2 or die "$filename2 File can't open! $!";
while (<TXT>) {
if (/^(\d+)\D+$/ && exists $file1{$1}) {
s/$file1{$1}//;
print;
}
else {
print;
}
}

-------------------------------------------------


perl_slave
Novice

Dec 6, 2008, 2:59 PM

Post #4 of 4 (468 views)
Re: [seema] Using one file to manipulate another [In reply to] Can't Post

Thank you very much seema! Originally I was doing like Kevin was, but I'm being forced to program this thing so completely stupid, its gonna be wearing a helmet by time its done.

The program has no protocol for data input so I have to have a predefined list possible input names. They could be calling the items in file1 something like:

christmas
bigfoot
fred22
...

So its really ridiculous on the project leader to make be this way; but I digress. Thanks you guys for your help.


(This post was edited by perl_slave on Dec 6, 2008, 3:05 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives