----- Original Message ----- From: Pablo Wolter <[EMAIL PROTECTED]> Date: Thursday, March 24, 2005 3:43 pm Subject: Help working/searching fields in 3 files
> Hi, Hello, > > I need some ideas because I'm just trying and error programming. > > I have some .dat files that come in this format (call this file > dat.dat): > # > # Field1 description of Field1 > # Field2 description of Field2 > # Field3 description of Field3 > # Field4 description of Field4 > # Field5 description of Field5 > # > "Field1" "Field2" "Field3" "Field4" "Field5" > "Field1" "Field2" "Field3" "Field4" "Field5" > "Field1" "Field2" "Field3" "Field4" "Field5" > "Field1" "Field2" "Field3" "Field4" "Field5" > "Field1" "Field2" "Field3" "Field4" "Field5" > > And I have another two files, one a csv file with this format > (call this file csv.txt): > > Description Field1a, Description Field2a > Field1a, Field2 > Field1a, Field2 > Field1a, Field2 > Field1a, Field2 > > And another one that just contains (call this file pc.txt); > > Field2 > Field2 > Field2 > Field2 > > What I need to do is search for each field that appears in pc.txt, > if this field doen't exists in the dat.dat file, then > I need to delete the row in csv.txt that contains Field2. > > To search this I have this: > > > sub openReadFiles { > sysopen(PCERROR, "$dir/$pointcodeError", O_RDONLY) > or die "Cannot open file $pointcodeError for reading!\n"; > @pcError = <PCERROR>; > chomp(@pcError); > > > sub openWriteFiles { > sysopen(PCCSV, "$dir/$ss7SigGroupPointcode", O_RDWR) > or die "Cannot open file $ss7SigGroupPointcode for > read/write!\n"; @pcCsv = <PCCSV>; > chomp(@pcCsv); > } > > sub ParseData { > @out = @_; > for (@out) { > s/^\s+//g; > s/\s+$//g; > s/\s+/ /g; > # s/\d+,//g; > # s/\d+'//g; > } > return wantarray ? @out : $out[0]; > } > > sub closeAll { > close(PCERROR); > close(SS7NODE); > } > > > sysopen(SS7NODE, "$dir/$ss7Node", O_RDONLY) > or die "Cannor open file $ss7Node for reading!\n"; > openReadFiles(); > while (<SS7NODE>) { > (($nodeId, $name, $nodeSetId, $pointCodeId, $nodeType, $desc, > $monitored) = split(' ')) if ($_ !~ /^#/); #This is > to not consider the # at the begining of the .dat file > foreach $pcError (@pcError) { > > print ("\t\t$name\n"); > } > closeAll(); > > But I don't know how to do it to test for the Field2 and delete > this field from csv file > > Hope somebody can help me. I think I just need some guides or > examples to see how this kind of things could be achieved. I think the main problem with your code, is lack of proper data strcutures which perl offers. You should try a more searchable structure such as a hash, so that there exists common keys/fields betwean each structure. Here is one way you can achive this task: #!PERL use warnings; use strict; # Lets get our dat.dat into a structure my %Dat_Hash; open RD, "dat.dat" or die "ERROR: $!\n"; foreach my $line ( <RD> ){ chomp $line; my @tmp = split ' ',$line; $Dat_Hash{ $tmp[0] } = $line; } close RD; # Now lets get the csv stuff my %Csv_Hash; open RD, "csv.txt" or die "ERROR: $!\n"; foreach my $line ( <RD> ){ chomp $line; my @tmp = split ' ',$line; $Csv_Hash{ $tmp[1] } = $line; } close RD; # and now we process pc.txt open RD, "pc.txt" or die "ERROR: $!\n"; my $debug; foreach my $line ( <RD> ){ chomp $line; print "Match => $line !!!\n" if $Dat_Hash{$line} && $debug; delete $Csv_Hash{$line} if $Dat_Hash{$line}; } close RD; # Now you have your data in %Csv_Hash print $Csv_Hash{$_},"\n" for keys %Csv_Hash; > > Thanks in advance > > -- > (o_ Pablo A. Wolter N. > //\ Usuario Registrado #284649 > V_/_ Linux Debian Sid Kernel 2.6.8 > > "Pienso....luego instalo Linux....entonces existo." > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>