carcus88 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use XML::Twig; use DBI; use DBD::Pg; use SQL::Abstract; use File::Copy; use File::Basename; my $inFile = 'data_100000_100500.xml'; if ( ! $inFile ) { die("No input file specified"); } if ( ! -f $inFile ) { die("file '$inFile' not found"); } my $dbname = "test"; my $user = "test"; my $password = "test"; my $host = "test03.server.com"; my $port = "5432"; my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port", +$user, $password, {AutoCommit => 0}); my $sql = SQL::Abstract->new(quote_char=>'"'); my @missing; my @localmissing; my $trust = 0; my $localcount; my $count; my $sth; my $fileStartID; my $fileEndID; my %BIOG; process($inFile); if (@missing) { open(MISSINGFILE, ">>missing.txt"); foreach my $missing (@missing) { print MISSINGFILE $missing . "\n"; } close MISSINGFILE; print "\nUNVERIFIED see missing.txt for missing records.\n"; } else { print "\nVerified 100%\n"; } exit 0; # # Process the file # sub process { %BIOG = (); $inFile =~ /data_(\d+)_(\d+)/; $fileStartID = $1; $fileEndID = $2; $localcount = 0; print "Processing file " . $inFile . "\t"; my $t= new XML::Twig( TwigHandlers=> { BIOG => \&BIOG }, ); $t->parsefile( $inFile ); $t->dispose(); # Try to Free memory but does not work... if ( @localmissing ) { push(@missing,@localmissing); my $missing = @localmissing; print "Missing $missing/$localcount \n"; } else { print "Verified 100%\n"; my $folder = dirname($inFile); $folder =~ s/data_done/data_verified/; move($inFile, $folder.basename($inFile)); } } # # BIOG is XML element we are triggering # sub BIOG { my ($t, $BIOG)= @_; ++$localcount; if ( ! checkBiog($BIOG->field('BIOG_NBR')) ) { push(@localmissing, $BIOG->field('BIOG_NBR')); } $t->purge(); # Tell XML::Twig to dispo of the rest of the tree we + don't care about return 1; } # # Check database for ID # sub checkBiog { my ($biog) = @_; if ( !%BIOG ) { my %where = ( BIOG_NBR => { -between => [ $fileStartID, $fileEndID ] }, ); my($stmt, @bind) = $sql->select('BIOG', '"BIOG_NBR"', \%where) +; if (!$sth) { $sth = $dbh->prepare($stmt); } my $result = $sth->execute(@bind); while(my $data = $sth->fetchrow_hashref()) { $BIOG{$data->{BIOG_NBR}} = 1; } } if(defined($BIOG{$biog})) { return 1; } else { return 0; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Twig loves to eat my memory
by almut (Canon) on Jul 22, 2010 at 20:02 UTC | |
by carcus88 (Acolyte) on Jul 22, 2010 at 20:11 UTC | |
by almut (Canon) on Jul 22, 2010 at 20:17 UTC | |
by carcus88 (Acolyte) on Jul 22, 2010 at 21:05 UTC | |
by almut (Canon) on Jul 22, 2010 at 21:20 UTC | |
| |
|
Re: XML::Twig loves to eat my memory
by intel (Beadle) on Jul 22, 2010 at 19:27 UTC | |
by carcus88 (Acolyte) on Jul 22, 2010 at 20:06 UTC | |
|
Re: XML::Twig loves to eat my memory
by AndyZaft (Hermit) on Jul 22, 2010 at 20:17 UTC | |
by AndyZaft (Hermit) on Jul 22, 2010 at 20:30 UTC | |
by carcus88 (Acolyte) on Jul 22, 2010 at 21:24 UTC | |
by ikegami (Patriarch) on Jul 22, 2010 at 22:42 UTC | |
by carcus88 (Acolyte) on Jul 23, 2010 at 04:45 UTC | |
| |
by AndyZaft (Hermit) on Jul 22, 2010 at 21:54 UTC | |
by carcus88 (Acolyte) on Jul 23, 2010 at 04:38 UTC | |
|
Re: XML::Twig loves to eat my memory
by ahmad (Hermit) on Jul 22, 2010 at 22:09 UTC | |
|
Re: XML::Twig loves to eat my memory
by mirod (Canon) on Jul 26, 2010 at 09:50 UTC | |
by carcus88 (Acolyte) on Jul 26, 2010 at 14:48 UTC |