Roime bin Puniran <[EMAIL PROTECTED]> wrote:

: I have a script that read some text file in the directory. All
: the data inside the text file then would be extracted into my
: sql, and i used foreach loop to read the text file, then doing
: some loop while data are sorted. Here is my code.

    Your description indicates this order of actions:

    1. Retrieve files in directory,
    2. Open data file,
    3. Retrieve data,
    4. Sort data,
    5. Insert information into DB,
    6. Repeat for each file in directory.


    Had your code worked, this is the order your code is written
in:

    1. Insert information into DB,
    2. Retrieve file names,
    2. Open data file,
    3. Set values for insert.


    As written here's your code:

    1. Insert no information into DB,
    2. Do not retrieve files,
    3. Open no data file (see step 2),
    4. Set and discard values for insert.

    Perl doesn't "know" that you wrote the code out of order. You
have to write it as you want it to be performed. First write the
code which retrieves the file names in the directory.

    Test that code and debug it. Once that is working write code
for the next step, test it, and debug. Then move on to the next
step.


: ================================
: #!/usr/bin/perl


    You skipped these modules. Start every script with them. Write
part of the script and test it. Then debug and write some more
code. Debug it and write some more code. Do not write the whole
thing and then debug it.

use strict;
use warnings;


: use DBI;
: use IO::Socket;

    This module is not used in this script.


: my $path = "/home/roime/flow/";
: my @folder;

    Declare this when you define it. It will help you avoid
using it before defining it as you have done here.


: my $file = ".flow";

    This variable is not used in this script.


: my $dbh = DBI->connect('dbi:mysql:ayam','root','');
:
:   #Prepare the insert SQL
:   my $rec = $dbh->prepare("INSERT INTO t_flows(ipSrc,
: ipDst, pktSent, bytesSent, startTime, endTime, srcPort,
: dstPort, tcpFlags, proto,     tos) VALUES ('$value1',
: '$value2', '$value3', '$value4', '$value5', '$value6',
: '$value7', '$value8', '$value9', '$value10',  '$value11')");
: $rec->execute;

   $value1 through $value11 have not been set to anything yet.
You can't insert their values until *after* you set their values.
When you find yourself numbering variables consecutively, think
"array".

    Use white space when writing SQL. This is easier to read and
change.

my $dbh = DBI->connect(
                'dbi:mysql:ayam',
                'root',
                ''
        );

my $record = $dbh->prepare( qq~
    INSERT
        INTO
            t_flows(
                ipSrc,
                ipDst,
                pktSent,
                bytesSent,
                startTime,
                endTime,
                srcPort,
                dstPort,
                tcpFlags,
                proto,
                tos
            )
        VALUES (
            '$value1',
            '$value2',
            '$value3',
            '$value4',
            '$value5',
            '$value6',
            '$value7',
            '$value8',
            '$value9',
            '$value10',
            '$value11'
        )~
);

    Or, more likely:

my $dbh = DBI->connect(
            'dbi:mysql:ayam',
            'root',
            '',
            {
                PrintError => 0,
                RaiseError => 1,
                AutoCommit => 1,
            },
        );

my $sth = $dbh->prepare( q~
    INSERT
        INTO
            t_flows(
                ipSrc,
                ipDst,
                pktSent,
                bytesSent,
                startTime,
                endTime,
                srcPort,
                dstPort,
                tcpFlags,
                proto,
                tos
            )
        VALUES ( ?, ?, ?,
                 ?, ?, ?,
                 ?, ?, ?,
                 ?, ?
        )~
);

    You need to read the documentation for DBI.pm. There are
plenty of examples and pointers to more resources.


: foreach my $file (@folder)
: {

    @folder has nothing in it. This loop runs 0 times. What do
you want @folder to hold? You need to write the code to put
something in there.


:   my $full_path = $path.$file;
:   open(FILE, $full_path)||die("Could not read file !");

    Use the built-in error variable ($!) in error messages. I
have an editor macro for file openings. This is one of the most
common idioms I use.


my $file = "$path$file";
open FH, $file or die qq(Cannot open "$file": $!);
 #...
close FH;



:   my $file_contents = <FILE>;
:     close(FILE);

    $file_contents is local to this loop. If the loop did run,
$file_contents would be undefined after the loop ends.


: }
:
:
:
: my $counter = 0;
: @file_array = split(/ /, file_contents);

    $file_contents and @file_array are out of scope here. They
must be declared in this scope to avoid errors. @file_array is
obviously an array. Why not call it @files?

:
: foreach (@file_array)
: {
:   my $value1 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value2 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value3 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value4 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value6 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value7 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value8 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value9 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value10 = @file_array[$counter];
:   $counter = $counter + 1;
:   my $value11 = @file_array[$counter];
:   $counter = $counter + 1;
:
: }

    This loop doesn't do anything. All the variables are scoped to
this code block and will not be defined outside the foreach loop.
Even if they had been defined, the program ends here and their
values are useless. @file_array[$counter] is better written as
$file_array[$counter].

    This loop could be written as:

{
    my( $value1, $value2,  $value3, $value4,
        $value5, $value6,  $value7, $value8,
        $value9, $value10, $value11, ) = @file_array;
}
my $counter = 11;


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to