Stanislav Nedelchev wrote:
Hi to all ,
I'm trying to make one script that analyzes some data from a file.
One guy from internet help me a lot .
Here is how data is organized .
Every row starts with skill_begin and it ends with skill_end.
between those 2 words there is field_name = some_data and etc.
Some rows has more field that other and sometimes there is no whitespace
between field_name=some_data. I want remove white spaces and tabs one or
more with only one tab and write it to file . Second this is most
difficult i want to find unique field and create SLQ create and insert
based on this data. But anyway we made it some how but we are still
working on it. I will be very happy if somebody can improve the script
or give me some advice how to do in in other way .
Here you can find example data.
http://rapidshare.com/files/21298750/skilldata.rar.html
and here is the script i was try to make some remarks for some new in
Perl like I'm .
if ( ! open OUT, ">outfile.txt") { die "Cannot create outfile.txt: $!";}
if ( ! open OUT1, ">sqlstmt.txt") { die "Cannot create sqlstmt.txt: $!";}
if ( ! open IN, "skilldata.txt") { die "Cannot open skilldata.txt: $!";}
$var = "varchar(45)";
%hash = {};
while(<IN>)
{
s/\/\*.*\*\//\t/; #Remove Remarks
s/(\S+)\=(\S+)/$1 \= $2/g; #Add Whitespace betwee = and
data
for($i=1;$i<=$#a;$i=$i+3) { #Loop for generating Create
statesment
if(!$hash{$a[$i]}) { #filing hash keys from array key =
"a"
if($a[$i]!~/skill_end/){
$hash{$a[$i]} = "a";
$create = $create."," . "\n" .$a[$i]." ".$var;
}
}
}
$create =~s/^,//; #Remove , in the begining.
$stmt = "create table datafields($create)";
for($j=3;$j<=$#a;$j=$j+3){ #Loop for Generate insert
statesment
$ins = $ins.","." "."\"".$a[$j]."\""; #taking 3 position value
because data = value
}
$ins=~s/^,//;
$sql = "insert into datafields values($ins)";
if($sql =~/values\(\)/){} else { #if diffrent that initial var
then wtite to file
print OUT1 "$sql\n";
$ins = "";
}
@a = split /\s+/, $_; #split input data on pieces
based on whitespace.
foreach $a(@a){ #Loop
@b = split/= /, $a; #split data on parts by =
#$b[0]=~s/ $//; #Removes empty space on the
beginning
#$b[0]=~s/^ //; #Removes empty space on the end
if($b[0]!~/_begin|_end|\/\*|\*\//) {
print OUT "$b[0]\t"; #Write Data without skill_begin and
skill_edn and add tabs delimeter.
}
}
print OUT "\n"; #Put new line on every row
}
print OUT1"\n\n$stmt\n\n";
Hi Stanislav.
What game would this be then? I'd like to know :)
I've rewritten your program to do what I think you want. I couldn't work out
exactly what you wanted sent to outfile.txt so I've written just the SQL.
All the data is held in the @data array so you can derive pretty much any
output from it.
I added a field list to your insert statement as not all fields are present
in each record so the list of values needs a corresponding list of column
names.
You still have a problem though because, for instance, the 'effect' field of
s_q_giants_scroll_1 is 881 characters long and won't fit into your
45-character varchar database fields.
I hope this helps.
Rob
use strict;
use warnings;
open OUT, '>sqlstmt.txt' or die "Cannot create sqlstmt.txt: $!";
open IN, 'skilldata.txt' or die "Cannot open skilldata.txt: $!";
my $var = "varchar(45)";
my %columns;
my @data;
while (<IN>) {
chomp;
s|/\*.*?\*/||g;
my %record;
while (/(\S+)\s*=\s*(\S+)/g) {
$record{$1} = $2;
}
push @data, \%record;
}
foreach my $record (@data) {
$columns{$_}++ foreach keys %$record;
}
my $sql = sprintf "create table datacolumns(\n%s)",
join ",\n", map " $_ $var", keys %columns;
print OUT $sql, "\n\n";
foreach my $record (@data) {
$sql = sprintf "insert into datacolumns (\n%s)\nvalues(\n%s)",
join(",\n", map qq( $_), keys %$record),
join(",\n", map qq( "$_"), values %$record);
print OUT $sql, "\n\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/