Professional Web Applications Themes

How best to grab SQL table names? - PERL Beginners

I've got a script that goes through SQL files and returns an array of table names. To find table names I use: while ($_ = <FILE>) { if ($_ =~ m/CREATE.*TABLE/) { $_ = $'; while (not ($_ =~ m/)//)) { # match last parenthesis $_ .= <FILE>; } my $table_name = $`; $table_name = tr/ \t\n\r//d; return $table_name; } } My Programming Perl book says not to use $' $` and $&. Is there a better way to do this? Thanks in advance, Dan...

  1. #1

    Default How best to grab SQL table names?

    I've got a script that goes through SQL files and returns an array of
    table names. To find table names I use:

    while ($_ = <FILE>) {
    if ($_ =~ m/CREATE.*TABLE/) {
    $_ = $';
    while (not ($_ =~ m/)//)) { # match last parenthesis
    $_ .= <FILE>;
    }
    my $table_name = $`;
    $table_name = tr/ \t\n\r//d;
    return $table_name;
    }
    }

    My Programming Perl book says not to use $' $` and $&. Is there a
    better way to do this?

    Thanks in advance,

    Dan
    Dan Anderson Guest

  2. #2

    Default Re: How best to grab SQL table names?

    oops that should be
    while (not ($_ =~ m/\(//)) { # match the first parenthesis
    and not:
    while (not ($_ =~ m/)//)) { # match last parenthesis

    My apologies,

    Dan
    Dan Anderson Guest

  3. #3

    Default RE: How best to grab SQL table names?


    I think you have an extra '/' in there for an m// operation. Also,
    maybe I'm just looking at this wrong, since I wasn't following the whole
    thread, but wouldn't that be the same as this?

    while( $_ !~ /\(/ ){

    which (purely my opinion) is a little easier to read?

    -----Original Message-----
    From: Dan Anderson [mailto:danmathjunkies.com]
    Sent: Sunday, November 09, 2003 4:39 PM
    To: [email]beginnersperl.org[/email]
    Subject: Re: How best to grab SQL table names?

    oops that should be
    while (not ($_ =~ m/\(//)) { # match the first parenthesis and not:
    while (not ($_ =~ m/)//)) { # match last parenthesis

    My apologies,

    Dan

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email] For additional
    commands, e-mail: [email]beginners-helpperl.org[/email]

    Tim Johnson Guest

  4. #4

    Default Re: How best to grab SQL table names?

    Dan Anderson wrote:
    >
    > I've got a script that goes through SQL files and returns an array of
    > table names. To find table names I use:
    >
    > while ($_ = <FILE>) {
    > if ($_ =~ m/CREATE.*TABLE/) {
    > $_ = $';
    > while (not ($_ =~ m/)//)) { # match last parenthesis
    > $_ .= <FILE>;
    > }
    > my $table_name = $`;
    > $table_name = tr/ \t\n\r//d;
    > return $table_name;
    > }
    > }
    >
    > My Programming Perl book says not to use $' $` and $&. Is there a
    > better way to do this?
    Hi Dan.

    Your code won't compile, so I'm guessing hard at what you're trying
    to do. The best way to present a problem to the group is in plain
    English, not in the form of non-funtional Perl that we have to
    backtrack to understand what you meant.

    I think you're trying to scan an SQL 'CREATE TABLE' statement, and
    grab the name of the table and all of the field specifications.
    Or something like that.

    This isn't hard even without the 'forbidden' $' $` and $& but, even
    though my belief is that people who avoid them are phobic, they
    don't help here at all!

    Explain your problem more clearly, with an example of your data,
    and we'll be able to help.

    Cheers,

    Rob


    Rob Dixon Guest

  5. #5

    Default Re: How best to grab SQL table names?


    On Sunday, Nov 9, 2003, at 16:32 US/Pacific, Dan Anderson wrote:
    [..]
    >
    > while ($_ = <FILE>) {
    > if ($_ =~ m/CREATE.*TABLE/) {
    > $_ = $';
    > while (not ($_ =~ m/)//)) { # match last parenthesis
    > $_ .= <FILE>;
    > }
    > my $table_name = $`;
    > $table_name = tr/ \t\n\r//d;
    > return $table_name;
    > }
    > }
    >
    > My Programming Perl book says not to use $' $` and $&. Is there a
    > better way to do this?
    well a bit of it has to do with how the Perl Regular Expression
    Engine works, the other part has to do with making your code a
    bit more readable, and maintainable. The use of the $' and $`
    as the "perldoc perlvar" pages will tell you imposes a considerable
    performance penalty.

    right, a bit of clean up of the code:

    while (<FILE>) { # it will set the line to $_ by default
    # cut off everything BEFORE our 'pattern'
    if ( s/.*CREATE.*TABLE//)
    {
    chomp; # remove any 'newline tokens'
    my $line = $_ ; # save off what we have
    # read until we have come to the closing paren
    while ( $line !~ m/\)/ ) {
    $_ = <FILE>;
    chomp;
    $line .= $_;
    }
    # we will either have seen that last paren, or it did not exist
    ( my $table_name = $line ) =~ s/.*\)//;
    $table_name = tr/ \t\n\r//d;
    return $table_name;

    } # found opening line
    }# end while loop

    being a bit of a tight translation.

    ciao
    drieux

    ---

    Drieux Guest

Similar Threads

  1. Database table names
    By saver2th in forum Coldfusion Database Access
    Replies: 3
    Last Post: April 17th, 06:28 AM
  2. Microsoft SQL 2000 - table names
    By Eindride in forum Coldfusion Server Administration
    Replies: 1
    Last Post: December 1st, 02:12 PM
  3. querying table names in a database
    By Brian Gallutia in forum Coldfusion - Getting Started
    Replies: 1
    Last Post: April 20th, 04:43 PM
  4. Access table names
    By Marina in forum ASP.NET General
    Replies: 1
    Last Post: August 4th, 04:03 PM
  5. Dataset and table names.....
    By Jim in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: June 30th, 10:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139