Re: Very Sluggish Code

2012-07-11 Thread Jenda Krynicky
From: GlenM 
> # Read them into an array
> my @files_in_dir = grep { /xml/ } readdir(DIR);

You want all files with lowercase "xml" anywhere the the name? 
Really? I think you want 

my @files_in_dir = grep { /\.xml$/i } readdir(DIR);

instead. That is files with extension .xml.

> closedir DIR;
> 
> # connect to database and create parser object
> my $dbh = DBI->connect ("DBI:mysql:can_us_ratecenters",
>"", "xx",
>{ RaiseError => 1, PrintError => 0});
> 
> # clear the database - new DIDs coming in
> $dbh->do ('TRUNCATE TABLE rc_city_town');
> 
> #Now the fun begins - read each file and put it in the database
> foreach my $f (@files_in_dir) {
> open IN, "<$f";
> 
> my $xp = XML::XPath->new (filename => "./$f");
> my $nodelist = $xp->find ("//row");
> foreach my $row ($nodelist->get_nodelist ())
>{
>$dbh->do (
>"INSERT IGNORE INTO rc_city_town (state_prov, city_town, 
> did_number) VALUES (?,?,?)",
>undef,
>$row->find ("state")->string_value (),
>$row->find ("ratecenter")->string_value (),
>$row->find ("number")->string_value (),
>);

Do split this into two calls 

  my $sth = $dbh->prepare("INSERT IGNORE INTO rc_city_town 
(state_prov, city_town, did_number) VALUES (?,?,?)");

just below the connection to the database and

  $sth->execute(
$row->find ("state")->string_value (),
$row->find ("ratecenter")->string_value (),
$row->find ("number")->string_value (),
  );

in the foreach loop.

Then you most probably want to turn the autocommit off

  my $dbh = DBI->connect ("DBI:mysql:can_us_ratecenters",
   "", "xx",
   { RaiseError => 1, PrintError => 0, 
AutoCommit = 0});

and then call

  $dbh->commit or die $dbh->errstr;

once every (say) 1000 rows and at the end of each file.

Jenda
= je...@krynicky.cz === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




regular error

2012-07-11 Thread xiyoulaoyuanjia
*hi all:*
*   i have an error in below * regular

$a="abc[0]";
$b="bc[0]";
$a=~/$b/;
 -
how can i do in this way?
thanks !
-- 
继续上路。。
you can follow me at twitter
@xiyoulaoyuanjia


RE: regular error

2012-07-11 Thread Jack Maney
I don't understand what you're trying to say or do. Exactly what error message 
are you getting?

-Original Message-
From: xiyoulaoyuanjia [mailto:xiyoulaoyuan...@gmail.com] 
Sent: Wednesday, July 11, 2012 10:54 PM
To: beginners@perl.org
Subject: regular error

*hi all:*
*   i have an error in below * regular

$a="abc[0]";
$b="bc[0]";
$a=~/$b/;
 -
how can i do in this way?
thanks !
-- 
继续上路。。
you can follow me at twitter
@xiyoulaoyuanjia


Re: regular error

2012-07-11 Thread xiyoulaoyuanjia
i am very sorry ! i am intend to   parse  variable $a by the  regular
variable $b . in  below way!

$a="abc[0]";
$b="bc[0]";
$a=~/$b/;
 -
but it can not parse right ! just because the "[" symbol。 i kow
$a=~/abc\[0]/ it can get right!
but when regular is a varible . I do not know how to solve it?
Sorry if this isn't clear let me know if there are any questions.
thanks in advance!


2012/7/12 Jack Maney 

> I don't understand what you're trying to say or do. Exactly what error
> message are you getting?
>
> -Original Message-
> From: xiyoulaoyuanjia [mailto:xiyoulaoyuan...@gmail.com]
> Sent: Wednesday, July 11, 2012 10:54 PM
> To: beginners@perl.org
> Subject: regular error
>
> *hi all:*
> *   i have an error in below * regular
> 
> $a="abc[0]";
> $b="bc[0]";
> $a=~/$b/;
>  -
> how can i do in this way?
> thanks !
> --
> 继续上路。。
> you can follow me at twitter
> @xiyoulaoyuanjia
>



-- 
继续上路。。
you can follow me at twitter
@xiyoulaoyuanjia


RE: regular error

2012-07-11 Thread Jack Maney
I’m still mostly uncertain what you’re trying to do.  However, looking at the 
three lines of code that you have below, you’re correct in that the brackets 
will make $a=~/$b/ return undef (by the way, the third line is a no-op, since 
you aren’t doing anything with any potential matches…).  What you need is the 
quotemeta function (http://perldoc.perl.org/functions/quotemeta.html):

$a=”abc[0]”;
$b=”bc[0]”;
$c=quotemeta($b); # $c eq ‘bc\[0\]’;
If($a=~/$c/){print “match\n”;} # results in “match” being printed.

From: xiyoulaoyuanjia [mailto:xiyoulaoyuan...@gmail.com]
Sent: Wednesday, July 11, 2012 11:46 PM
To: Jack Maney
Cc: beginners@perl.org
Subject: Re: regular error

i am very sorry ! i am intend to   parse  variable $a by the  regular variable 
$b . in  below way!

$a="abc[0]";
$b="bc[0]";
$a=~/$b/;
 -
but it can not parse right ! just because the "[" symbol。 i kow   $a=~/abc\[0]/ 
it can get right!
but when regular is a varible . I do not know how to solve it?
Sorry if this isn't clear let me know if there are any questions.
thanks in advance!


2012/7/12 Jack Maney mailto:jma...@adknowledge.com>>
I don't understand what you're trying to say or do. Exactly what error message 
are you getting?

-Original Message-
From: xiyoulaoyuanjia 
[mailto:xiyoulaoyuan...@gmail.com]
Sent: Wednesday, July 11, 2012 10:54 PM
To: beginners@perl.org
Subject: regular error

*hi all:*
*   i have an error in below * regular

$a="abc[0]";
$b="bc[0]";
$a=~/$b/;
 -
how can i do in this way?
thanks !
--
继续上路。。
you can follow me at twitter
@xiyoulaoyuanjia



--
继续上路。。
you can follow me at twitter
@xiyoulaoyuanjia



Re: regular error

2012-07-11 Thread timothy adigun
Hi xiyoulaoyuanjia,
  Check my comments below.

On 7/12/12, xiyoulaoyuanjia  wrote:
> i am very sorry ! i am intend to   parse  variable $a by the  regular
> variable $b . in  below way!

 the below script will not give any output.
> 
> $a="abc[0]";
> $b="bc[0]";
> $a=~/$b/;
>  -
even if you decide to use:

print $a if $a=~m/$b/;   #no output

> but it can not parse right ! just because the "[" symbol。 i kow
> $a=~/abc\[0]/ it can get right!
> but when regular is a varible . I do not know how to solve it?

  to get what you wanted you will do might do this:

   $a="abc[0]";
   $b="bc[0]";
   print $a if $a=~m/\Q$b\E/;   # prints abc[0]

  NOTE:
   Please take note of the \Q and \E in the REs.
   Don't use variable $a and $b, since these are default for sort
function. You will observe that without declaring these variables you
have your intended output (I suppose).
 Others will say use strict and warnings pragma in your scripts,
however few the lines of codes.


> Sorry if this isn't clear let me know if there are any questions.
> thanks in advance!
>
>
> 2012/7/12 Jack Maney 
>
>> I don't understand what you're trying to say or do. Exactly what error
>> message are you getting?
>>
>> -Original Message-
>> From: xiyoulaoyuanjia [mailto:xiyoulaoyuan...@gmail.com]
>> Sent: Wednesday, July 11, 2012 10:54 PM
>> To: beginners@perl.org
>> Subject: regular error
>>
>> *hi all:*
>> *   i have an error in below * regular
>> 
>> $a="abc[0]";
>> $b="bc[0]";
>> $a=~/$b/;
>>  -
>> how can i do in this way?
>> thanks !
>> --
>> 继续上路。。
>> you can follow me at twitter
>> @xiyoulaoyuanjia
>>
>
>
>
> --
> 继续上路。。
> you can follow me at twitter
> @xiyoulaoyuanjia
>


-- 
Tim

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/