On Wed, Sep 12, 2001 at 04:25:47PM -0500, Jay Strauss wrote:
> I have a flag in my pgm that indicates to look for "create table" statements
> OR anything else. The end result I'd like is that my scalar ($rebuilt) will
> equal all the create table statements, or everything else.
> a couple of regexs, one to null out anything not a "create table" statement,
> one to null out all the "create table" statements.
>
> But alas, I don't know how.
>
> I run my pgm by:
>
> # cat sample.data | tmp
>
> Jay
>
> #!/usr/bin/perl
>
> undef $/;
> my $file = <>;
> $file =~ s/\bREM\s+//g;
> $file =~ s/\n//g;
>
> my $flag = "t";
> my $rebuilt = '';
>
> foreach my $line (split(/;/,$file)) {
>
> $create = ($line =~ /^CREATE\s+TABLE/) ? 1 : 0;
>
> if ($flag eq "t") {
> $line = '' if ! $create;
> }
> else {
> $line = '' if $create;
> }
>
> $rebuilt .= $line.";" if ($line);
> }
this loop can be :
$file =~ s/(;?\s*)(CREATE\s+TABLE[^;]+)(.*)/$1$3/g unless $t;
$file =~ s/(;?\s*)(CREATE\s+TABLE[^;]+)(.*)/$2/g if $t;
which should simplify to (and should run faster)
$file =~ s/\b(CREATE\s+TABLE[^;]+)//g unless $t;
$file = join('', $file =~ m/\b(CREATE\s+TABLE[^;]+)/g) if $t;
though I don't use \b often enough to be sure of its operation in every
sitation.
(should be close... (;?\s*) is a weak attempt to avoid CREATE TABLE inside
strings... (you used \b instead)... if you did a:
$file=join(";\n", split(/;/, $file)) . ";\n";
you could use s///m for multiline, and then use ^ and $ for "start of line"
from a \n perspective.)
> foreach my $line (split(/;/,$rebuilt)) {
> print substr($line,0,80)."\n";
> }
this hard coded 0,80 is suspect, but if your input data is assured it's
not a problem (which doesn't appear to be the case). You'll also lose
your ; characters.
--
Ted Deppner
http://www.psyber.com/~ted/