For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > Help working/searching fields in 3 files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help working/searching fields in 3 files
Pablo Wolter

2005-03-25, 3:55 am

Hi,

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.

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."
mgoland@optonline.net

2005-03-25, 3:55 am



----- Original Message -----
From: Pablo Wolter <pwolter@gmail.com>
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 ta
sk:

#!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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


Pablo Wolter

2005-03-25, 8:55 pm

mgoland@optonline.net wrote:
>
> ----- Original Message -----
> From: Pablo Wolter <pwolter@gmail.com>
> Date: Thursday, March 24, 2005 3:43 pm
> Subject: Help working/searching fields in 3 files
>
>
>
>
> Hello,
>
>
>


[...]

>
> 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:
>


At the begining I think on hash, but I get with the key and that stuff

> #!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 for the ideas, I'll try this aproach and see if works for the job I need to do. What I was thinking to do is to
work directly on the csv files, doing a search line by line and delete the lines that don't appear on the dat file but
are on the pc.txt. Thanks for the help.

>
>
>
>


Pablo.

--
(o_ Pablo A. Wolter N.
//\ Usuario Registrado #284649
V_/_ Linux Debian Sid Kernel 2.6.8

"Pienso....luego instalo Linux....entonces existo."
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com