-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.pl
executable file
·63 lines (53 loc) · 1.4 KB
/
schema.pl
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
#!/usr/bin/perl -w
########################################
#
# Create ArcGIS schema.ini file from csv.
#
# Automatically create ArcGIS schema.ini files from the header of a CSV[";"] file.
# The first column is considered to be the grid (Text) and everything else the count
# of pieces (Double).
#
# Author: Arjuna Del Toso (http://arjuna.deltoso.net)
#
# Example:
# $ head -n 1 example.csv
# grid;stones;bones;coins
# $ ./schema.pl example.csv
# [example.csv]
# Col1=grid Text
# Col2=stones Double
# Col3=bones Double
# Col4=coins Double
#
########################################
use strict;
use warnings;
use Text::CSV;
# WINDOWS
# cat -v file_grezzo.csv
# tr -d '\r' < file_grezzo.csv > file_grezzo_mac.csv
my $csv = Text::CSV->new({ sep_char => ';', binary => 1 });
my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";
open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$file' $!\n";
print '['.$file.']', "\n";
my $linenum = 0;
while (my $line = <$data>) {
chomp $line;
$linenum = $linenum + 1;
if ($linenum eq 1) {
if ($csv->parse($line)) {
my @cols = $csv->fields();
print 'Col1='.$cols[0].' Text', "\n";
my $col_num = 2;
shift @cols;
foreach my $col (@cols) {
if ($col) {
print 'Col'.$col_num.'='.$col.' Double', "\n";
$col_num = $col_num + 1;
}
}
} else {
warn "Line could not be parsed: $line\n";
}
}
}