Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Location
    London
    Posts
    76

    Unanswered: Using variable for match expression

    Hi there,

    I would be grateful for some help on the following:

    I have to query the database and create a file with the order numbers, which I have done. I then need to read the contents of a number of different txt files in a directory and match the order number from my list in these txt files. If there is a match I need to move the file. However, my script does not seem to work when I try to parse the order number from the file as the match expression, it works when I type a known order number in the match expression.

    My scripts is below, any help would be appreciated...thanks

    use DBI;
    use MIME::Lite;


    # Connect to the Database
    my $db = DBI->connect("dbi:OracleATABASE", "username", "password", { AutoCommit => 0 } );

    check connect to DB
    $db or (print <<EOD
    Error: Can't connect to database
    EOD
    and die $DBI::errstr);

    print "connected.\n";

    # SQL Query to extract data
    my $O4K_Clients = qq{select distinct mo.orderno
    from monitororder mo, deliveryaddress da
    where mo.DELIVERYADDRESSID = da.DELIVERYADDRESSID
    and mo.ORDERNAME like '%/26%'
    };

    my $sth = $db->prepare ($O4K_Clients);
    $sth->execute();

    my $log = "\\\\pgitdept06\\c\$\\data\\test\\O4K_Portal_Names .txt";

    open OUTPUT, "> $log";

    while (($row) = $sth->fetchrow_array) {

    chomp $row;

    print OUTPUT "$row\n";
    }

    close OUTPUT;

    $sth->finish();

    $db->disconnect();

    $directory = "\\\\pgitdept06\\c\$\\data\\test\\";

    open(DAT, $log) || die("Could not open file!");
    @raw_data=<DAT>;
    close(DAT);
    foreach $O4K_Portal_Clients(@raw_data)

    {
    print "$O4K_Portal_Clients\n";

    opendir(DIR1, "$directory");
    @files1 = readdir(DIR1);
    $arrlength1 = @files1;
    closedir(DIR1);

    my $xmlcount = 0;

    for ($x = 0; $x <= $arrlength1; $x++)
    {
    $path = "$directory@files1[$x]";

    print "$path\n\n";

    if ( @files1[$x] =~ m/.xml/ )
    {
    open(DAT, $path) || die("Could not open file!");
    @xml_file=<DAT>;


    foreach $line (@xml_file)
    {
    if ($line=~ m/($O4K_Portal_Clients)/)
    {
    close(DAT);

    print "@files1[$x] is A O4K xml\n\n";
    move("$directory@files1[$x]","$directorymove@files1[$x]") or die ("Error in moving XML's");
    }
    else
    {
    print "@files1[$x] is NOT AN O4K xml\n\n";
    }
    }
    }
    $xmlcount++;

    }
    }

  2. #2
    Join Date
    Feb 2006
    Posts
    56
    maybe you should be using rename() instead of move(). There is no native move() function in perl unless it's new to perl 6 and I am unaware of it.

  3. #3
    Join Date
    Feb 2004
    Location
    London
    Posts
    76
    The move function works, I have used it in many scripts...but this is not the issue. I just cant seem to parse the variable as the match expression, however if I type some text that I know exists in the file then it works ok.

  4. #4
    Join Date
    Feb 2006
    Posts
    56
    what version of perl are you using? There is no move() function and the modules you are using don't import a move() function. There is no way a move() function will work that I am aware of like you have it. Also I am not clear on where you are having the matching problem but maybe it's here:

    if ( @files1[$x] =~ m/.xml/ )

    try using:

    if ( @files1[$x] =~ m/\.xml$/ )

    and @file[$x] should really be written as $file[$x] although the deprecated way you wrote it should still work.

    also $directorymove was never defined and has no value.
    Last edited by KevinADC; 03-10-06 at 19:08.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •