in reply to Trying to avoid line noise (brain cramp)
Instead of one big regexp that splits the line you could consume it chunk by chunk, which lets you break the regexp into smaller pieces and give names to the fields.
In any case you will have to deal with the fact that (s and )s have to be escaped so it will look ugly no matter what you try. Is there a Perl 6 RFC that would allow us to change that character?
Update: I have worked on the display part too, and figured that as naming things is good I should go all the way and use a hash to store the 3 parts of the SQL instruction. Plus I am not very familiar with hash slices so it was a good opportunity to use one ;--)
#!/bin/perl -w use strict; my ( @results, %fields ); my $TABLE= 'Photo'; while(<DATA>){ s/\0//g; if ( s/(?<!from )(INSERT INTO $TABLE\s*)//) { my $table= $1; # easy to get +it here my $fields= $1 if( s/(\([^(]+\))//); # the complex +one my $values= $_; # just grab th +e rest push @results, { table => $table, # naming thing +s is good! fields => $fields, values => $values }; } } my @final = sort{ $a->{fields} cmp $b->{fields} } @results; # the so +rt is easier to understand open OUT, ">results.txt" or die $!; foreach my $insert ( @final ) { my $fieldNames = $insert->{fields}; if ( ! exists $fields{ $fieldNames } ) { $fields{ $fieldNames } = 1; print @$insert{qw(table fields values)}; # and n +ow the hash slice } } close OUT; __DATA__ 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
|
|---|