I have a sample of SQL from the MS-SQL profiler. I need to identify all unique "INSERT INTO Photo..." statements. My thought was to read the file, select lines that have the approriate sql and break them into a three part array with the middle element containing the field names. Then I push a reference to that array onto another array. Next, I sort that array on the field names and print unique SQL inserts (tracking them by stuffing the first instance of each combination of field names in a hash). Here's the code:
Here's some sample input (heavily edited because the SQL statements are HUGE):use warnings; use strict; my ( @results, %fields ); while(<>){ s/\0//g; if (/(?<!from )INSERT INTO Photo/) { my @s = ( /([^(]+)(\([^)]+\))(.*)/ ); push @results, \@s; } } my @final = sort{ $a->[1] cmp $b->[1] } @results; open OUT, ">results.txt" or die $!; foreach( @final ) { my $fieldNames = $_->[1]; if ( ! exists $fields{ $fieldNames } ) { $fields{ $fieldNames } = 1; print OUT @{ $_ } } } close OUT;
My code works fine, but it's a great example of the "line noise" problem. Unfortuanately, I really can't work my mind around the problem of cleaning the code to the point where it's comprehensible to someone with a low level of Perl knowledge. I could break the regex out with the /x modifier, but that's only a partial solution. Any advice would be appreciated.exec new_int_id 'documents', 'did' go INSERT documents ( did, DocID, Template ) VALUES ( 3093, '000000000000 +000000003093', 'Photo' ) go SELECT * FROM documents WHERE did=3093 go INSERT INTO Photo (AccessLevelID, ANRNumber, ColorID, ContactSheetID) +VALUES (1, '225058', 1, 4) go exec new_int_id 'Photo', 'PhotoID' go exec new_str_id 'documents', 'DocId' go INSERT INTO Photo (QualityID, Remark, UniqueID, VolNum) VALUES (1, 'St +. Nicolaasfeest -v. Moorsel', '211357', '1') go
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
In reply to Trying to avoid line noise (brain cramp) by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |