[13345] in Perl-Users-Digest
Perl-Users Digest, Issue: 755 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 10 00:08:14 1999
Date: Thu, 9 Sep 1999 21:05:09 -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 Thu, 9 Sep 1999 Volume: 9 Number: 755
Today's topics:
Re: Advanced replace function <madebeer@igc.apc.org>
Re: Advanced replace function (Kragen Sitaker)
Re: Alternative to ELSIF statements (Matthew Bafford)
Re: Comparing against today's date? <madebeer@igc.apc.org>
Re: Comparing against today's date? (Larry Rosler)
Re: foreach oddity <bwalton@rochester.rr.com>
Format an variable <rich.harris@#prodigy.net>
Re: Format an variable (Kragen Sitaker)
Re: Generating random numbers from an array <dove@synopsys.com>
Re: Help! HTTP500 error when running Perl script on Fro <gilbert@yahoo.com>
Re: How to capture STDERR from the perl binary (Kragen Sitaker)
Re: How to run a script from a script? (Abigail)
Re: Inserting environment variables at the command prom <dove@synopsys.com>
Re: Inserting environment variables at the command prom (Kragen Sitaker)
IPC daemonization (Bill Moseley)
Link CGI to microsoft Access <andriani@singnet.com.sg>
Re: Link CGI to microsoft Access <bwalton@rochester.rr.com>
Re: LWP: Get <bwalton@rochester.rr.com>
Re: make this a one-liner? <bmetcalf@nortelnetworks.com>
Re: Opening files versus array searching (Kragen Sitaker)
Re: Opening files versus array searching (Eric Bohlman)
Re: perl objects and hashes of hashes <duane@blur.com>
Question: How to send large (3+ MB) emails via Net::SMT eric_poulsen@my-deja.com
Re: Removing a line from a text file (Larry Rosler)
Re: Removing a line from a text file (Eric Bohlman)
Re: Sorting by mid-record without splitting? (Kragen Sitaker)
Re: Syntax Error <uri@sysarch.com>
Re: using 'my' and 'required' files (Kragen Sitaker)
Re: Will an apology end this mess? (Id Est)
Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 09 Sep 1999 16:59:12 -0700 (PDT)
From: Michael de Beer <madebeer@igc.apc.org>
Subject: Re: Advanced replace function
Message-Id: <APC&1'0'50775da1'ecc@igc.apc.org>
#!/usr/local/bin/perl5 -w
# setup the example
#
$string = "%anything1%anything2\n";
chomp ($string);
# do the substitution
#
$string =~ s|%(.[^%]*)%(.[^%]*)|<font class=$1>$2</font>|;
# print the results
#
print $string . "\n";
# some comments on the regex:
# 1) I used the | instead of / to avoid HTML closing tag </font>
# my basic strategy is to enclose what we want in parantheses, and exclude
# what we don't want outside the parantheses
# so the first match is the literal %, which is before the parantheses
# inside the first paran's , we stash anything (except a %) in $1
# NOTE, this regex won't work right if $string has extra % or is multi-line
# similarly for the second paren
__END__
------------------------------
Date: Fri, 10 Sep 1999 04:01:09 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Advanced replace function
Message-Id: <9o%B3.1284$N77.78625@typ11.nn.bcandid.com>
In article <APC&1'0'50775da1'ecc@igc.apc.org>,
Michael de Beer <madebeer@igc.apc.org> wrote:
>$string = "%anything1%anything2\n";
>chomp ($string);
The requestor asked to replace the \n; I don't know if he really meant
that or not.
>$string =~ s|%(.[^%]*)%(.[^%]*)|<font class=$1>$2</font>|;
I am curious why you have a leading . inside each pair of (). Are you
thinking that sometimes the first character, but no other character, of
anything1 or anything2 might be a %? I didn't gather that from the
original poster's question, but maybe you've exchanged email with them.
># 1) I used the | instead of / to avoid HTML closing tag </font>
Yeah, I probably should have done that too.
Kragen
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Fri, 10 Sep 1999 02:57:45 GMT
From: *@dragons.duesouth.net (Matthew Bafford)
Subject: Re: Alternative to ELSIF statements
Message-Id: <slrn7tgs57.ov.*@dragons.duesouth.net>
On Thu, 09 Sep 1999 11:36:51 GMT, dave h <dave4000@my-deja.com> enriched
us with:
: if ($option ==1) {
: do option1();
: }
: elsif ($option ==2) {
: do option2();
: }
: ...etc, etc, all the way to 46.
perldoc -q switch
: Thanks in advance
HTH,
: Dave.
--Matthew
------------------------------
Date: Thu, 09 Sep 1999 16:27:29 -0700 (PDT)
From: Michael de Beer <madebeer@igc.apc.org>
Subject: Re: Comparing against today's date?
Message-Id: <APC&1'0'50775da0'6dd@igc.apc.org>
I think your split/compare with localtime strategy is good.
You might also look at some of the Date modules in CPAN.
$> perldoc -q dates
Found in /usr/local/lib/perl5.004/pod/perlfaq4.pod
How can I compare two dates and find the difference?
If you're storing your dates as epoch seconds then simply subtract one
from the other. If you've got a structured date (distinct year, day,
month, hour, minute, seconds values) then use one of the Date::Manip
and Date::Calc modules from CPAN.
-Mike
------------------------------
Date: Thu, 9 Sep 1999 20:17:20 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Comparing against today's date?
Message-Id: <MPG.1242181f77febb7a989f3e@nntp.hpl.hp.com>
In article <APC&1'0'50775da0'6dd@igc.apc.org> on Thu, 09 Sep 1999
16:27:29 -0700 (PDT), Michael de Beer <madebeer@igc.apc.org> says...
> I think your split/compare with localtime strategy is good.
> You might also look at some of the Date modules in CPAN.
>
> $> perldoc -q dates
> Found in /usr/local/lib/perl5.004/pod/perlfaq4.pod
> How can I compare two dates and find the difference?
>
> If you're storing your dates as epoch seconds then simply subtract one
> from the other. If you've got a structured date (distinct year, day,
> month, hour, minute, seconds values) then use one of the Date::Manip
> and Date::Calc modules from CPAN.
Those are heavyweight modules, and not necessarily installed. If you
have a structured date, use a module that is part of the standard
distribution, Time::Local.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Thu, 09 Sep 1999 23:48:19 -0400
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: foreach oddity
Message-Id: <37D87F83.1D514D32@rochester.rr.com>
David Bradley wrote:
>
> Has anyone noticed that if you try
> to return the value of the foreach
> iterator in a function, you are screwed.
>
> For example
>
> sub func {
> foreach my $i (1..10) {
> return $i if $i == 4; } }
> print &func();
>
> doesn't print anything out. Doing the following
Hmmm...when I try the exact code above, it prints "4" (ActiveState Perl
build 518, Windoz 95). What is your OS, Perl version, platform, etc?
> seems to rectify the problem:
>
> sub func {
> foreach my $i (1..10) {
> return (my $retval = $i) if $i == 4; } }
> print &func();
>
> Questions:
> ----------
> 1) Is this a documented feature?
> 2) Can this be easily fixed?
>
> - Dave
>
> P.S. For the cut and pasters out there:
>
> perl5 -le 'sub junk { foreach my $i (1..10) { return $i if $i == 4; } }
> print &junk();'
> perl5 -le 'sub junk { foreach my $i (1..10) { return (my $retval = $i)
> if $i == 4; } } print &junk();'
------------------------------
Date: Thu, 9 Sep 1999 23:15:16 -0400
From: "Rich Harris" <rich.harris@#prodigy.net>
Subject: Format an variable
Message-Id: <7r9t98$52c6$1@newssvr03-int.news.prodigy.com>
Flip, how the heck to for format a variable.
I guess you use printf %something or another?!!??!!??
Help with the syntax please.
I have a variable which has a large number of characters after the decimal
point, I would like to display the number with one decimal point .
Thanks
Rich
------------------------------
Date: Fri, 10 Sep 1999 04:03:46 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Format an variable
Message-Id: <Cq%B3.1291$N77.78497@typ11.nn.bcandid.com>
In article <7r9t98$52c6$1@newssvr03-int.news.prodigy.com>,
Rich Harris <rich.harris@#prodigy.net> wrote:
>I guess you use printf %something or another?!!??!!??
>
>Help with the syntax please.
The Unix man pages for the printf function should help. man 3 printf,
or if you're on some SysV monstrosity, man -s 3 printf.
>I have a variable which has a large number of characters after the decimal
>point, I would like to display the number with one decimal point .
Is your variable a number you've produced with arithmetic or a string
that happens to be numeric? (If the latter, using floating-point
formats like %.1f might produce undesirable rounding.) How many
decimal points does it have in it to begin with, and which are the ones
you want to get rid of?
Kragen
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Thu, 09 Sep 1999 17:11:39 -0700
From: David Amann <dove@synopsys.com>
Subject: Re: Generating random numbers from an array
Message-Id: <37D84CBB.CE573CD0@synopsys.com>
--------------970918FF548196CD5551F054
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi,
> Would anyone happen to know how to generate a random number from
> a set of array elements?
Yeppir. I got this answer from the great book, "The Perl Cookbook" by Tom
Christiansen and Nathan Torkington, published by O'Reilley. (Unabased plug
to follow.) This book is by far my most referenced Perl book. The
organization is amaizing! Definitely worth the $40 bux. Run out to Amazon
and buy it immediately! (End plug)
Begin example:
------------------------------------------------------------------
#!/usr/local/bin/perl -w
my @array = ("10", "20", "30");
my $random_element = $array[rand @array];
print "Random Element: $random_element\n";
-------------------------------------------------------------------
Hope this helps,
-=dav
--------------970918FF548196CD5551F054
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi,
<blockquote TYPE=CITE>Would anyone happen to know how to generate a random
number from
<br>a set of array elements?</blockquote>
<p><br>Yeppir. I got this answer from the great book, "The Perl Cookbook"
by Tom Christiansen and Nathan Torkington, published by O'Reilley.
(Unabased plug to follow.) This book is by far my most referenced
Perl book. The organization is amaizing! Definitely worth the $40
bux. Run out to Amazon and buy it immediately! (End plug)
<p>Begin example:
<br>------------------------------------------------------------------
<br><tt>#!/usr/local/bin/perl -w</tt><tt></tt>
<p><tt>my @array = ("10", "20", "30");</tt>
<br><tt>my $random_element = $array[rand @array];</tt>
<br><tt>print "Random Element: $random_element\n";</tt>
<br>-------------------------------------------------------------------
<p>Hope this helps,
<br>-=dav
<br> </html>
--------------970918FF548196CD5551F054--
------------------------------
Date: Fri, 10 Sep 1999 11:28:27 +0800
From: "gilbert" <gilbert@yahoo.com>
Subject: Re: Help! HTTP500 error when running Perl script on Frontpage
Message-Id: <7r9tsa$pc4@imsp212.netvigator.com>
Thank Bill,
I try to find the web server log by searching my harddisk, but I couldn't
find it. Please tell me where can I check the error log.
Thanks,
Bill Moseley <moseley@best.com> wrote in message
news:MPG.124158c9acd9bd0a989729@nntp1.ba.best.com...
> gilbert (gilbert@yahoo.com) seems to say...
> > Please help, why I can't run the perl script on frontpage?
> > The following step have been done.
> >
> > 1. perl script was located at frontpage's cgi-bin, both cgi-bin & local
are
> > checked with "Allow Script or Program to be run"
> > 2. Frontpage pws was on.
> > 3. regedit's ScriptMap .pl was added and pointed to local perl.exe %s
%s.
>
> You have an error on line 17.
>
> Check your web server error log and check back with the CGI newsgroup.
> So far, this isn't a problem with perl.
>
>
>
> --
> Bill Moseley mailto:moseley@best.com
> pls note the one line sig, not counting this one.
------------------------------
Date: Fri, 10 Sep 1999 03:56:40 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: How to capture STDERR from the perl binary
Message-Id: <Yj%B3.1276$N77.74566@typ11.nn.bcandid.com>
In article <37e21846.20182328@mdnews.btv.ibm.com>,
<raymondj@NOSPAM.raleigh.ibm.com> wrote:
>test.pl >out 2>&1
>
>and then I issue a sigkill via kill -9 (pid of the perl process) I get
>a text message of "Killed" from the perl binary but I want to capture
>this text in the file "out" as described above.
>
>Seondly, how can the perl binary catch the sigkill interrupt in order
>to print the "Killed" message in the first place?
The Perl binary exits without having a chance to do anything, as soon
as it receives this signal. Your shell notices that the process has
died, looks at its exit status, concludes it was killed by signal 9,
and prints "Killed" on your screen. I think redirecting your shell's
standard error should capture the message. (It will also make your
shell almost unusable. :)
Kragen
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: 9 Sep 1999 19:30:24 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: How to run a script from a script?
Message-Id: <slrn7tgke6.pg8.abigail@alexandra.delanet.com>
Joe Frey (frey@saturn.med.nyu.edu) wrote on MMCC September MCMXCIII in
<URL:news:37D82217.DAE99E2F@saturn.med.nyu.edu>:
:: I'm using a file grepped fron ps to determine if I need to restart
:: another script. All works well except #exec "home/joe/31donegone.pl";#
:: How do I call a script from a script?
exec, open, @ARGV/<>, system and qx.
Abigail
--
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
print } sub __PACKAGE__ { &
print ( __PACKAGE__)} &
__PACKAGE__
( )
-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
http://www.newsfeeds.com The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including Dedicated Binaries Servers ==-----
------------------------------
Date: Thu, 09 Sep 1999 17:18:56 -0700
From: David Amann <dove@synopsys.com>
Subject: Re: Inserting environment variables at the command prompt
Message-Id: <37D84E6F.41BAE1A6@synopsys.com>
--------------4CD79946CA8A55FC850E394C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
steveeq1@earthlink.net wrote:
> I am debugging a script in Perl that uses variables dervied from the
> $ENV{"QUERY_STRING"} variable. Is there a way to manually provide the
> $ENV{"QUERY_STRING"} from the command prompt for debugging purposes?
>
Hi Steve,
Yeppir. The easiest way is to use the CGI.pm package. My favorite book
about CGI.pm is Lincoln Stein's little tome, "The Official Guide to
Progamming with CGI.pm". It'll save you bunches of time in writing
CGI's. I've already worn out the first copy I've bought and now have two
(one for the office and one for home.)
In particular, if you use CGI.pm, when you run your CGI from the command
line, it will prompt you for the various parameters you want to pass.
You just type them in name value format as follows:
% test.cgi
(offiline mode: enter name=value pairs on standard input)
first_name=David
last_name=Amann
^D
You can also just enter these in on the command line as follows:
% test.cgi 'name=David Amann' age=31
Hope this helps;
-=dav
--------------4CD79946CA8A55FC850E394C
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<br>steveeq1@earthlink.net wrote:
<blockquote TYPE=CITE>I am debugging a script in Perl that uses variables
dervied from the
<br>$ENV{"QUERY_STRING"} variable. Is there a way to manually provide the
<br>$ENV{"QUERY_STRING"} from the command prompt for debugging purposes?
<br> </blockquote>
Hi Steve,
<p>Yeppir. The easiest way is to use the CGI.pm package. My
favorite book about CGI.pm is Lincoln Stein's little tome, "The Official
Guide to Progamming with CGI.pm". It'll save you bunches of time
in writing CGI's. I've already worn out the first copy I've bought
and now have two (one for the office and one for home.)
<p>In particular, if you use CGI.pm, when you run your CGI from the command
line, it will prompt you for the various parameters you want to pass.
You just type them in name value format as follows:
<p><tt>% test.cgi</tt>
<br><tt>(offiline mode: enter name=value pairs on standard input)</tt>
<br><tt>first_name=David</tt>
<br><tt>last_name=Amann</tt>
<br><tt>^D</tt>
<p>You can also just enter these in on the command line as follows:
<p><tt>% test.cgi 'name=David Amann' age=31</tt>
<p>Hope this helps;
<p>-=dav
<br> </html>
--------------4CD79946CA8A55FC850E394C--
------------------------------
Date: Fri, 10 Sep 1999 01:43:05 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Inserting environment variables at the command prompt
Message-Id: <JmZB3.986$N77.54843@typ11.nn.bcandid.com>
In article <7r9c66$56a$1@nnrp1.deja.com>, <steveeq1@earthlink.net> wrote:
>I am debugging a script in Perl that uses variables dervied from the
>$ENV{"QUERY_STRING"} variable. Is there a way to manually provide the
>$ENV{"QUERY_STRING"} from the command prompt for debugging purposes?
Assuming you're exactly like me and use bash, you can say something like
export QUERY_STRING='walnuts=bananas&tilapia=fish'
./whatchamacallit.cgi
If you're using sh, csh, or a non-Unix OS, you will have to set it
differently. One alternative that works with all shells under Unix:
env QUERY_STRING='walnuts=bananas&tilapia=fish' ./whatchamacallit.cgi
But you should probably use CGI.pm, which makes this all much easier.
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Thu, 9 Sep 1999 17:37:06 -0700
From: moseley@best.com (Bill Moseley)
Subject: IPC daemonization
Message-Id: <MPG.1241f28ea829afc498972e@nntp1.ba.best.com>
My IPC skills are really bad.
I want to fork a child and then let the parent exit with the child
running. (The parent happens to be a CGI script.)
I don't really follow the example under "Complete Dissociation of Child
from Parent" in perlipc. I don't understand what setsid does, for one
thing.
The child will run for probably 5 minutes or so, so I need to close the
session with the web server and let the child run.
I'm wondering if the child's code can be in the parent's script (or if I
have to have a separate script), and how to actually code thing.
I'm also unclear if I need to do anything special to keep the web server
from killing the child.
Also, the parent has a log file open (via STDERR) and I wonder if that
can be passed to the child and remain open for writing after the parent
exits.
Can someone clue in the clueless?
Thank,
--
Bill Moseley mailto:moseley@best.com
pls note the one line sig, not counting this one.
------------------------------
Date: Fri, 10 Sep 1999 10:21:31 +0800
From: Andriani Siau <andriani@singnet.com.sg>
Subject: Link CGI to microsoft Access
Message-Id: <37D86B2B.EE5DC283@singnet.com.sg>
anyone know how to retrieve and save data from microsoft access using
perl.
Thank you in advance.
bye
andriani
------------------------------
Date: Thu, 09 Sep 1999 23:26:33 -0400
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Link CGI to microsoft Access
Message-Id: <37D87A69.A0EA00A8@rochester.rr.com>
Andriani Siau wrote:
> anyone know how to retrieve and save data from microsoft access using
> perl.
> Thank you in advance.
>
> bye
> andriani
Here is one way, courtesy of the perl2exe folks. Works in CGI scripts,
too.
# ODBC test program
# Seting up an ODBC database
#a) Click Settings/ControlPanel
#b) Click 32-Bit ODBC
#c) Select "System DSN" tab
#d) Click Add, select "Microsoft Access Driver", click Finish
#e) Enter the following properties
# Data Source Name: test_dsn (This is the most important field)
# Description: Anything
#f) Click on Create... and choose a filename, eg. c:\database\test.mdb
#g) Next time Click on Select ... and choose a filename, eg.
c:\database\test.mdb
use Win32::ODBC;
&insertData;
&getData;
exit(0);
sub insertData {
$dsn = "test_dsn";
$db = new Win32::ODBC($dsn);
die "ERROR: Failed to open database\n" if(!$db);
$sql = "CREATE TABLE test_table (";
$sql .= "id char(02), ";
$sql .= "name char(04), ";
$sql .= "city char(03) )";
$db->Sql($sql);
($ErrNum, $ErrText, $ErrConn) = $db->Error();
$sql = "DELETE FROM test_table";
$db->Sql($sql);
$sql = "INSERT INTO test_table (id, name, city) VALUES ('01', 'Jack',
'NY')";
$db->Sql($sql);
$sql = "INSERT INTO test_table (id, name, city) VALUES ('02', 'John',
'Lon')";
$db->Sql($sql);
$sql = "INSERT INTO test_table (id, name, city) VALUES ('03', 'Mike',
'HK')";
$db->Sql($sql);
$sql = "INSERT INTO test_table (id, name, city) VALUES ('04', 'Carl',
'LA')";
$db->Sql($sql);
}
sub getData {
$sql = "SELECT * from test_table order by id";
$db->Sql($sql);
while ($db->FetchRow()) {
($id, $name, $city) = $db->Data("id", "name", "city");
print $id, " - ", $name, " - ", $city, "\n";
}
$db->Close();
}
--
Bob Walton
------------------------------
Date: Thu, 09 Sep 1999 23:38:27 -0400
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: LWP: Get
Message-Id: <37D87D33.213F8BC9@rochester.rr.com>
Susan Malmrose wrote:
> I need to use LWP and the get function to pull several text files from an
> outside server, however I need them to all end up as one text file on my
> server. I'm completely new to Perl. Can anyone give me an example or point
> me to some resources that will show examples of how to code for something
> like this?
...
Susan, something like:
use LWP::Simple;
print get("http://www.server1.com/whatever.txt").
get("http://www.server2.com/blahblah.txt").
get("http://www.server3.com/xyz.txt");
should do the trick. See the docs for LWP::Simple.
------------------------------
Date: Thu, 09 Sep 1999 17:20:29 -0700
From: Brandon Metcalf <bmetcalf@nortelnetworks.com>
Subject: Re: make this a one-liner?
Message-Id: <37D84ECD.CE4F2C14@nortelnetworks.com>
Good stuff.
Thanks.
Brandon
------------------------------
Date: Fri, 10 Sep 1999 02:19:10 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Opening files versus array searching
Message-Id: <yUZB3.1116$N77.60767@typ11.nn.bcandid.com>
In article <7r9emq$ffd$1@news.fas.harvard.edu>,
Jefferson Bell <jebell@fas.harvard.edu> wrote:
> I'm writing a script that searches through
>several textfiles that are somewhat interconnected
>as a rough database. In writing this script, I'm
>wondering if it would be faster in searching to create
>one large 3 dimensional array at first or to open and close
>each of the ~50 files as needed possibly opening and
>closing the same file more than once.
It would probably be faster to slurp everything into RAM -- if it fits
-- and build hashes to help find things quickly. The 'Benchmark'
module is supposed to help answer questions like this; here are my results:
Benchmark: timing 10000 iterations of openclose...
openclose: 31 wallclock secs ( 5.04 usr + 4.88 sys = 9.92 CPU)
Benchmark: timing 1000000 iterations of writeref...
writeref: 29 wallclock secs (21.02 usr + 0.00 sys = 21.02 CPU)
Benchmark: timing 1000000 iterations of readref...
readref: 33 wallclock secs (21.85 usr + 0.01 sys = 21.86 CPU)
'openclose' opens the benchmark program's source text (20 lines), reads
it in, and closes the file. 'writeref' writes through a three-level
hash (a hash of hashes of hashes). 'readref' reads the entry
'writeref' wrote.
You might notice that all three took about the same time. You might
miss that writeref and readref did 100 iterations for each of
openclose's iterations during that time.
This is on
SunOS kirk 5.6 Generic_105181-13 sun4m sparc SUNW,SPARCstation-20
So my guess is that slurping into RAM is almost guaranteed to be a win.
Here's the code. (It's my first Benchmark.pm program.)
#!/usr/bin/perl -w
use Benchmark;
my $i;
my %a;
timethese(10000, {
'openclose' => sub {
open X, "<tmp.x" or die "can't open tmp.x: $!\n";
while (<X>) { }
close X;
}
}
);
timethese(1_000_000, {
'writeref' => sub { $a{b}{c}{d} = 'foogle' },
}
);
timethese(1_000_000, {
'readref' => sub {$a{b}{c}{d}}
}
);
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: 10 Sep 1999 03:46:44 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: Opening files versus array searching
Message-Id: <7r9uv4$p5d@dfw-ixnews6.ix.netcom.com>
Jefferson Bell (jebell@fas.harvard.edu) wrote:
: I'm writing a script that searches through
: several textfiles that are somewhat interconnected
: as a rough database. In writing this script, I'm
: wondering if it would be faster in searching to create
: one large 3 dimensional array at first or to open and close
: each of the ~50 files as needed possibly opening and
: closing the same file more than once.
If each search is typically going to access all or most of the files,
then reading them all into memory first will usually be fastest, assuming
you have enough physical memory (things will slow down if your process
starts swapping). If each search is going to access more than a few
files, but not all of them, I'd go with the array but build it in a lazy
fashion: wait until you actually need a file before reading it in
(remember that the way Perl implements multidimensional arrays, an empty
row takes up very little space). This is easy to do:
unless (defined $files[$file]) {
open (TEXT,"file$file.txt") or die "Can't open file$file.txt: $!";
@{$files[$file]}=<TEXT>;
close TEXT;
}
foreach (@{$files[$file]}) {
# do something with each line
}
------------------------------
Date: Thu, 09 Sep 1999 17:50:26 -0700
From: duane powell <duane@blur.com>
Subject: Re: perl objects and hashes of hashes
Message-Id: <37D855D2.22572DF2@blur.com>
Thanks for the style points.
Maybe I was unclear. There is no bug here. (But I'll be sure to use strict and
-w, so don't fret)
The question was "But how do I define a hash of hashes within a class?"
A hash within a class is easy:
my $self = {
_hashREF = {};
}
but how do you go deeper and represent a hash of hashes?
Now I realize you don't have to define it in the constructor.
You just code something like
$self->{_hashREF}->{$var}->{_deeperHashREF}->{$someOtherVar}++;
in a method and it springs into existence.
Damn, perl is amazing.
"I.J. Garlick" wrote:
> In article <37D711C5.D669826A@blur.com>,
> duane powell <duane@blur.com> writes:
> > My question is about hashes of hashes and perl objects. It's clear to me
> > how to create a self extending object with a hash reference. But how do
> > I define a hash of hashes within a class?
> >
>
> Do hope there is a use strict somewhere in the code you didn't cut and
> paste. What there wasn't? shame on you. (Bet there was no -w either).
>
> > package Tree;
> > sub new {
> > my($class,$name) = @_;
> > my $class = ref($class) || $class;
> ^^
> Get rid of that. No need. It may actually ruin anything you try to use
> this package to inherit from, will need some one more expert than me to
> confirm.
>
> >
> > my $self = {};
> > $self->{NAME} = $name;
> >
> > $self->{_BRANCHES} = {};
>
> Personally I have a tendancy to do
> my $self = {
> NAME => $name,
> _BRANCHES => {},
> };
>
> for the base class. But that's just me.
>
> >
> [snipped, what looks like ok code, it runs anyway]
>
> > print "\t", $b, "\t", $self->{_BRANCHES}->{$b}, "\n";
>
> would change the above to
>
> print "\t$b\t$self->{_BRANCHES}->{$b}\n";
>
> personally.
>
> Exactly where are you having a problem? The code you used could have come
> from the camel if memory serves. You just changed a few variables and
> added a couple of accessor methods.
>
> --
> Ian J. Garlick
> ijg@csc.liv.ac.uk
>
> Democracy is a device that insures we shall be governed no better than
> we deserve.
> -- George Bernard Shaw
------------------------------
Date: Fri, 10 Sep 1999 01:05:58 GMT
From: eric_poulsen@my-deja.com
Subject: Question: How to send large (3+ MB) emails via Net::SMTP or other module
Message-Id: <7r9lgq$bs1$1@nnrp1.deja.com>
I have a remote machine reporting system that emails zipped files to
clients & myself. It works very well for smallish files (<1MB), but
usually cannot send larger files.
The Net::SMTP module doesn't allow passing of a filehandle or filename
(that I'm aware of, and this is confirmed from viewing the source), but
instead requires a variable or reference. Passing a filehandle
reference does not work.
Has anyone out there had any success at sending large SMTP messages via
(pure) perl?
I'm running 5.004 on NT boxes, so no access to those cool *NIX utilities
like sendmail =P
Thanks ...
epoulsen@*spam*be*gone*htsinteractive.com
Naturally remove the *spam*be*gone* if you're nice enough to email me.
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
------------------------------
Date: Thu, 9 Sep 1999 20:23:31 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Removing a line from a text file
Message-Id: <MPG.1242198b309f759c989f3f@nntp.hpl.hp.com>
In article <7OYHEEpXw-B@khms.westfalen.de> on 09 Sep 1999 23:21:00
+0200, Kai Henningsen <kaih=7OYHEEpXw-B@khms.westfalen.de> says...
> lr@hpl.hp.com (Larry Rosler) wrote on 28.08.99 in <MPG.1231bf3083d23984989ec4@nntp.hpl.hp.com>:
>
> > That question is Frequently Asked here.
>
> What fascinates me is that this *is* a FAQ, when to me the answer would
> seem extremely obvious. I don't think I want to think about what this says
> about the people posing this particular question.
>
> I mean, even a completely non-computer person should get the analogy to
> inserting/deleting from the middle of a page of a book, say.
That is a reasonable analogy. However, in most likelihood those who ask
this question are using a different analogy: adding, deleting, or
modifying lines using a text editor or a word processor. There the
mechanism is hidden.
--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 10 Sep 1999 03:56:17 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: Removing a line from a text file
Message-Id: <7r9vh1$p5d@dfw-ixnews6.ix.netcom.com>
Larry Rosler (lr@hpl.hp.com) wrote:
: That is a reasonable analogy. However, in most likelihood those who ask
: this question are using a different analogy: adding, deleting, or
: modifying lines using a text editor or a word processor. There the
: mechanism is hidden.
Except that, especially if they're on a GUI system, they're aware that
they have to explicitly save the file they're editing, which ought to
suggest that the changes are actually done by rewriting the whole file.
I'm more inclined to think that it's a result of thinking of files as
array-like structures of physical records rather than as streams of
bytes. As late as 20 years ago, I could understand why people would be
automatically inclined to think that way (22 years ago, *I* had a hard
time thinking of a file as anything other than an array of fixed-size
records), but I'm not sure why people think that now.
------------------------------
Date: Fri, 10 Sep 1999 03:52:16 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Sorting by mid-record without splitting?
Message-Id: <Qf%B3.1270$N77.77252@typ11.nn.bcandid.com>
In article <37b4759d.527933037@news.iinc.com>,
Effie Rover <null@effierover.com> wrote:
>Once I have my array, I want to sort it by the third field in the
>record, i.e. here's a sample of my database:
>1|Effie's Birthday|effie|10/07/1999|||||||Owner's Birthday|
>2|Basqual's Birthday|basqual|10/14/1999|||||||The Mighty Ranger|
>
>I already know how to split records, pull fields and either build a
>hash or another array with the key field in front for sorting. Isn't
>there a Perl construct to sort these without splitting? I thought
>there was, but I pored over 'sort' in all my available resources
>(book, docs, faq and Deja) and can't find a solution without building
>an extraneous hash. Or maybe I'm just not understanding the code
>correctly.
Well, you probably don't really want to do this, but you can if you
think it's fun.
#!/usr/bin/perl -w
use strict;
my ($k1, $k2);
print sort {
$a =~ /^[^|]*\|[^|]*\|([^|]*)\|/ or die "didn't match $a\n";
$k1 = $1;
$b =~ /^[^|]*\|[^|]*\|([^|]*)\|/ or die "didn't match $a\n";
$k2 = $1;
die "k2 undef" if not defined $k2;
$k1 cmp $k2
} <>;
This avoids splitting or extraneous storage, but it ends up doing two
regex matches for every comparison in the sort.
This can be simplified a bit:
#!/usr/bin/perl -w
use strict;
print sort {
($a =~ /^[^|]*\|[^|]*\|([^|]*)\|/ && $1) cmp
($b =~ /^[^|]*\|[^|]*\|([^|]*)\|/ && $1) } <>;
Except that doesn't work, possibly because $1 is an lvalue. Copying $1
into another variable makes it work:
#!/usr/bin/perl -w
use strict;
print sort {
do {my $x; $a =~ /^[^|]*\|[^|]*\|([^|]*)\|/ and $x=$1; $x} cmp
do {my $x; $b =~ /^[^|]*\|[^|]*\|([^|]*)\|/ and $x=$1; $x} } <>;
This will sort incorrect records before all the rest, printing warnings.
If I had this problem, though, I'd solve it with splitting, although I
don't think I'd use the sort key as the key of a hash. Probably more
like this:
#!/usr/bin/perl -w
use strict;
my @lines = map {chomp; [split /\|/]} <>;
print map { join ('~', @$_), "\n" } sort { $a->[2] cmp $b->[2] } @lines;
This solution is probably easier to read, simpler in terms of the
abstract structure of the data, and probably faster too, although I
haven't benchmarked.
(I'm always reluctant to compare the readability of things I wrote five
minutes ago -- after all, I don't have any trouble understanding them,
usually, but that's because I just wrote them.)
Here I rejoined with ~ instead of | just to prove I was really
splitting it :)
HTH. Let me know if this code is less readable than I think it is and
I'll be happy to explain it.
Kragen
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: 09 Sep 1999 22:02:51 -0400
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Syntax Error
Message-Id: <x7671jmsl0.fsf@home.sysarch.com>
>>>>> "A" == Abigail <abigail@delanet.com> writes:
A> Uri Guttman (uri@sysarch.com) wrote on MMCC September MCMXCIII in
A> \\ the bind operator is =~.
A> \\ and you can better express the negative if with unless.
A> Or with !~
i rarely need to use that. it doesn't resonate in my skull in a pleasing
tone. i just don't think, "$foo does not match regex". it also make no
sense for tr///. i'd just as soon not use it.
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
uri@sysarch.com --------------------------- Perl, Internet, UNIX Consulting
Have Perl, Will Travel ----------------------------- http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
"F**king Windows 98", said the general in South Park before shooting Bill.
------------------------------
Date: Fri, 10 Sep 1999 01:59:44 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: using 'my' and 'required' files
Message-Id: <kCZB3.1061$N77.56729@typ11.nn.bcandid.com>
In article <37D8381B.F529E7F6@direct2u.co.uk>,
cLive hoLLoway <cLive@direct2u.co.uk> wrote:
>I require a configuration file at runtime. How should I declare
>variables in this file, and how should I then refer to them from the
>program?
If you require it in a sub BEGIN, it'll be just like old times. Only
with error checking ;)
(ObPartyLine: require is evil. Don't use it.)
>Also, I create a bunch of vars (for convenience) from a hash by:
>
>foreach $var (keys %Hash) {
> $$var = $Hash{$var};
>}
>
>I'm still a little vague on reference thingies. Do I declare the $$var
>created? how do I refer to it in one script if it's being run in a
>script required by the original script?
You can't do symrefs under strict 'refs'. I tried and failed to do the
same thing by messing with %main:: -- here's my broken code:
#!/usr/bin/perl -w
use strict;
my %hash = ('a' => 'b', 'c' => 'd');
map { $main::{$_} = $hash{$_} } keys %hash;
print $a, "\n";
For some bizarre reason, according to perl -d, this makes $main::{'a'}
be *main::b, and $main::{'c'} point to *main::d.
You can, of course, do symrefs with eval STRING. (ObPartyLine: eval
STRING is evil too.)
#!/usr/bin/perl -w
use strict;
my %hash = ('a' => 'b', 'c' => 'd');
map { eval "\$main::$_ = \$hash{$_};"; die $@ if $@ } keys %hash;
print $a, "\n";
(To get this to work without warnings, I think you need to use vars on
each of the variables.)
>aaaarghh. Anyone seen a nice description of all this with lovely, pink,
>fluffy examples that won't make my brain hurt too much?
HTH. (And I hope you end up doing things a different way, because I
don't really disagree with the party line. :)
Kragen
--
<kragen@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Thu Sep 09 1999
60 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>
------------------------------
Date: Fri, 10 Sep 1999 01:28:12 GMT
From: id-est@home.com (Id Est)
Subject: Re: Will an apology end this mess?
Message-Id: <slrn7tgnlb.3hq.id-est@erato.bigredrockeater.com>
In article <7r8v51$755$0@216.39.133.47>, Robert Front wrote:
(much un-necessary kowtowing deleted)
geez kid, show some backbone. who gives a rats ass if Abigoon doesn't like
you? he/she/it and his/her/its defenders are arrogant bullies who think
they're above the rest of us. who cares? f*ck 'em and killfile 'em.
you can learn perl quite well without their "help".
------------------------------
Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 1 Jul 99)
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq" from
almanac@ruby.oce.orst.edu. The real FAQ, as it appeared last in the
newsgroup, can be retrieved with the request "send perl-users FAQ" from
almanac@ruby.oce.orst.edu. Due to their sizes, neither the Meta-FAQ nor
the FAQ are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq" from
almanac@ruby.oce.orst.edu.
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 V9 Issue 755
*************************************