[22926] in Perl-Users-Digest
Perl-Users Digest, Issue: 5146 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 27 14:05:36 2003
Date: Fri, 27 Jun 2003 11:05:06 -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 Fri, 27 Jun 2003 Volume: 10 Number: 5146
Today's topics:
Re: BTree examples ?? <paul.marquess@btinternet.com>
Re: Nutshell <johannes.fuernkranz@t-online.de>
Re: Nutshell <flavell@mail.cern.ch>
Re: Offer tips, comments on this code (generates html t (Bryan Castillo)
Re: Offer tips, comments on this code (generates html t <emschwar@pobox.com>
Re: Offer tips, comments on this code (generates html t <spamblock@junkmail.com>
Re: Offer tips, comments on this code (generates html t <nobull@mail.com>
Regex to capture ip address from hosts file <bcraw@adelphia.net>
Re: Regex to capture ip address from hosts file <glex_nospam@qwest.net>
Re: Regex to capture ip address from hosts file <krahnj@acm.org>
running UNIX with perl <tunmaster@hotmail.com>
Re: running UNIX with perl <thens@nospam.com>
Re: running UNIX with perl <nobull@mail.com>
Re: running UNIX with perl <tunmaster@hotmail.com>
Re: simple way to test for undef <abc@nowhere.com>
transform 3*0.0 into 0.0, 0.0, 0.0 <starobs99@yahoo.com>
Re: transform 3*0.0 into 0.0, 0.0, 0.0 <nobull@mail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 27 Jun 2003 16:49:55 +0100
From: "Paul Marquess" <paul.marquess@btinternet.com>
Subject: Re: BTree examples ??
Message-Id: <3efc67a3$0$18488$ed9e5944@reading.news.pipex.net>
"Kenjis Kaan" <tivolinewbie@canada.com> wrote in message
news:6a8ba9f8.0306270501.1b436eb0@posting.google.com...
> Hello. I am wondering if anyone has an example of using BTree for
> storing data on a disk file.?? I found some BTree modules on the
> web, but none give example of how you would then store it and retrieve
> it from disk drive. I have a need to store hundred of thousands of
> key, data values but told not to use a RDBMS. It would be nice if I
> can get some sample of that actually shows the write to disk and
> subsequent retrieval from it, which then allows you to do search for
> records by supplying a key. TIA
The DB_File module, that comes with Perl, has a btree opion. You can easily
store hundres of thousands of key/data pairs in it.
The example below is derived from the docuemntation that comes with DB_File
use warnings ;
use strict ;
use DB_File ;
my %h ;
tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0666, $DB_BTREE
or die "Cannot open file 'tree': $!\n" ;
# Add a key/value pair to the file
$h{'Wall'} = 'Larry' ;
$h{'Smith'} = 'John' ;
$h{'mouse'} = 'mickey' ;
$h{'duck'} = 'donald' ;
# Delete
delete $h{"duck"} ;
# Cycle through the keys printing them in order.
# Note it is not necessary to sort the keys as
# the btree will have kept them in order automatically.
foreach (keys %h)
{ print "$_\n" }
untie %h ;
Paul
------------------------------
Date: Fri, 27 Jun 2003 17:41:21 +0200
From: =?ISO-8859-1?Q?Johannes_F=FCrnkranz?= <johannes.fuernkranz@t-online.de>
Subject: Re: Nutshell
Message-Id: <bdhojf$dk$05$1@news.t-online.com>
Abigail wrote:
> Johannes Fürnkranz (johannes.fuernkranz@t-online.de) wrote on MMMDLXXXVII
> September MCMXCIII in <URL:news:bdhfou$73t$06$1@news.t-online.com>:
> ~~
> ~~ Nutshell is the only one I have which covers 5.8., which is why I
> ~~ mentioned it here. This was not necessarily meant to be a recommendation.
>
> I'll bite.
No, I won't let you...
> What parts of 5.8, which weren't in 5.6, does it cover?
I don't really know what's new in 5.8 that wasn't in 5.6. Locked hashes
are one thing that I happen to know of, and this is covered (albeit I
wasn't able to find out what they are good for, see a different posting
of mine earlier today).
The book covers all of the standard modules of 5.8 (certainly a much
larger set than 5.6) + CGI/LWP + DBI + a bit of XML + various networking
+ Tk + Win32. More than I'll ever need. If I only have one book to go
with me, this is the one, even if only for the doc of the standard
modules. But of course, if I want to learn any of these, this is a lousy
book. It's a reference.
Anyways, I'm not saying there are no better books out there, and I'm
also not saying it's perfect. It generally suits my needs as a
reference. You're mileage obviously varies, and that's fine.
Juffi
------------------------------
Date: Fri, 27 Jun 2003 18:37:55 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Nutshell
Message-Id: <Pine.LNX.4.53.0306271830030.11164@lxplus076.cern.ch>
On Fri, Jun 27, Johannes Fürnkranz inscribed on the eternal scroll:
> But of course, if I want to learn any of these, this is a lousy
> book. It's a reference.
If I wanted to learn something unfamiliar, I'd appreciate a good book
about it. Learning from reference documentation isn't such a good
idea IMNSHO.
However, if I wanted reference documentation, I don't see how I could
find anything better, at least as the core (i.e modulo any patches or
commentaries which might have been written to augment it), as the
reference documentation that accompanies the version of the software
that I'm using.
> Anyways, I'm not saying there are no better books out there, and I'm
> also not saying it's perfect. It generally suits my needs as a
> reference.
What's more, books (at least the conventional kind) can't be grep'ed
when you desperately need to locate something specific which the
publisher didn't happen to put in the index.
Well, everyone to their own taste, I suppose.
all the best
------------------------------
Date: 27 Jun 2003 08:49:55 -0700
From: rook_5150@yahoo.com (Bryan Castillo)
Subject: Re: Offer tips, comments on this code (generates html to index image files)
Message-Id: <1bff1830.0306270749.47558552@posting.google.com>
"David Oswald" <spamblock@junkmail.com> wrote in message news:<vfmqkhnh6eumf1@corp.supernews.com>...
> Thanks to all for the input. Now that I've got it working again, and now
> that it passes both "-w" and "use strict;", I'd like to hear comments again
> from a style, efficiency, and lexicon standpoint: How can the existing code
> be improved upon retaining equal functionality? In other words, I'm not
> looking for how to add features. I'm looking at critiquing the script
> holding its given featureset constant.
>
Im surprised no one else mentioned this.
I think it is good practice to pass references to filehandles to
subroutines, instead of using a global filehandle.
(Example)
open(HTML, ">out.html") || die;
call_sub1(\*HTML);
or
use IO::File;
my $html = IO::File->new(">out.html") || die;
call_sub1($html);
sub call_sub1 {
my $html = shift;
print $html "<h1>print something</h1>\n";
}
------------------------------
Date: 27 Jun 2003 10:39:27 -0600
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: Offer tips, comments on this code (generates html to index image files)
Message-Id: <eto65mrpj9c.fsf@wormtongue.emschwar>
"David Oswald" <spamblock@junkmail.com> writes:
> Having played with localtime(time); I'm not encouraged though.
'perldoc POSIX'; search for 'strftime'. It is your friend.
> Not to mention the fact that typing "date" at the shell prompt gives me a
> correct date, yet the raw dump of "localtime(time)" thinks it's four in the
> afternoon (it's ten at night really) December 31st, the 364th day of the
> year (reality is June). I suspect that my sysadmin has the time wrong on
> the computer that's hosting the perl engine.
Most likely, it's running in UTC, which is fairly common for servers.
You can either convert to local time, or (my favourite) just write
"<time> UTC".
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
------------------------------
Date: Fri, 27 Jun 2003 10:03:48 -0700
From: "David Oswald" <spamblock@junkmail.com>
Subject: Re: Offer tips, comments on this code (generates html to index image files)
Message-Id: <vfoudus1gtdu4d@corp.supernews.com>
Will a reference to a filehandle eliminate the issue of having "HTML"
filehandle suddenly become global? I thought that it does so regardless.
From what I can surmise, passing the filehandle as a reference basically
allows the filehandle to be created under a different name, and after that
all other subs will just reference it as a "reference" thus eliminating the
need to hardwire the name itself into the other subs. Correct?
Dave
"Bryan Castillo" <rook_5150@yahoo.com> wrote in message
news:1bff1830.0306270749.47558552@posting.google.com...
> "David Oswald" <spamblock@junkmail.com> wrote in message
news:<vfmqkhnh6eumf1@corp.supernews.com>...
> > Thanks to all for the input. Now that I've got it working again, and
now
> > that it passes both "-w" and "use strict;", I'd like to hear comments
again
> > from a style, efficiency, and lexicon standpoint: How can the existing
code
> > be improved upon retaining equal functionality? In other words, I'm not
> > looking for how to add features. I'm looking at critiquing the script
> > holding its given featureset constant.
> >
>
> Im surprised no one else mentioned this.
>
> I think it is good practice to pass references to filehandles to
> subroutines, instead of using a global filehandle.
>
> (Example)
>
> open(HTML, ">out.html") || die;
> call_sub1(\*HTML);
>
> or
>
> use IO::File;
> my $html = IO::File->new(">out.html") || die;
> call_sub1($html);
>
> sub call_sub1 {
> my $html = shift;
> print $html "<h1>print something</h1>\n";
> }
------------------------------
Date: 27 Jun 2003 18:39:50 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Offer tips, comments on this code (generates html to index image files)
Message-Id: <u9r85fctcp.fsf@wcl-l.bham.ac.uk>
"David Oswald" <spamblock@junkmail.com> rudely spits TOFU in our faces:
>
> "Bryan Castillo" <rook_5150@yahoo.com> wrote in message
> >
> > Im surprised no one else mentioned this.
> >
> > I think it is good practice to pass references to filehandles to
> > subroutines, instead of using a global filehandle.
> >
> > (Example)
> >
> > open(HTML, ">out.html") || die;
> > call_sub1(\*HTML);
> >
> > or
> >
> > use IO::File;
> > my $html = IO::File->new(">out.html") || die;
> > call_sub1($html);
> >
> > sub call_sub1 {
> > my $html = shift;
> > print $html "<h1>print something</h1>\n";
> > }
Or (in recent Perl) a hybrid:
open(my $html,'>','out.html) || die;
> Will a reference to a filehandle eliminate the issue of having "HTML"
> filehandle suddenly become global?
Not directly.
> I thought that it does so regardless.
Yes using the simple Perl4-style file handle "HTML" does make a
package scoped filehandle called HTML.
But if you look the second example in Bryan's post you'll see there is
another way. (Why is it, do you think, that top-posted responses are
highly correlated with not having read all of what's said).
> From what I can surmise, passing the filehandle as a reference basically
> allows the filehandle to be created under a different name, and after that
> all other subs will just reference it as a "reference" thus eliminating the
> need to hardwire the name itself into the other subs. Correct?
Yes.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 27 Jun 2003 17:04:40 GMT
From: "BC" <bcraw@adelphia.net>
Subject: Regex to capture ip address from hosts file
Message-Id: <IO_Ka.19018$Jw6.7827936@news1.news.adelphia.net>
i am trying to view the contents of the %ips hash created by the following
sub routine. create the hash, view the keys and values with the while loop
and list assignment using the each function. this has worked for me before
but i'm getting no data. is my regex wrong and i'm not building the hash
maybe?
my %ips = ();
cache_ips();
# for troubleshooting
# print keys and values of hash
while ( ( $key, $value ) = each(%ips ) ) {
print "$key => $value\n";
}
sub cache_ips {
open HOSTS, "</etc/hosts" or die "$!";
while(<HOSTS>) {
if(/^#?((\d{1,3}\.){3}\d{1,3})\s+[^ \t]+\s#\s+(.*)$/) {
$ips{$3}=$1;
}
}
}
regards,
bc
------------------------------
Date: Fri, 27 Jun 2003 12:15:22 -0500
From: "J. Gleixner" <glex_nospam@qwest.net>
Subject: Re: Regex to capture ip address from hosts file
Message-Id: <iY_Ka.88$AE.29803@news.uswest.net>
BC wrote:
[...]
> but i'm getting no data. is my regex wrong and i'm not building the hash
> maybe?
Correct. If you put a
print "found a match $1 $3\n"
after your if(), you'd have found that out.
Try:
if(/^#?((\d{1,3}\.){3}\d{1,3})\s+(.*)$/)
Provided you want to include any comments.
------------------------------
Date: Fri, 27 Jun 2003 17:52:07 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Regex to capture ip address from hosts file
Message-Id: <3EFC844A.53C92747@acm.org>
BC wrote:
>
> i am trying to view the contents of the %ips hash created by the following
> sub routine. create the hash, view the keys and values with the while loop
> and list assignment using the each function. this has worked for me before
> but i'm getting no data. is my regex wrong and i'm not building the hash
> maybe?
>
> my %ips = ();
>
> cache_ips();
>
> # for troubleshooting
> # print keys and values of hash
> while ( ( $key, $value ) = each(%ips ) ) {
> print "$key => $value\n";
> }
>
> sub cache_ips {
> open HOSTS, "</etc/hosts" or die "$!";
> while(<HOSTS>) {
> if(/^#?((\d{1,3}\.){3}\d{1,3})\s+[^ \t]+\s#\s+(.*)$/) {
> $ips{$3}=$1;
> }
> }
> }
You could always use the built-in gethostent() function:
use Socket;
my %ips;
cache_ips();
# for troubleshooting
# print keys and values of hash
while ( my ( $key, $value ) = each %ips ) {
print "$key => $value\n";
}
sub cache_ips {
while ( my @host = gethostent ) {
$ips{ $host[ 0 ] } = inet_ntoa( $host[ -1 ] );
}
}
__END__
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 27 Jun 2003 17:16:06 GMT
From: "joe" <tunmaster@hotmail.com>
Subject: running UNIX with perl
Message-Id: <qZ_Ka.427220$122.40223139@amsnews02.chello.com>
Dear Gurus,
I got stuck at the following. I've tried running a UNIX command in my Perl
CGI-script, but it doesn't work the way I expected it to. When I run the
next line:
$ls = `ls > file.txt`; or
system("ls > file.txt");
everything is ok. So running a UNIX command is not a problem an sich. But
when I run something like:
$ls = `man perl > file.txt`; or
system("mysqldump --user=username --password='password' dbname tablename >
file.txt");
an empty text-file is the result. How come? I'm a verry happy man already
knowing someone can help me out.
TIA
Joe
------------------------------
Date: Fri, 27 Jun 2003 23:03:19 +0530
From: Thens <thens@nospam.com>
Subject: Re: running UNIX with perl
Message-Id: <20030627230319.713e9e39.thens@nospam.com>
On Fri, 27 Jun 2003 17:16:06 GMT
"joe" <tunmaster@hotmail.com> wrote:
>Dear Gurus,
>everything is ok. So running a UNIX command is not a problem an sich. But
>when I run something like:
Yes. The problem is not with running unix commands.
>$ls = `man perl > file.txt`; or
>system("mysqldump --user=username --password='password' dbname tablename >
>file.txt");
>an empty text-file is the result. How come? I'm a verry happy man already
>knowing someone can help me out.
CGI programs run in the server as user nobody.
* Never use relative paths for accessing files
* Use absolute paths for system executables.
You might try
system("/usr/local/bin/mysqldump --user=username --password='password' dbname tablename > file.txt")
or wherever the installation is. And also how are you accessing the file 'file.txt'. Be sure to access it from the right place.
Regards,
Thens.
------------------------------
Date: 27 Jun 2003 18:31:03 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: running UNIX with perl
Message-Id: <u9vfurctrc.fsf@wcl-l.bham.ac.uk>
"joe" <tunmaster@hotmail.com> writes:
> I got stuck at the following. I've tried running a UNIX command in my Perl
> CGI-script, but it doesn't work the way I expected it to. When I run the
^^^^^^^^
> next line:
>
> $ls = `ls > file.txt`; or
> system("ls > file.txt");
>
> everything is ok. So running a UNIX command is not a problem an sich. But
> when I run something like:
>
> $ls = `man perl > file.txt`; or
> system("mysqldump --user=username --password='password' dbname tablename >
> file.txt");
>
> an empty text-file is the result. How come? I'm a verry happy man already
> knowing someone can help me out.
Have you tried these things from a command line script? Perlhaps you
are getting problems because of the CGI environment.
Are these failing commands emitting anything on STDOUT? (Would you know if
they were?).
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 27 Jun 2003 18:00:19 GMT
From: "joe" <tunmaster@hotmail.com>
Subject: Re: running UNIX with perl
Message-Id: <TC%Ka.427748$122.40315178@amsnews02.chello.com>
Thanks for the quick response, but i'm nu further. I made a shell script
with the following lines:
echo test1 > /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
mysqldump --user=username --password='password' dbname tablename >> file.txt
echo test2 >> /opt/guide/www.secretaressebank.nl/HTML/dev/dbdump.txt
When i exec this from the bash, it works just fine. But when i run it
through a CGI-script, it 'forgets' to execute the second and most important
line. Does it have anything to do with the rights? The CGI-script's rights
are 755, the shell script's are 777.
TIA again!
------------------------------
Date: Fri, 27 Jun 2003 17:20:25 GMT
From: ktom <abc@nowhere.com>
Subject: Re: simple way to test for undef
Message-Id: <3EFC7CD9.6070903@nowhere.com>
Tad McClellan wrote:
> ktom <abc@nowhere.com> wrote:
>
>
>>Subject: simple way to test for undef
>
>
>
> Testing for undef has nothing to do with your problem.
>
>
>
>>foreach my $item ( @ca ) {
>> if( defined ref $ca[$item] ) {
>
>
>
> $item gets the _values_ from @ca, you seem to be expecting it to
> get the _indexes_ of the values. So you want either:
>
> foreach my $item ( @ca ) { # preferred
> if( defined ref $item ) {
>
> or
>
> foreach my $index ( 0 .. $#ca ) {
> if( defined ref $ca[$index] ) {
>
i found this to work..
foreach my $index ( 0..$#ca ) {
if( defined $ca[$index] ) {
print "chain $index has $ca[$index] exceptions\n";
}
}
with an output of..
$VAR1 = 22;
$VAR2 = undef;
$VAR3 = 1;
$VAR4 = 5;
$VAR5 = 4;
$VAR6 = 141;
$VAR7 = 15;
chain 0 has 22 exceptions
chain 2 has 1 exceptions
chain 3 has 5 exceptions
chain 4 has 4 exceptions
chain 5 has 141 exceptions
chain 6 has 15 exceptions
also, converting to a hash and using keys works too.
thanks for your help.
>
>
>>am i making harder than it needs to be.
>
>
>
> I think the answer to that should be clear by now. :-)
>
>
------------------------------
Date: Fri, 27 Jun 2003 12:46:19 -0500
From: CM <starobs99@yahoo.com>
Subject: transform 3*0.0 into 0.0, 0.0, 0.0
Message-Id: <bdhvrl$t4ods$1@ID-189674.news.dfncis.de>
Hi,
I have a little probleme. As output of a code, I have something like:
9935.69043, 549.563843, 500., 6
10048.5518, 549.505005, 500., 7
10305.9766, 549.381104, 500., 8
3*0.E+0, 0
3*0.E+0, 0
3*0.E+0, 0
The problem is comming from the 3*0.E+0, which I want to transfrom into:
0.E+0, 0.E+0, 0.E+0
The solution should not be dependet of the both values, I mean I need a
general way to transform
N*blabla
into
blabla, blabla, blabla, .... N times.
Thanks for any help,
CM
PS: I don't know anything to perl, except it is certainly the apropriate
language for solving my problem... So if possible, provide a fully
useable script so that I just need to type:
removestars.ps input.dat output.dat
Thanks.
------------------------------
Date: 27 Jun 2003 18:53:54 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: transform 3*0.0 into 0.0, 0.0, 0.0
Message-Id: <u9llvncsp9.fsf@wcl-l.bham.ac.uk>
CM <starobs99@yahoo.com> writes:
> Hi,
> I have a little probleme. As output of a code, I have something like:
> 9935.69043, 549.563843, 500., 6
> 10048.5518, 549.505005, 500., 7
> 10305.9766, 549.381104, 500., 8
> 3*0.E+0, 0
> 3*0.E+0, 0
> 3*0.E+0, 0
>
> The problem is comming from the 3*0.E+0, which I want to transfrom into:
> 0.E+0, 0.E+0, 0.E+0
>
> The solution should not be dependet of the both values, I mean I need
> a general way to transform
>
> N*blabla
> into
> blabla, blabla, blabla, .... N times.
s/(\d+)\*(^[,]+)/join ", ", ( $2 ) x $1/eg;
> PS: I don't know anything to perl, except it is certainly the
> apropriate language for solving my problem... So if possible, provide
> a fully useable script so that I just need to type:
> removestars.ps input.dat output.dat
Oh sorry, handn't realised you were pan-handling. Oh well I've answered
the question now so I'll hit send anyhow.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
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 5146
***************************************