Tab delimited to prolog clauses
diotalevi
created: 2006-01-20 19:18:53
I had lots of tab delimited data and wanted to run prolog queries so I wrote this program to reformat everything as a series of prolog clauses.
#!/usr/bin/perl
my $name = lcfirst shift
    or die "Usage: $0 clause-name\n";
@ARGV = ();

while (<>) {
    if ( $. == 1 ) {
        s/\t/, /g;
        print "% ", lc;
        next;
    }

    chomp;
    s/'/''/g;
    s{
      # Find a place that is preceded by the beginning of the string or a tab
      (?:
         (?<=\t)
         |
         (?<=^)
      )
      
      # Only non-numbers are to be escaped.
      (?!-?(?:\d+(?:\.\d+)?|0?\.\d+)(?:\t|$))

      # Capture everything up to the next delimiter or end of string.
      ([^\t]*)

     }
     {'$1'}gx;

    tr/\t/,/;
    print "$name($_).\n";
}
Re: Tab delimited to prolog clauses
created: 2006-01-23 13:25:02

On behalf of the three people on Perlmonks who know what that means (or cares): ++

:)

Cheers,
Ovid

New address of my CGI Course.

perlmonks.org content © perlmonks.org and diotalevi, Ovid

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03