[24504] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6684 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 12 14:05:37 2004

Date: Sat, 12 Jun 2004 11:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 12 Jun 2004     Volume: 10 Number: 6684

Today's topics:
    Re: Contructing a dir. tree (JR)
    Re: Extracting Text (Anno Siegel)
    Re: How to test a variable to see if it is a valid rege <kjetilskotheim@yahoo.com>
    Re: loop in loop takes too much time (Anno Siegel)
    Re: loop in loop takes too much time <jurgenex@hotmail.com>
    Re: loop in loop takes too much time (FMAS)
        Need guidance on SNMP::translateObj (Eric Stanfield)
    Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ };  (A. Farber)
    Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ };  (Anno Siegel)
    Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ };  <tadmc@augustmail.com>
    Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ };  <gnari@simnet.is>
    Re: perl IF DBI::errsrt <javier@t-online.de>
    Re: perl IF DBI::errsrt (Kevin Collins)
    Re: Perl text-handling help (Anno Siegel)
    Re: Taint mode and PERL5LIB (Anno Siegel)
        Values From Multiple Arrays <bill@hotmail.com>
    Re: Values From Multiple Arrays <noreply@gunnar.cc>
    Re: Values From Multiple Arrays (Anno Siegel)
    Re: Values From Multiple Arrays <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 12 Jun 2004 09:17:03 -0700
From: jrolandumuc@yahoo.com (JR)
Subject: Re: Contructing a dir. tree
Message-Id: <b386d54b.0406120817.4374f901@posting.google.com>

>   JR> Rather than lambaste my script, why don't you show us what you
>   JR> have, in the way of a solution?  That would be a far more useful
>   JR> way for you to spend your time.  You are clearly a vastly superior
>   JR> programmer, so put your talents to use.
> 
he did put his talents to use in trying to educate you. why don't you
try to put your talents to use by learning from him how to improve
your
code.

Educate me?  Haven't I said that I know exactly to what he was
referring with his comments?  There is no education necessary.  I was
only trying to help the o/p.  The code given did address the o/p's
question.  Sure, almost all code can be re-written more succinctly-I
don't argue that.  Don't people often just put code out there in
newsgroups, without making it as succinct as possible, to address a
given person's questions as quickly as possible?  Surely, this isn't
the first time.  If the initialization of several variables
unnecessarily is such a bad thing, I'll make sure to email any o/p
about a possible solution in the future, rather than throwing out
there some code for everyone to critique.

BTW-If I were coding a script such as the one wanted by the o/p, for
my purposes, I wouldn't have initialized the variables.  I tried to
explain that I thought it would just make things more clear to the
o/p.  If not, well then, that's too bad, I guess.  I was only trying
to help.


> 
> uri


------------------------------

Date: 12 Jun 2004 10:36:32 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Extracting Text
Message-Id: <caembg$9lp$1@mamenchi.zrz.TU-Berlin.DE>

Jake Gottlieb <jakegottlieb@hotmail.com> wrote in comp.lang.perl.misc:
> Tore Aursand <tore@aursand.no> wrote in message
> news:<pan.2004.06.11.12.33.34.640009@aursand.no>...
> > On Fri, 11 Jun 2004 11:08:59 +0000, Anno Siegel wrote:
> > >>> next unless ( index($_, 'GO:000') >= 0 );
> >  
> > >> index($_, 'GO:000') > -1 or next;
> >  
> > > 1 + index $_, 'GO:000' or next;
> > 
> > While we're at it: How about keeping those two lines (the check and the
> > print) on one line?
> > 
> >   while ( <> ) {
> >       index($_, 'GO:000') and print;
> >   }
> 
> Thank you all. What is the command to save it to a text file. Thanks again.

Read up on it.  perldoc -f open, perldoc -f print.

Anno


------------------------------

Date: Sat, 12 Jun 2004 16:03:46 +0200
From: "Kjetil Skotheim" <kjetilskotheim@yahoo.com>
Subject: Re: How to test a variable to see if it is a valid regexp pattern?
Message-Id: <opr9hiokzztm9jxl@edelweiss.upc.no>

On 11 Jun 2004 05:04:30 -0700, Leif Wessman <leifwessman@hotmail.com>  
wrote:

> How do I test a variable to see if it contains a valid regexp pattern?
> Is it possible?
>
> Leif



Using eval (like those earlier replies suggest) is probably the easyest  
way to go.
But be awary of the security implications!

If the regexp's comes from web input, then they could be able to
take control over your webserver. You might concider using
"joker-chars" instead of a full-blown perl-regexp. Then
you could translate the input into a perl-regexp:

my $regexp=$input;
my $legalchars='a-zA-Z0-9...*?';

$regexp=~ s/[^$legalchars]+//gs;
$regexp =~ s/\*/.*/g;
$regexp =~ s/\?/./g;

$regexp = eval{qr/$regexp/};
print "ERROR: $@\n" if $@;


if($information =~ $regexp){
    #match!!!
    print ...
}


--
KS


------------------------------

Date: 12 Jun 2004 12:34:47 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: loop in loop takes too much time
Message-Id: <caet97$dd5$1@mamenchi.zrz.TU-Berlin.DE>

Iain  <spamtrap@deepsea.force9.co.uk> wrote in comp.lang.perl.misc:
> massion@gmx.de (FMAS) wrote in news:f0b3f4c9.0406112239.7c53b9c4
> @posting.google.com:
> 
> > I am comparing 2 lists of words and want to output the words in list 1
> > which are not available in list 2. Both lists have a different number
> > of entries.
> > 
> > Basically the script below works, but if the lists are large it takes
> > ages. I tested it on 2 lists of approx 15,000 entries each and after
> > 25 min I had processed only 316 entries!
> 
> If you're merely trying to find words in file1 that aren't in file2 I 
> wouldn't bother with arrays at all.

That's what OP said, but the code shows the matches are supposed to
be case-insensitive.

>                                      Hashes are usually much more 
> convenient for this kind of thing. Your other big problem was using a 
> regular expression match construct to see if the two words are 
> identical. Don't do that! If you simply want to test two strings for 
> equality use the 'eq' string comparison operator.

A hash can still be used, but some case normalization must be done:

    my ( $f1, $f2);
    open $f1, $_ or die "Can't read $_: $!" for '/tmp/a';
    open $f2, $_ or die "Can't read $_: $!" for '/tmp/b';
    my %file2words = map { uc() => 1 } <$f2>;
    $file2words{ uc()} or print while <$f1>;

However, the regex solution should not be dismissed out of hand.  The
regex engine can be surprisingly efficient.  You would build a combined
regex out of the second file, as in

    our $re = join '|', map quotemeta, sort { length $b <=> length $a } <$f2>;

The sorting is necessary in case one of the words is the beginning of
another.  We want to try the longer word first.

In this case, for the small set of data I used, the hash method is indeed
faster by a factor of 2, but different data, or a different machine, might
give different results.

The efficiency of the regex is still remarkable, considering that it
checks all alternatives (words from file 2) each time, while the hash
computes one hash key and tries one access.  The reason is the same
as always in low-level optimization in Perl.  You can do a *lot* of
stuff when you can make the Perl core (or an Inline'd or otherwise
loaded routine) do it in one step.

The more looping and logic you can squeeze into one Perl opcode (as
the parlance is), the better, efficiency-wise.  The regex engine
incorporates much looping and logic in one call. It is often still
efficient when a faster algorithm (that needs a few opcodes to
implement) is available.

Anno


------------------------------

Date: Sat, 12 Jun 2004 12:41:59 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: loop in loop takes too much time
Message-Id: <rSCyc.13658$tA6.8955@nwrddc03.gnilink.net>

FMAS wrote:
> I am comparing 2 lists of words and want to output the words in list 1
> which are not available in list 2. [...]

This Question is Asked Frequently, please see "perldoc -q instersection

Some more comments on your code:

Please add
    use strict;
    use warnings;
You should never develop a program without them.

> open(WORDLIST1,'C:\temp\a.txt') || die("cannot open file1!\n");
You may want to add the reason why the open failed: ....open file1 because
$! !\n");
You may want to use the lower precedence operator "or" instead of "||"
because then there is no ambiguity as to evaluation order.

> @list1 = <WORDLIST1>;
There is absolutely no need to read list 1 into memory. You can process this
file easier line by line.

> open(WORDLIST2,'C:\temp\b.txt') || die("cannot open file2!\n");
> @list2 = <WORDLIST2>;

If you would use a hash for list2 instead of an array, then you could take
advantage of the (almost) O(1) lookup time in a hash.

> # loop in loop
>
> foreach $list1 (@list1) {
> foreach $list2 (@list2) {
> chomp $list1;
> chomp $list2;

While in general chomp() is good idea in this specific case it isn't needed.
Normally not such a big issue, but you are even chomping every line many,
many times over. If at all use chomp) _once_ on each array _outside_ of the
loop (chomp() takes a list, too, no need to loop through each element one by
one).

> last if ($list1 =~ m/$list2/i) ; # if match found look for next $list1
Ok, this will work. But do you really need a RegExp? From your verbal
description I would exepect that actually you are looking for equality, not
for an RE match?

> $lastentry = $list2[$#list2]; # in order to print entry only once when
> no match found
> if ($list2 =~ m/$lastentry/i) {
It took me a long time to understand what you are doing with this
$lastentry.
The more canonical way would be to use a binary flag $found, which you set
to "0" just before starting the inner loop, if you find a match then set it
to "1" inside the inner loop, and then do the print() just after ending the
the inner loop iff the flag is true (i.e. "1").

> print  "$list1\n";
> }}}
>
> Any suggestions?
>
> I have heard that it would be much faster to build a search tree, but

That is true, but it would be even faster to use a hash.
What about:

use warnings; use strict;
my %list2;
open(WORDLIST2,'C:\temp\b.txt') or
    die("cannot open 'C:\temp\b.txt' because $! !\n");
while (<WORDLIST2>) {
    $list2{$_}=1; #or any other value
}
open(WORDLIST1,'C:\temp\a.txt') or
    die("cannot open file1 because $! !\n");
while (<WORDLIST1>){
    print unless exists($list2{$_});
}

jue




------------------------------

Date: 12 Jun 2004 10:32:06 -0700
From: massion@gmx.de (FMAS)
Subject: Re: loop in loop takes too much time
Message-Id: <f0b3f4c9.0406120932.5396d783@posting.google.com>

Thanks Lain!

It works amazingly fast (time cut from hours to one second!).
I see that I have a lot of homework to do...

rgds

Francois


------------------------------

Date: 12 Jun 2004 08:50:07 -0700
From: eric@thestanfields.com (Eric Stanfield)
Subject: Need guidance on SNMP::translateObj
Message-Id: <9359bed2.0406120750.20d64759@posting.google.com>

I'm trying to use translateObj to dupliate the return value of
the cmd line snmptranslate utility.  Looking for some guidance
as I've had no luck combing through a hundred posts to see
what I'm doing wrong.  translateObj never seems to translate
between raw numeric OID's and "pretty" OID's such as I can get from
the cmd line.

I've posted the different behavior I'm seeing from both Perl
and the cmd line below for reference.  The point of this is I 
need string output back form Perl for various reasons - getting
numeric OID's spit back isn't going to cut it.


#!/usr/bin/perl

use SNMP;
use SNMP_util;

@xxx = &snmpwalk("public\@10.0.254.109",".1.3.6.1.2.1.25.2.3");

foreach $line (@xxx) {

  print &SNMP::translateObj($line, 1)."\n";

}

Returns: 

 .1.1.1
 .1.1.2
 .1.1.3
 .1.1.4
 .1.1.5
 .1.2.1.3.6.1.2.1.25.2.1.5
 .1.2.2.3.6.1.2.1.25.2.1.4
 .1.2.3.3.6.1.2.1.25.2.1.4
 .1.2.4.3.6.1.2.1.25.2.1.7
 .1.2.5.3.6.1.2.1.25.2.1.3
<etc, etc>

Whereas >snmptranslate -Ofn .1.3.6.1.2.1.25.2.3

Returns:  .iso.org.dod.internet.mgmt.mib-2.system.sysUpTime.6.1.2.1.25.2.3


------------------------------

Date: 12 Jun 2004 05:03:32 -0700
From: Alexander.Farber@t-online.de (A. Farber)
Subject: Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
Message-Id: <c9ccaf83.0406120403.4ccfb2a6@posting.google.com>

"gnari" <gnari@simnet.is> wrote in message news:<caddc0$gs7$1@news.simnet.is>...
> "A. Farber" <Alexander.Farber@t-online.de> wrote in message
> news:c9ccaf83.0406110314.4dfa34a5@posting.google.com...
> >
> > I have a hash where keys and values are file paths.
> > I'm going to use that hash to generate a GNU Makefile.
> > For some parts of the file paths I have shortcuts
> > (like $(TOPDIR)) which I'd like to substitute into the
> > paths. I.e. I'd like to go through the hash keys and
> > perform a substitution on them, like in this test case:
> >
> > perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
> 
> perl -e '%h=(a=>1,b=>2); my @k=keys %h; for (@k) { s/a/b/ }; print @k'

Thanks, but that doesn't help me.

What I have in my program (a converter of some weird build system to 
GNU Makefile) is a hash with files (KEY=destination,VAL=source path):

$prj_exports = {
                 '/mnt/cali5/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
=> '"../data/Clock alert.mid"',
                 '/mnt/cali5/tcf/cmmphonebookstoremesshandler.h' =>
'../inc/cmmphonebookstoremesshandler.h',
                 '/mnt/cali5/tcf/cmmphonebookstoreextinterface.h' =>
'../inc/cmmphonebookstoreextinterface.h'
               };

Since I have the value "/mnt/cali5" in my Makefile variable $(TOPDIR)
already, here is what I'd like to convert the hash above to:

$prj_exports = {
                 '$(TOPDIR)/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
=> '"../data/Clock alert.mid"',
                 '$(TOPDIR)/tcf/cmmphonebookstoremesshandler.h' =>
'../inc/cmmphonebookstoremesshandler.h',
                 '$(TOPDIR)/tcf/cmmphonebookstoreextinterface.h' =>
'../inc/cmmphonebookstoreextinterface.h'
               };


------------------------------

Date: 12 Jun 2004 13:33:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
Message-Id: <caf0mu$f5j$1@mamenchi.zrz.TU-Berlin.DE>

A. Farber <Alexander.Farber@t-online.de> wrote in comp.lang.perl.misc:
> "gnari" <gnari@simnet.is> wrote in message news:<caddc0$gs7$1@news.simnet.is>...
> > "A. Farber" <Alexander.Farber@t-online.de> wrote in message
> > news:c9ccaf83.0406110314.4dfa34a5@posting.google.com...
> > >
> > > I have a hash where keys and values are file paths.
> > > I'm going to use that hash to generate a GNU Makefile.
> > > For some parts of the file paths I have shortcuts
> > > (like $(TOPDIR)) which I'd like to substitute into the
> > > paths. I.e. I'd like to go through the hash keys and
> > > perform a substitution on them, like in this test case:
> > >
> > > perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
> > 
> > perl -e '%h=(a=>1,b=>2); my @k=keys %h; for (@k) { s/a/b/ }; print @k'
> 
> Thanks, but that doesn't help me.
> 
> What I have in my program (a converter of some weird build system to 
> GNU Makefile) is a hash with files (KEY=destination,VAL=source path):
> 
> $prj_Gexports = {
>                  '/mnt/cali5/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
> => '"../data/Clock alert.mid"',
>                  '/mnt/cali5/tcf/cmmphonebookstoremesshandler.h' =>
> '../inc/cmmphonebookstoremesshandler.h',
>                  '/mnt/cali5/tcf/cmmphonebookstoreextinterface.h' =>
> '../inc/cmmphonebookstoreextinterface.h'
>                };
> 
> Since I have the value "/mnt/cali5" in my Makefile variable $(TOPDIR)
> already, here is what I'd like to convert the hash above to:
> 
> $prj_exports = {
>                  '$(TOPDIR)/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
> => '"../data/Clock alert.mid"',
>                  '$(TOPDIR)/tcf/cmmphonebookstoremesshandler.h' =>
> '../inc/cmmphonebookstoremesshandler.h',
>                  '$(TOPDIR)/tcf/cmmphonebookstoreextinterface.h' =>
> '../inc/cmmphonebookstoreextinterface.h'
>                };

So you want do a text replacement to the keys of a hash.  The general
problem with this is the possibility of key clashes.  From what you say
here, clashes can't occur, but that was far from obvious in your
original posting.

There is no way of directly replacing a hash key, as has been discussed
elsewhere in this thread.  The general procedure is to delete the old
key and create the new one (untested):

    $hash{ $newkey} = delete $hash{ $oldkey} if exists $hash{ $oldkey};

If you want to apply an s///-operation to all keys of the hash, as seems
to be your case, this is one way:

    for ( keys %hash ) {
        my $oldkey = $_;
        $hash{ $_} = delete $hash{ $oldkey} if s{^/mnt/cali5} {\$(TOPDIR)};
    }

An existence test is, of course, not needed here.  The test of s///
is there for cleanliness, and, perhaps, efficiency.  Without it the
value would be re-assigned to the unchanged key.

Anno


------------------------------

Date: Sat, 12 Jun 2004 08:43:58 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
Message-Id: <slrnccm24u.mee.tadmc@magna.augustmail.com>

A. Farber <Alexander.Farber@t-online.de> wrote:
> "gnari" <gnari@simnet.is> wrote in message news:<caddc0$gs7$1@news.simnet.is>...
>> "A. Farber" <Alexander.Farber@t-online.de> wrote in message
>> news:c9ccaf83.0406110314.4dfa34a5@posting.google.com...

>> > I'd like to go through the hash keys and
>> > perform a substitution on them,


Which will create a new hash element, you will also need to delete
the old hash element, and you can't do it while foreach-ing over the hash.

It would be much better if you could do the s/// while populating
the hash in the first place, but you haven't shown how you're
building the hash in your real code...


> 
> $prj_exports = {
>                  '/mnt/cali5/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
>=> '"../data/Clock alert.mid"',


You should have written it with shorter line lengths for posting
(or at least disabled word-wrapping).



> here is what I'd like to convert the hash above to:
> 
> $prj_exports = {
>                  '$(TOPDIR)/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
>=> '"../data/Clock alert.mid"',



----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $prj_exports = {
    '/mnt/cali5/epoc32/wins/c/nokia/sounds/Digital/Clock_alert.mid'
    => '"../data/Clock alert.mid"',
    '/mnt/cali5/tcf/cmmphonebookstoremesshandler.h' 
    => '../inc/cmmphonebookstoremesshandler.h',
    '/mnt/cali5/tcf/cmmphonebookstoreextinterface.h' 
    => '../inc/cmmphonebookstoreextinterface.h'
};

my $TOPDIR = quotemeta '/mnt/cali5'; # \Q needed if regex metachars 
                                     # might be in the path, though
                                     # there are none in this example

my @destinations = keys %$prj_exports; # don't modify hash while foreach-ing
foreach my $dest ( @destinations ) {
   (my $newdest = $dest ) =~ s/$TOPDIR/\$\(TOPDIR)/;  # make new key
   $prj_exports->{$newdest} = $prj_exports->{$dest};  # copy value
   delete $prj_exports->{$dest};                      # goodbye old key/value
}

print Dumper $prj_exports;
----------------------------


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Sat, 12 Jun 2004 13:49:04 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
Message-Id: <caf1gv$nbv$1@news.simnet.is>

"A. Farber" <Alexander.Farber@t-online.de> wrote in message
news:c9ccaf83.0406120403.4ccfb2a6@posting.google.com...
> "gnari" <gnari@simnet.is> wrote in message
news:<caddc0$gs7$1@news.simnet.is>...
> > "A. Farber" <Alexander.Farber@t-online.de> wrote in message
> > news:c9ccaf83.0406110314.4dfa34a5@posting.google.com...
> > >
> > > perl -e '%h=(a=>1,b=>2); for (keys %h) { s/a/b/ }; print keys %h'
> >
> > perl -e '%h=(a=>1,b=>2); my @k=keys %h; for (@k) { s/a/b/ }; print @k'
>
> Thanks, but that doesn't help me.
>
[snip example]

if I understand you correctly, you want

  my @newkeys=@oldkeys=keys %$prj_exports;
  s[^/mnt/cali5/][$(TOPDIR)/] for (@newkeys);
  my $new_export;
  @{$new_export}{@newkeys}=@{$prj_export}{@oldkeys};

your original post was confusing because your example inplied
duplicate key 'b' in result.

gnari





------------------------------

Date: Sat, 12 Jun 2004 12:22:21 +0200
From: Xaver Biton <javier@t-online.de>
Subject: Re: perl IF DBI::errsrt
Message-Id: <caelgt$l5g$06$1@news.t-online.com>

Kevin Collins wrote:
> In article <841xkms3lg.fsf@notvalid.se>, moller@notvalid.se wrote:
> 
>>Xaver Biton <javier@t-online.de> writes:
>>
>>
>>>Hi,
>>>
>>>I'writing a program which will be used to migrate a mysql DB to
>>>another mysql DB.

>>In cases like this it's *almost* *always* better to use
>>the tools provided by the database.

>>From webpage:
>>"The mysqldump client can be used to dump a database or a collection of
>> databases for backup or for transferring the data to another SQL server
>> (not necessarily a MySQL server). The dump will contain SQL statements
>> to create the table and/or populate the table. "
>>
>>Start reading here if interested.
>>http://dev.mysql.com/doc/mysql/en/mysqldump.html
> 
Hi,

its not only a question of dumping and inserting, the big problem is 
that the old DB was not normalized, the new db is normalized, so I've to 
transform/process the data and insert other columnvalues, keysvalues, 
names and I use a anderstandable name convention, so the best way is to 
use perl for such work. I found The approach of Xho interesting, but 
later I thought, that inserting the failed record in a error table its 
not cleaver, because I've to create a new error table for each real 
table, instead I tought that using a file in which will be written every 
the message error ( row number and tablename) should be better.

what you think about?

Xaver


------------------------------

Date: Sat, 12 Jun 2004 17:00:33 GMT
From: spamtotrash@toomuchfiction.com (Kevin Collins)
Subject: Re: perl IF DBI::errsrt
Message-Id: <slrnccmdlg.s31.spamtotrash@doom.unix-guy.com>

In article <84vfhxreh9.fsf@notvalid.se>, moller@notvalid.se wrote:
> spamtotrash@toomuchfiction.com (Kevin Collins) writes:
> 
>> In article <841xkms3lg.fsf@notvalid.se>, moller@notvalid.se wrote:
>> > Xaver Biton <javier@t-online.de> writes:
>> > 
>> >> Hi,
>> >> 
>> >> I'writing a program which will be used to migrate a mysql DB to
>> >> another mysql DB.
>> >> 
>> >> Because the data quantity is relative big, if an arror occur while
>> >> inserting a record  in the new db I would like to deviate this record
>> >> error table.
>> >> 
>> >> How ban achive that.
>> >> If someone could make a little example I would be grathefull.
>> > 
>> > Not an answer to your question but..
>> > 
>> > In cases like this it's *almost* *always* better to use
>> > the tools provided by the database.
>> 
>> No, in cases like *this*, it is next to impossible to use the tools provided by
>> the database. The OP said he wants to do something when an error occurs (other
>> than fail, which is what would happen) and the tools provided by MySQL don't
>> support that.
> 
> Hmm, I have never done this in MySQL but I find it strange that it doesen't
> have any errorhandling besides failure. It should at least log the sql's that
> failed, this then would give the OP a smaller subset to work with.

The mechanism would be something like:

$ mysqldump old_db | mysql new_db

Actually, I've just looked at the help of the 'mysql' command and there is a
'--force' option which will continue regardless of a SQL error... This still
doesn't allow you to perform any action, but it may be better than just
failing.

Now, you could use one of the MySQL APIs and write something, but that is the
equivalent of what the OP is attempting to do with Perl and DBI.

> But as I'm not familiar with MySQL I really can't disagree with you.
> 
> An afterthought.
> If the *somthing* the OP wants to do when somthing fails is nontrivial a perl
> solution is probably best regardless of database. The most complicated I have
> had to do is a few conditionals and/or followed by an alternative sql statment.
 
Kevin


------------------------------

Date: 12 Jun 2004 14:58:19 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Perl text-handling help
Message-Id: <caf5mb$hu1$1@mamenchi.zrz.TU-Berlin.DE>

Camelback Jones  <ignoromnibus@cochon.fr> wrote in comp.lang.perl.misc:
> Paul Lalli wrote:
> 
> > On Fri, 11 Jun 2004, Camelback Jones wrote:
> > 
> >> Ben Morrow wrote:
> >>
> >> > Quoth Camelback Jones <ignoromnibus@cochon.fr>:
> >> >
> >> >> Paul Lalli wrote:
> >> >>
> >> >> > On Fri, 11 Jun 2004, Paul Lalli wrote:
> >> >> >
> >> >> >> On Fri, 11 Jun 2004, Camelback Jones wrote:
> >> >> >>
> >> >> >> > In the xBase languages (Clipper, FoxPro, *Harbour, etc) there is
> >> >> >> > a set of functions for handling "memo" files of text. Basically,
> >> >> >> > a memo is a chunk of text of any size whatever, and the functions
> >> >> >> > include the ability to set
> >> >> >> > the desired width of output text, find the number of lines of
> >> >> >> > text in the memo (given the desired width), and get any given
> >> >> >> > line of text
> >> >>
> >> >> Text::Wrap formats the text for output nicely, but leaves a bit to be
> >> >> desired with regard to being able to retrieve a specific line. The
> >> >> problem is that the formatted output is not consistent in length, so
> >> >> that direct access by offset doesn't work reliably... I'm looking for
> >> >> a way to put the output text in an array (list?) so that a desired
> >> >> line is found simply by the array index.
> >> >>
> >> >> use Text::Wrap;
> >> >> $linelen = 53;
> >> >> Text::Wrap::columns = $linelen;
> >> >>
> >> >> $text = magic();
> >> >>
> >> >> print wrap('','',$text);
> >> >> print "\n\nso far, so good...";
> >> >
> >> > my @wrapped = wrap '', '', $text;
> >> > print "line 6 is $wrapped[5]\n";
> >>
> >> Thanks, Ben, but it didn't work - came out empty ($text is about 12 lines
> >> long at 50 char per line, so line 6 does exist).
> > 
> > Like I said in my original post, I hadn't played with this module very
> > often.  I did just run a few tests, however.  It seems like wrap() always
> > returns the formatted data as scalar.  I don't know why the documentation
> > shows the return value being assigned to an array.  That being the case,
> > if you want to find the 6th line, there are a variety of ways you could do
> > it.  Here's one.  I'm willing to bet others will suggest more.
> > 
> > my $formatted = wrap('','', @orig);
> > my ($line) = $formatted =~ m|(?:.*$/){5}(.*)|;

Well, split suggests itself to just supply the missing functionality
to Text::Wrap.  To make it work as advertised, I'd do:

    use Text::Wrap();
    sub wrap { 
        return wantarray ? split /\n/, $_ : $_ for Text::Wrap::wrap( @_);
    }

Then use the solutions that have been suggested, but rely on the behavior
in array context.

Anno


------------------------------

Date: 12 Jun 2004 14:21:23 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Taint mode and PERL5LIB
Message-Id: <caf3h3$gp8$1@mamenchi.zrz.TU-Berlin.DE>

Gunnar Strand  <MyFirstnameHere.News1@gustra.org> wrote in comp.lang.perl.misc:
> Ben Morrow wrote:
> > Quoth kj <socyl@987jk.com>:
> > 
> >>When running under taint mode (-T switch), $ENV{PERL5LIB} is ignored.
> >>This presents a problem to CGI scripts that want to run in taint
> >>mode but need libraries installed in directories not mentioned in
> >>the default value of @INC [1].
> >>
> >>Then again, is running under taint mode really necessary past the
> >>development and testing phase?  In other words, is taint mode
> >>anything more than an additional check that the developer can make
> >>prior to releasing the code to make sure that there are no security
> >>gaps in the code, but once the code passes, taint mode can be safely
> >>turned off?
> > 
> > 
> > No.
> 
> That is interesting. According to the Perl taint faq i found
> (http://gunther.web66.com/FAQS/taintmode.html):
> 
>    Run-time checking means that you need to test all logical paths of
>    execution your script might take so that "legal operations" do not
>    get halted because of taint mode.
> 
> I get the impression that if the testing is thorough enough, taint
> mode could be turned off for release code. I do *not* argue that
> it *should* be turned off, I am just curious to know if there are
> situations after testing which could cause taint to fail, for
> instance that it would object to the contents of input, despite
> untainting?

Well, for one, there is more than tainted data that may make a taint
test fail.  If $ENV{ PATH} points to something that is writable by
anyone but its owner, you can launder the string all day but "system"
and friends will fail in taint mode.  That is one test that can only
be done at run time, there are certainly more.

But even if without run time tests, for a complete cover of all cases
you'd have to determine the set of all variables in your program
whose taintedness may matter (including parts of %ENV) and check the
behavior for every combination of tainted/untainted there is.
A look at the long section (in perlsec) that lists which Perl functions
may be taint-sensitive and in which way is discouraging.  

So, taint mode must stay on in production, for fundamental and
practical reasons.

Anno


------------------------------

Date: Sat, 12 Jun 2004 10:24:12 GMT
From: "blnukem" <bill@hotmail.com>
Subject: Values From Multiple Arrays
Message-Id: <gRAyc.16859$0g3.5038734@news4.srv.hcvlny.cv.net>

Hi All

I'm REALLY lost on getting this to work maybe someone can help me I have
three arrays that look like this:

my @Array1 = qw(john bill peter james);
my @Array2 = qw(car house motorcycle house);
my @Array3 = qw(red blue green teal);

and I want to output this format:

john car red
bill house blue
peter motorcycle green
james house teal

Thank to all in advance
Blnukem




------------------------------

Date: Sat, 12 Jun 2004 12:43:56 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Values From Multiple Arrays
Message-Id: <2j052eFruuobU1@uni-berlin.de>

blnukem wrote:
> I'm REALLY lost on getting this to work maybe someone can help me

What have you tried??

> I have three arrays that look like this:
> 
> my @Array1 = qw(john bill peter james);
> my @Array2 = qw(car house motorcycle house);
> my @Array3 = qw(red blue green teal);
> 
> and I want to output this format:
> 
> john car red
> bill house blue
> peter motorcycle green
> james house teal

     while (@Array1) {
         print (shift @Array1, ' ');
         print (shift @Array2, ' ');
         print (shift @Array3, "\n");
     }

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



------------------------------

Date: 12 Jun 2004 10:54:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Values From Multiple Arrays
Message-Id: <caencj$9lp$3@mamenchi.zrz.TU-Berlin.DE>

blnukem <bill@hotmail.com> wrote in comp.lang.perl.misc:
> Hi All
> 
> I'm REALLY lost on getting this to work maybe someone can help me I have
> three arrays that look like this:
> 
> my @Array1 = qw(john bill peter james);
> my @Array2 = qw(car house motorcycle house);
> my @Array3 = qw(red blue green teal);
> 
> and I want to output this format:
> 
> john car red
> bill house blue
> peter motorcycle green
> james house teal

Ah, ye olde transposition problem.

    print join( ' ', map shift @$_, \ ( @Array1, @Array2, @Array3)),
        "\n" while @Array1;

This destroys the original arrays.

Anno



------------------------------

Date: Sat, 12 Jun 2004 12:47:52 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Values From Multiple Arrays
Message-Id: <YXCyc.13677$tA6.10193@nwrddc03.gnilink.net>

blnukem wrote:
> I'm REALLY lost on getting this to work maybe someone can help me I
> have three arrays that look like this:
>
> my @Array1 = qw(john bill peter james);
> my @Array2 = qw(car house motorcycle house);
> my @Array3 = qw(red blue green teal);
>
> and I want to output this format:
>
> john car red
> bill house blue
> peter motorcycle green
> james house teal

What's the problem? Assuming the arrays have the same length:
    for (0..$#Array1) {
        print "$Array1[$_] $Array2[$_] $Array3[$_]\n"
    }

jue




------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6684
***************************************


home help back first fref pref prev next nref lref last post