in reply to regular expression to get the tablename.column_name

time awk -F ';' '/^TABLE/ {table=$2} /^COLUMN/ {printf("%s.%s\n",tabl +e,$2) }' * real 0m0.35s user 0m0.29s sys 0m0.05s time perl -ne '/^TABLE;([^;\n]+)/ && ($table = $1) or /^COLUMN;([^;\n] ++)/ && print "$table.$1\n"' * real 0m0.24s user 0m0.21s sys 0m0.03s time perl -nle '/TABLE;(.*)/?($t=$1):/COLUMN;([^;]*)/&&print(qq{$t.$1} +)' * real 0m4.88s user 0m0.34s sys 0m0.05s

Replies are listed 'Best First'.
Re^2: regular expression to get the tablename.column_name
by imp (Priest) on Oct 18, 2006 at 05:16 UTC
    To get a better idea of how each strategy performs you need to observe the results over many invocations. Here is a benchmark of a few strategies:
    use Benchmark qw(cmpthese); use strict; my @data = <DATA>; my $data = join "\n",@data; #open OUT, '>&STDOUT'; open OUT, '>', '/dev/null'; cmpthese(1000, { table_col => sub {open my $fh, '<', \$data; table_col($fh)}, col_table => sub {open my $fh, '<', \$data; col_table($fh)}, multi => sub {open my $fh, '<', \$data; multi($fh)}, regex => sub {open my $fh, '<', \$data; regex($fh)}, }); sub multi { my $fh = shift; my $table; while(<$fh>) { if (/^COLUMN;(.+?);/) { print OUT $table,$1,"\n"; } elsif (/^TABLE;(.+)$/) { $table = $1 . '.'; } } } sub col_table { my $fh = shift; my $table; while(<$fh>) { /^COLUMN;(.+?);/ && print OUT ($table,$1,"\n") or /^TABLE;(.+) +$/ && ($table = $1 . '.') } } sub table_col { my $fh = shift; my $table; while(<$fh>) { /^TABLE;(.+)$/ && ($table = $1 . '.') or /^COLUMN;(.+?);/ && p +rint OUT ($table,$1,"\n") } } sub regex { my $fh = shift; local $/; my $data = <$fh>; my $table; $data =~ s{ ^TABLE;(.+?)\s*\n | ^COLUMN;(.+?);.*\n }{$1 ? (($table=$1),"") : "$table.$2"}mexg; print OUT $data; } __DATA__ TABLE;nokia_sgsn_tot_int_util_month COLUMN;nc_id;integer COLUMN;sgsn_id;varchar(50) not null COLUMN;month_of;integer not null COLUMN;nokia_sgsn_interface_utilisation_busy_hour;utime COLUMN;data_coverage_pc;float COLUMN;tot_measurement_seconds;integer COLUMN;avg_measurement_seconds;integer COLUMN;tot_ifinbroadcastpkts;int8 COLUMN;avg_ifinbroadcastpkts;int8 COLUMN;min_ifinbroadcastpkts;int8 COLUMN;max_ifinbroadcastpkts;int8 COLUMN;nsiubh_ifinbroadcastpkts;int8 COLUMN;tot_ifindiscards;int8 COLUMN;avg_ifindiscards;int8 COLUMN;min_ifindiscards;int8 COLUMN;max_ifindiscards;int8 COLUMN;nsiubh_ifindiscards;int8 COLUMN;tot_ifinerrors;int8 COLUMN;avg_ifinerrors;int8 COLUMN;min_ifinerrors;int8 COLUMN;max_ifinerrors;int8 COLUMN;nsiubh_ifinerrors;int8 COLUMN;tot_ifinmulticastpkts;int8 COLUMN;avg_ifinmulticastpkts;int8 COLUMN;min_ifinmulticastpkts;int8 COLUMN;max_ifinmulticastpkts;int8 COLUMN;nsiubh_ifinmulticastpkts;int8 COLUMN;tot_ifinnucastpkts;int8 COLUMN;avg_ifinnucastpkts;int8
    Results:
    Rate col_table multi table_col regex col_table 565/s -- -10% -14% -36% multi 625/s 11% -- -5% -29% table_col 658/s 16% 5% -- -26% regex 885/s 57% 42% 35% --
    With a different set of test data you will of course get different results.

    I was expecting the col_table and multi strategies to do better than table_col, but they consistently perform worse for me.

Re^2: regular expression to get the tablename.column_name
by ikegami (Patriarch) on Oct 18, 2006 at 04:16 UTC
    Do you want fast or short? Those are conflicting goals. Adding ^ makes a big diff.
      Obviously.. both