[23983] in Perl-Users-Digest
Perl-Users Digest, Issue: 6184 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 24 03:05:34 2004
Date: Tue, 24 Feb 2004 00:05:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 24 Feb 2004 Volume: 10 Number: 6184
Today's topics:
Re: finding common words <dwall@fastmail.fm>
Re: finding common words <dwall@fastmail.fm>
Re: help on hash of hashes (Anno Siegel)
Re: how to use the pm in other location instead of curr (Anno Siegel)
Re: parsing data file into mysql table - help <gpatnude@adelphia.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 23 Feb 2004 14:34:43 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: finding common words
Message-Id: <Xns94986170C204Adkwwashere@216.168.3.30>
Tore Aursand <tore@aursand.no> wrote:
> my %ListA = ( apple => 1.1, banana => 2.2, cat => 3.3 );
> my %ListB = ( apple => 100, boy => 500, cat => 1000 );
> my @common = grep { $ListB{$_} } keys %ListA;
> for ( sort @common ) {
> print "$_\t$ListA{$_}\t$_\t$ListB{$_}\n";
> }
>
> Anything wrong with this one?
One nitpick: make it
my @common = grep { exists $ListB{$_} } keys %ListA;
in case %listB has a key whose value is false.
It's interesting to see so many different ways to do it. :-)
--
David Wall
------------------------------
Date: Mon, 23 Feb 2004 14:40:29 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: finding common words
Message-Id: <Xns9498626A7B8E7dkwwashere@216.168.3.30>
David K. Wall <dwall@fastmail.fm> wrote:
> Tore Aursand <tore@aursand.no> wrote:
>
>> my %ListA = ( apple => 1.1, banana => 2.2, cat => 3.3 );
>> my %ListB = ( apple => 100, boy => 500, cat => 1000 );
>> my @common = grep { $ListB{$_} } keys %ListA;
>> for ( sort @common ) {
>> print "$_\t$ListA{$_}\t$_\t$ListB{$_}\n";
>> }
>>
>> Anything wrong with this one?
>
> One nitpick: make it
>
> my @common = grep { exists $ListB{$_} } keys %ListA;
>
> in case %listB has a key whose value is false.
Oh, I see you already did that in another part of your post. Never mind.
I like it better than my adaptation of the 'intersection' FAQ answer.
--
David Wall
------------------------------
Date: 23 Feb 2004 15:13:22 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: help on hash of hashes
Message-Id: <c1d5ai$878$1@mamenchi.zrz.TU-Berlin.DE>
Stephen Moon <vibfft@yahoo.com> wrote in comp.lang.perl.misc:
> Anno,
[...]
> > > sub transpose {
> > > my $max = 0;
> > > $max > @$_ or $max = @$_ for @_; # length of longest line
> > > map [ map shift( @$_) || '', @_], 1 .. $max;
> > > }
>
> I have a hard time figuring out how transpose function. Maybe, a
> little explanation? Especially where you have "map [ map shift( @$_)
> || '',@_], 1 .. $max"
Well, it is rather compressed, so let's make it more explicit. First let's
expand the outer map and introduce meaningful variable names and some
comments along the way. (I'll assume $max has been calculated as before.
Since it's the length of the longest line it is also the number of lines
in the transposed matrix).
my @lines = @_; # an array of arrays which represents a matrix
my @cols; # will become the transposed matrix
# extract $max columns
for ( 1 .. $max ) {
push @cols, [ map shift @$_ || '', @lines]; # unchanged
}
# done, return result
@cols;
Now do the same with the inner map. We are using a fundamental identity
here. If some expression EXPR returns a list there are two ways to
get an array reference to that list. You can write
$ref = [ EXPR ];
or you can do
my @array = LIST;
$ref = \ @array;
The original code uses the first way for each (anonymous) column. The
expanded code uses the second with the named variable @col. The result
is the same.
my @lines = @_; # an array of arrays which represents a matrix
my @cols; # will become the transposed matrix
# extract $max columns
for ( 1 .. $max ) {
# extract one column
my @col;
# walk through all lines
for my $line ( @lines ) {
# shave off one element of $line and add it to the current
# column. if there are no more elements left in the line, add
# an empty string. $line is destroyed in the process.
push @col, shift @$line || '';
}
# another column is ready, push it onto the result list
push @cols, \ @col;
}
# done, return result
@cols;
I hope that makes things clearer.
As presented, this transposition routine isn't quite a general-purpose
matrix transposition. For one, it's destructive. After using it on
a matrix, the matrix lines will be reduced to empty lists. A general-
purpose routine shouldn't do that. At least, a property like that must
be documented. In blinking red, with a howling background noise.
Secondly, it replaces undefined elements in the original matrix with
empty strings. (Sloppily, it would even replace a 0 element with an
empty string.) A general purpose routine has no business doing that,
but in the case at hand it was convenient.
> Furthermore, what would be difference in using
> {} instead of [].
"{}" produces a hashref from a list of pairs. "[]" produces an
arrayref from any list. The two are never[1] interchangeable.
Anno.
[1] Huh. I said "never" about a Perl construct. Someone is going to
come up with a counter-example.
------------------------------
Date: 23 Feb 2004 15:34:59 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: how to use the pm in other location instead of current one.
Message-Id: <c1d6j3$878$2@mamenchi.zrz.TU-Berlin.DE>
Franklin Lee <pengtaoli@hotmail.com> wrote in comp.lang.perl.misc:
> Hi All,
>
> Now I have some pl and pm files in /opt/myapp/bin.
> But some pm (the same name with previous) may exit dir /opt/myapp/newbin.
> If this dir exist, I want to use the pm in it.
>
> So I write below codes:
> /opt/myapp/bin/app.pl
> **********************************************
> BEGIN {
> if(-r /opt/myapp/newbin) {
Missing quotes. This would result in a syntax error. Please copy/paste
code, don't re-type it.
> unshift(@INC, "/opt/myapp/newbin");
> } else {
> unshift(@INC, "/opt/myapp/bin");
> }
> }
>
> #my code begin....
> require "mymodule.pm";
>
> use strict;
> ....
> ***************************************************
>
> It seems work. The program load the PM in /opt/myapp/newbin/mymodule.pm.
> But I don't see this kind of example some where.
>
> So I want to know if it is a good one orthere exist better solution?
You can "use lib" instead, which saves you the BEGIN block:
use lib -r '/opt/myapp/newbin' ? '/opt/myapp/newbin' : '/opt/myapp/bin';
The expression after "lib" is evaluated at compile time, it piggybacks on
the compile-time execution of "use", as it were.
Anno
------------------------------
Date: Mon, 23 Feb 2004 07:25:02 -0800
From: "Greg Patnude" <gpatnude@adelphia.net>
Subject: Re: parsing data file into mysql table - help
Message-Id: <YIadneURiPzohqfdRVn-sQ@adelphia.com>
You've got AutoCommit set to off when you connect -- your database wont see
the inserted data until after you issue a $dbh->Commit();
--
Greg Patnude / The Digital Demention
2916 East Upper Hayden Lake Road
Hayden Lake, ID 83835
(208) 762-0762
"smrtalec" <smrtalec_NO_SPAM@algxmail.com> wrote in message
news:3vUZb.21103$wD5.18798@nwrddc03.gnilink.net...
>
>
> I have a script which function is to parse through a comma seperated data
> file, check if the entry is already existing, then if not enter the data
> into a mysql table. However, it only seems to be finding a match on the
> third time around. ie if 'john brown' is in the data file it will only
> recognise it as a match the second 'john brown'. It would almost seem the
> data from the previous row though inserted has not been registered. see
code
> snippet below
>
> <snippet>
> #! /usr/bin/perl -w
>
> use DBI;
>
>
> read_file();
>
> sub insert_data {
> my @$value = @_;
> my $rdate=get_date();
> my $p01=$dbh->quote ("0"); # own_id
> my $p02=$dbh->quote ($rdate); # prop_date_lmod
>
> ...........repetive code removed.................
>
> my $p12=$dbh->quote ($value->[9]); # own_mobile_phone
> my $sql = "INSERT INTO owner_info_inglewood
> VALUES ($p01,$p02,$p03,$p04,$p05,$p06,
> $p07,$p08,$p09,$p10,$p11,$p12
> );";
>
> my $sth_i = $dbh->prepare ($sql) or err_trap("failed to prepare
> statement\n");
> $sth_i->execute() or err_trap("failed to execute statement\n");
> $sth_i->finish();
> $dbh->commit() or err_trap("failed to execute statement\n");
> }
>
> sub verify_row {
> my @$vrow = @_;
> my $name_l = $dbh->quote($vrow->[0]);
> my $name_f = $dbh->quote($vrow->[1]);
> my $own_str_addr = $dbh->quote($vrow->[4]);
> my $own_city_addr = $dbh->quote($vrow->[5]);
> ## query property info table to retrieve property id.
> my $sql = ("SELECT own_name_l,own_name_f,
> own_str_addr,own_city_addr
> FROM owner_info_inglewood
> WHERE own_name_l LIKE $name_l
> AND own_name_f LIKE $name_f
> AND own_str_addr LIKE $own_str_addr
> AND own_city_addr LIKE $own_city_addr;")
> or err_trap('failed to prepare statement\n');
> my $sth_v = $dbh->prepare ($sql) or err_trap("failed to prepare
> statement\n");
> $sth_v->execute or err_trap("failed to execute statement\n");
> my @own_name = $sth_v->fetchrow();
> $sth_v->finish();
> return @own_name;
>
> }
>
> sub read_file {
> my @own_name;
> open (SDDB, "/home/rowan/perl/ho_info.csv") || die print "::$!\n";
> $dbh = connect_try("rowan","******");
> while (<SDDB>) {
> chomp $_;
> my $row = [(split (/,/,$_))];
> ## verify existance of property and retrieve property id.
> unless (verify_row(@$row)) {
> my $frow = format_data(@$row);
> insert_data(@$frow);
> }else{
> @own_name = verify_row(@$row);
> print "read_file::duplicate found ->
> $own_name[0],$own_name[1],$own_name[2],$own_name[3]\n";
> print "read_file::duplicate original->
> $row->[0],$row->[1],$row->[4],$row->[5]\n";
> }
> }
> $dbh->disconnect or err_trap("failed to disconnect statement\n");
> close SDDB;
> }
>
> sub connect_try {
> my ($db_user,$db_password) = @_;
> my $dbh = DBI->connect("dbi:mysql:studio_3:67.**.**.**", "$db_user",
> "$db_password",{AutoCommit => 0 }) or
> err_trap("error connecting to DB");
> return $dbh;
> }
>
> </snippet
>
>
>
> +++++++++++++++++++++++++++++++
> ++REMOVE _NO_SPAM from return ++
> ++e-mail address in order to respond. ++
> +++++++++++++++++++++++++++++++
>
>
------------------------------
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 6184
***************************************