[18506] in Perl-Users-Digest
Perl-Users Digest, Issue: 674 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Apr 11 09:10:40 2001
Date: Wed, 11 Apr 2001 06:10:20 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <986994619-v10-i674@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 11 Apr 2001 Volume: 10 Number: 674
Today's topics:
reference , 2-dimensional array <a.v.a@home.nl>
Re: reference , 2-dimensional array <bart.lateur@skynet.be>
Re: reference , 2-dimensional array (Bernard El-Hagin)
Regex: Finding multiple targets <p.carmichael@btinternet.com>
Re: Regex: Finding multiple targets (Anno Siegel)
Re: Regex: Finding multiple targets <iltzu@sci.invalid>
Re: So what do YOU use Perl for? <bart.lateur@skynet.be>
Re: We have 'use strict' and 'my', and now 'our', but n <dont.reply@myself.com>
What is the correct syntax to count an item in db <luvkatz@tds.net>
Re: What is the correct syntax to count an item in db (Rafael Garcia-Suarez)
Re: Why is finish() necessary here with DBI? Bug in DBD <bart.lateur@skynet.be>
Re: Why Perl? <bart.lateur@skynet.be>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 11 Apr 2001 10:27:32 GMT
From: AvA <a.v.a@home.nl>
Subject: reference , 2-dimensional array
Message-Id: <3AD40958.4DFB2F6C@home.nl>
hi all,
i have a mysql database which records of websites and i need to load
them into a reference...a two-dimensional array.
The outcome should look like this:
@list = ( [ "1", "200", "http://www.yahoo.com"],
[ "2", "302", "http://www,somedomain.com"] ,
.........
);
im stuck with:
snip.....
$sth=$dbh->prepare ("select rec_id, status, url from url where rec_id
between 1
and 5");
$sth->execute or die "unable to execute query: $!";
foreach($sth->fetchrow_array){
push @list, [split];
}
.......snip
can someone enlighten me.
Thanks
AvA
irc.xchat.org #perl
------------------------------
Date: Wed, 11 Apr 2001 10:50:55 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: reference , 2-dimensional array
Message-Id: <dgd8dtocd0iideh86ej1nuk0qrhapk3q5s@4ax.com>
AvA wrote:
>@list = ( [ "1", "200", "http://www.yahoo.com"],
> [ "2", "302", "http://www,somedomain.com"] ,
> .........
> );
>
>im stuck with:
>snip.....
>
>$sth=$dbh->prepare ("select rec_id, status, url from url where rec_id
>between 1
>and 5");
>$sth->execute or die "unable to execute query: $!";
>foreach($sth->fetchrow_array){
>push @list, [split];
>}
I don't see how you can still need the split. You don't? This looks like
a design error.
This ought to be better:
while(my @fields = $sth->fetchrow_array){
push @list, \@fields;
}
or:
my $fields;
while($fields = $sth->fetchrow_arrayref){
push @list, [ @$fields ];
}
Unfortunately, fetchrow_arrayref returns the same reference every time
(for speed reasons).
--
Bart.
------------------------------
Date: Wed, 11 Apr 2001 11:07:11 +0000 (UTC)
From: bernard.el-hagin@lido-tech.net (Bernard El-Hagin)
Subject: Re: reference , 2-dimensional array
Message-Id: <slrn9d8emd.k53.bernard.el-hagin@gdndev32.lido-tech>
On Wed, 11 Apr 2001 10:27:32 GMT, AvA <a.v.a@home.nl> wrote:
>
>hi all,
>
>i have a mysql database which records of websites and i need to load
>them into a reference...a two-dimensional array.
>
>The outcome should look like this:
>
>@list = ( [ "1", "200", "http://www.yahoo.com"],
> [ "2", "302", "http://www,somedomain.com"] ,
> .........
> );
>
>im stuck with:
>snip.....
>
>$sth=$dbh->prepare ("select rec_id, status, url from url where rec_id
>between 1
>and 5");
>$sth->execute or die "unable to execute query: $!";
>foreach($sth->fetchrow_array){
>push @list, [split];
>}
> .......snip
>
>can someone enlighten me.
I don't know anything about that funky fetchrow_array thingy, but here's
an example that might help:
-----------------------
#!/usr/bin/perl -w
use strict;
my @stuff = qw(1|200|http://www.yahoo.com 2|302|http://www.somedomain.com);
my @list;
foreach(@stuff){
push @list, [split/\|/];
}
print @list->[1]->[2];
-----------------------
Cheers,
Bernard
--
perl -e'$\=qq;,\n;;s,,*Just.*another.*Perl.*hacker,e;%JaPh
=(q,*,,q, ,);s,((?:[\w]+?::)|\*),$JaPh{${((sin(32)**2)+
(cos(32)**2))}},gex;print;'
------------------------------
Date: Wed, 11 Apr 2001 09:26:46 +0100
From: "Patrick Carmichael" <p.carmichael@btinternet.com>
Subject: Regex: Finding multiple targets
Message-Id: <9b14lg$pcn$1@neptunium.btinternet.com>
I'm traversing a hash and searching for each value in a block of text. If
they are found (no problem with that) I want to print them out *in the
order* in which they *first appear* in the target text.
Is there a simple way of doing this? I can sort them on the length of $` -
i.e. how far they are from the beginning of the target but this has
performance overheads, involves setting up a temporary data structure en
route and it seems terribly inelegant!
Any offers?
------------------------------
Date: 11 Apr 2001 09:04:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Regex: Finding multiple targets
Message-Id: <9b16m2$9jv$1@mamenchi.zrz.TU-Berlin.DE>
According to Patrick Carmichael <p.carmichael@btinternet.com>:
> I'm traversing a hash and searching for each value in a block of text. If
> they are found (no problem with that) I want to print them out *in the
> order* in which they *first appear* in the target text.
Are those fixed strings?
> Is there a simple way of doing this? I can sort them on the length of $` -
> i.e. how far they are from the beginning of the target but this has
> performance overheads, involves setting up a temporary data structure en
> route and it seems terribly inelegant!
If you have fixed strings to search for, just use index() instead of
a regex. This will give you their position in the text immediately.
Anno
------------------------------
Date: 11 Apr 2001 11:44:27 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Regex: Finding multiple targets
Message-Id: <986988242.12507@itz.pp.sci.fi>
In article <9b14lg$pcn$1@neptunium.btinternet.com>, Patrick Carmichael wrote:
>I'm traversing a hash and searching for each value in a block of text. If
>they are found (no problem with that) I want to print them out *in the
>order* in which they *first appear* in the target text.
>
>Is there a simple way of doing this? I can sort them on the length of $` -
>i.e. how far they are from the beginning of the target but this has
>performance overheads, involves setting up a temporary data structure en
>route and it seems terribly inelegant!
Well, you could at least replace $` with pos():
print map "$$_[1]\n", sort {$a->[0] <=> $b->[0]} map {
my @rv = $string =~ /($_)/g ? [pos($string) - length($1), $_] : ();
pos($string) = 0; # reset position for next match
@rv;
} @search_rexeps;
That still sets up a temporary LoL, but I don't think there's any
cleaner way. It's certainly better than putting the match in the
sortsub.
Of course, if you're searching for simple strings instead of regexps,
simply use index().
--
Ilmari Karonen - http://www.sci.fi/~iltzu/
Please ignore Godzilla / Kira -- do not feed the troll.
------------------------------
Date: Wed, 11 Apr 2001 07:20:42 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: So what do YOU use Perl for?
Message-Id: <5q08dt4od09ieerpdmjnrbv2588qv9a21h@4ax.com>
Martien Verbruggen wrote:
>> True but far and away the most searched for items on search.cpan.org are
>> DBI, CGI, LWP, MIME::*, HTML::* and XML. While this doesn't
>> indicate usage it does indate that 80% of the queries are related to
>> CGI and the web in some form or another.
>I wouldn't count DBI, XML, MIME and LWP as CGI related. And even the
>HTML modules aren't necessarily part of that.
s/CGI/internet/
MIME, LWP, HTML are all indeed internet related. Even XML has it's crib
in the web, having been conceived as an "easier to parse" little brother
to SGML, mainly for programmers of browsers. OTOH LWP only serves to
process the *results* gotten back from a http query. CGI is on the other
end of the wire.
But not DBI. DBI is just a (*the*) standard way of accessing an SQL
database in Perl. Nothing to do with the web whatsoever.
The OP just might have said as well that the purpouse of print() is to
send data back from a CGI script to the web server. Well, almost. :-).
This is, of course, pretty narrow-minded.
--
Bart.
------------------------------
Date: Wed, 11 Apr 2001 09:51:29 +0100
From: Ian Phillipps <dont.reply@myself.com>
Subject: Re: We have 'use strict' and 'my', and now 'our', but no 'use local-scope' - why?
Message-Id: <3AD41B11.ED30BD56@myself.com>
Mark Jason Dominus wrote:
> In article <3AD2864E.68F8FBCF@nowhere.com>,
> Phil Voris <pvoris@nekophile.com> wrote:
> >I've long been baffled that there is no pragma to automatically declare
> >all variables as default locally-scoped.
>
> Because nobody knows what that would mean.
>
> >This way, rather than typing 'my' everywhere, one could just type the
> >ocassional 'our' and assume everything else is local (er, my'd).
> But local to what? The file? The innermost block in which it appears?
> Neither of those are sensible.
If the pragma is the equivalent of inserting an implicit "my" at the
first appearance of each variable name which is not 'local' or given an
explicit package), then I'd vote it as fairly sensible.
The semantics of the implicit "my" would, of course, depend on the
position of the declaration in the same way as the explicit version.
Obvious tweaks are needed; thus saying "$x[1]=2" would be equivalent to
"my @x; $x[1]=2;".
Whether such a pragma would be a Good Thing is another question. I'd
vote against it on grounds of extra obfuscation for comparitively little
benefit.
Ian
------------------------------
Date: Wed, 11 Apr 2001 11:51:09 GMT
From: "jim flaherty" <luvkatz@tds.net>
Subject: What is the correct syntax to count an item in db
Message-Id: <NyXA6.2250$wt6.1547838@ratbert.tds.net>
Hello I have red hat 7 with mysql DB . Perl loaded with DBI and DBD
modules. What is the correct syntax to count certain records in the DB ?
------------------------------
Date: 11 Apr 2001 12:09:02 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: What is the correct syntax to count an item in db
Message-Id: <slrn9d8ibu.o3j.rgarciasuarez@rafael.kazibao.net>
jim flaherty wrote in comp.lang.perl.misc:
} Hello I have red hat 7 with mysql DB . Perl loaded with DBI and DBD
} modules. What is the correct syntax to count certain records in the DB ?
Isn't this an SQL question ?
For the Perl part of your question :
connect to the DB, prepare the statement, execute it, and fetch the
results, as described in the DBI manpage.
--
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
------------------------------
Date: Wed, 11 Apr 2001 08:33:27 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Why is finish() necessary here with DBI? Bug in DBD::ODBC?
Message-Id: <4058dt4grogcbefroit09vassj32nca6hd@4ax.com>
Caveat: I don't understand much of your specific problem. These are just
a few loose remarks.
Thomas wrote:
>I am using DBI version 1.14, DBD::ODBC version 0.28, and SQL Server
>7.0 on a Windows NT machine.
>Because of the limitations of SQL Server, only one statement handle
>can be used per database handle.
Heh?
>While testing this module, I found that I would often get this error:
>
>DBD::ODBC::st execute failed: [Microsoft][ODBC SQL Server
>Driver]Connection
>is busy with results for another hstmt (SQL-S1000)(DBD:
>st_execute/SQLExecute err=-1) at C:/Perl/site/lib/DB.pm line 466.
>
>Line 466 is where I prepare a new statement handle, overwriting the
>old statement handle (which may have been in use already).
I don't know if this is how it's supposed to be. But, if I prepare my
statement handles before doing anything else, I can prepare as many as I
have tried so far (around 10).
Are you using placeholders? That definitely helps in avoiding having to
recreate a new statement every single time.
Anyway, my experience with finish() is that you don't need it if you get
every record from a SELECT until the dB notices that there are no more,
but you do need it if you "know" you can only find one record and this
is the one you retrieve, without testing if there are any more.
Oh, and some funny behaviour I found with MS SQL Server: you need to do
a rollback or a commit before disconnecting, even if you only did a
read-only query. That is silly, really, but easily done.
Oh well. HTH.
--
Bart.
------------------------------
Date: Wed, 11 Apr 2001 10:27:54 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Why Perl?
Message-Id: <5nb8dtgnig1ggg025fgiidsirev8n4sb4j@4ax.com>
GrapeApe wrote:
>Yep have MacPerl, but of course can't think of what to do with it yet. It may
>be a matter of looking at the o'reilly books again to scrap up some ideas, or
>digging through my old problems that I have worked some other work pattern
>around.
I think it's possible to create filters in Perl for use within BBEdit,
but I don't know how myself. You might need a commercial version of
BBEdit.
You could subscribe to the (main) MacPerl mailing list. Roughly 50
messages a week, and extremely friendly folks. They don't even mind an
off-topic question once in a while. At least *some* of them know how to
integrate BBEdit with MacPerl, as, for example, one of the main
developers of BBEdit is on the list.
[About the ST:]
>Actually yes, that looks handy, from reading some Perlmonk discussion.
It's very handy indeed. For example, here's how you can sort lines of
text, sorting an incorporated integer numerically:
@sorted = map $_->[0],
sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] ||
$a->[3] cmp $b->[3] }
map { [ $_, split /(\d+)/ ] } @unsorted;
This will, for example, sort "figure 99" in front of "figure 101" and
behind "figure 10".
--
Bart.
------------------------------
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.
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 674
**************************************