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 o +r a tab (?: (?<=\t) | (?<=^) ) # Only non-numbers are to be escaped. # Only non-numbers are to be escaped. (?! # Match a number -? (?: \d+ (?: \.\d+ )? | 0? \.\d+ ) # Check for the next delimiter or end of line (?: \t | $ ) ) # Capture everything up to the next delimiter or end of string. ([^\t]*) } {'$1'}gx; tr/\t/,/; print "$name($_).\n"; }

Replies are listed 'Best First'.
Re: Tab delimited to prolog clauses
by Ovid (Cardinal) on Jan 23, 2006 at 18:25 UTC

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

    :)

    Cheers,
    Ovid

    New address of my CGI Course.