Gregory Bernard wrote:

> Here is a little script I am working on, first part executes without any
> problem... The database is deleted from MySQL but the second part of the
> script which is supposed to take a file on my disk and insert it into my db
> does not work...
> 
> 
> Second part (after for (split(//
> 
> #!/usr/bin/perl


use strict;
use warnings;


> $in = "/Users/thomas/Sites/update/base.html";
> $enregistre = 0;
> $tete=1;
> 
> open(FILE,"<$in") || die "Erreur d'ouverture : $!";
> system("/usr/local/bin/mysql -u greg -pEud -e \"DELETE FROM ferrari\"
> ferrari");


BTW: Why not use the standardized, quick way via DBI ?!


> #################
> ## Second part ##
> #################
> 
> for (split(//,<FILE>)) {
>         if (/^<\/TR>$/) {
>         $enregistre = 0;
>         $script = "/usr/local/bin/mysql -u greg -pCt -e \"INSERT INTO
> ferrari VALUES \('" . @data[0] .
> "','@data[1]','@data[2]','@data[3]','@data[4]','@data[5]','@data[6]','@data[
> 7]','@data[8]','@data[9]','@data[10]',NULL,'@data[11]'\)\" ferrari";


It's important to write $data[0] instead of @data[0].
The first one stands for one array element,
the second for an array consisting of one element
what in scalar context is converted to the number of elements.

But it's surely better to avoid the boring, errorprone doubled code.
Insted of '" . $data[0] . "','@ . $data[1] . "','" . ...
just use the short and easier to read
join "," => map("'$_'", @data[0..10]), 'NULL', $data[11];


>         if ($tete eq "0") {system "$script\n";} else {$tete = 0;}
>     }
>     if ($enregistre eq "1") {
>         s#^<TD>(.*)</TD>$#$1#;
>         s#'#\\'#g;
>         s#^([0-9]{2})/([0-9]{2})/([0-9]{4}).*$#$3-$2-$1#;


Or shorter
s#^(\d\d)/(\d\d)/(\d\d\d\d)#$3-$2-$1#;


>         s/&#([0-9]*);/pack("C",$1)/eg;
>         @data[$champ]="$_";


Here also $data[$champ]=$_;
should be written.

Please read also
perldoc -q 'What\'s wrong with always quoting "$vars"'


>         $champ++;
>     }


BTW, you want to push some data to an array.
There's an easier way:

push @data, $_;

(instead of $data[$champ] = $_; $champ++)


>     if (/^<TR>$/) {
>         $enregistre = 1;
>         $champ = 0;
>     }
> }
> system("date +%d/%m/%Y > /Users/thomas/Sites/modif.inc");


Please read also the functions
perldoc -f time
perldoc -f localtime


Greetings,
Janek


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to