[25237] in Perl-Users-Digest
Perl-Users Digest, Issue: 7482 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 4 03:05:53 2004
Date: Sat, 4 Dec 2004 00:05:06 -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 Sat, 4 Dec 2004 Volume: 10 Number: 7482
Today's topics:
Re: (beginner question) downsample data points using ch (Oscar Stiffelman)
Re: conditioning system shell output from within Perl - <tintin@invalid.invalid>
FAQ 5.3: How do I count the number of lines in a file? <comdog@panix.com>
how I can get more error info from virtual machine? <end@dream.life>
installing dbd::mysql via cygwin on windows <user@example.net>
parse hash by array element seems to run slow (JRoot)
Re: parse hash by array element seems to run slow <uri@stemsystems.com>
Re: Perl / MySql / Shopping cart <nospam@bigpond.com>
Re: Perl Books <uri@stemsystems.com>
Perl crashes <jodox@sbcglobal.net>
Re: Perl crashes <1usa@llenroc.ude.invalid>
Re: Perl crashes <jurgenex@hotmail.com>
Re: print FILE truncating input <jkeen_via_google@yahoo.com>
Re: print FILE truncating input <noreply@gunnar.cc>
Re: Question on loops and return values <jkeen_via_google@yahoo.com>
Re: Question on loops and return values <tadmc@augustmail.com>
RegEx Help Needed <no-spam@sonic.net>
Re: RegEx Help Needed <spamtrap@dot-app.org>
Re: RegEx Help Needed <no-spam@sonic.net>
Re: RegEx Help Needed <eighner@io.com>
Re: RegEx Help Needed <dha@panix.com>
Re: RegEx Help Needed <no-spam@sonic.net>
write error message to a log file while processing the (Jing)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 3 Dec 2004 19:09:11 -0800
From: imagine@gmail.com (Oscar Stiffelman)
Subject: Re: (beginner question) downsample data points using chained maps
Message-Id: <a6aad5c7.0412031909.3d60dd9d@posting.google.com>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<copned$6o5$1@mamenchi.zrz.TU-Berlin.DE>...
> Oscar Stiffelman <imagine@gmail.com> wrote in comp.lang.perl.misc:
> > I have a vector of numeric points specified as 2 cols in a text file.
> > I want to break the points into windows and compute averages for each
> > window.
> >
> > And (this is important), I am looking for a compact perlish way to do
>
> Why is this *important*? A prototype that does what you want, even
> in a less than elegant way, would be worth more at this point.
>
Because this is just a toy problem I chose to understand the power of
the language for working with this kind of data (lists of
n-dimensional numbers). I am especially interested in Perl's support
for anonymous functions because I have found mappping of anonymous
functions to be a really convenient way to interact with this kind of
data in other languages.
Ultimately, what I am interested in is the type of transformations
that I can compactly express on the command line (using perl -e)
instead of from within a specific math environment.
I hate the context shifts that come from
1. c++ program produces data
2. start math software, load data, transform,plot,transform,plot,...
3. modify c++ program and goto 1
I am hoping to move to something more like:
c++ program | perl -e 'some transformation' | plot
for at least some fraction of my work.
> > it, ideally using the more "functional programming" parts of the
> > language, as opposed to the direct, nested for loops.
> >
> > I am new to Perl, so I would appreciate feedback on the approach I
> > have taken below.
>
> You're going about this the wrong way. Start out with something
> pedestrian and simple. When it *works*, you can transform it into
> tight code, if you want.
>
> > #!/usr/bin/perl
>
> No warnings, no strict. Add them, and the variable declarations that
> are then necessary.
>
> > $w = shift; # this is the block size
> > die "must specify window size" if !$w;
> > @x = sort {@$a[0] <=> @$b[0]} map {[split]} <>;
>
> What is this sort for? It sorts by the number of items in each line,
> which is 2 every time by your own specification.
>
> > print "@$_\n" for map {
> > ($s0,$s1)=(0,0);
> > for(@$_) {
> > $s0 += @$_[0];
> > $s1 += @$_[1]
> > };
> > [$s0/$w, $s1/$w]
>
> If $w doesn't divide the number of lines, you may end up with a
> final window of fewer than $w points. It would be better to
> divide by the actual number of points instead of $w.
>
> > } map {
> > [@x[$_*$w .. ($_+1)*$w-1]]
>
> This is fragile if $w doesn't divide the number of records. splice()
> would be a better tool.
>
> > } (0..$#x/$w);
>
> When I run this it prints out a series of pairs of zeroes for me
> (the right number of pairs, but zero). I'm not going to debug it.
>
> > # In pseudocode, this is what it does:
> > for(i = 0; i < n; i+= width) {
> > (x,y) = (0,0);
> > for(int j = 0; j < width; j++) {
> > x += vec[i+j][0];
> > y += vec[i+j][1];
> > }
> push results, [x/width, y/width];
> > }
>
> It would have been better to make this a working Perl solution first.
>
> > Can anyone suggest an even more compact or idiomatically-correct way
> > to do this?
>
> Here is how I would do it:
>
> my @data = <DATA>;
> my @res;
> while ( @data ) {
> my @chunk = splice @data, 0, $w;
> my ( $x_mean, $y_mean);
> for ( @chunk ) {
> my ( $x, $y) = split;
> $x_mean += $x;
> $y_mean += $y;
> }
> push @res, [ $x_mean/@chunk, $y_mean/@chunk];
> }
> print "@$_\n" for @res;
>
> Now, if you want, you can rework some of the loops as map()s:
>
> my @data = <DATA>;
> print "@$_\n" for map {
> my ( $x, $y);
> for ( @$_ ) {
> $x += $_->[ 0];
> $y += $_->[ 1];
> }
> [ $x/@$_, $y/@$_];
> } map [ map [ split], splice( @data, 0, $w)], 0 .. $#data/$w;
>
> Anno
Thanks, this is great.
-- Oscar
------------------------------
Date: Sat, 4 Dec 2004 15:55:27 +1300
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: conditioning system shell output from within Perl - Noob
Message-Id: <31cn64F34p94aU1@individual.net>
<jason@cyberpine.com> wrote in message
news:ef0a04d7.0412031340.12810549@posting.google.com...
> When I execute the folloing at the HPUX Posix shell
>
> diffmsg=`scripts/cdiff.sh`
> print $diffmsg
>
> I get the echo output from that script.
>
> However, inside Perl, apparently once the shell system call closes,
> that environment variable is gone as $diffmsg never has anything in
> it.
>
> system "diffmsg=`scripts/cdiff.sh` ";
> $diffmsg = ($ENV{'diffmsg'});
>
> Am I correct in my findings?
>
> Any easy way to be able to condition the output from this shell script
> in perl? I'm hoping without having to open, read and close a file.
The following two shell scripts should answer your question.
#!/bin/sh
echo "script1"
./script2
echo "$diffmsg is not set because my child process has completed"
#!/bin/sh
echo "script2"
diffmsg='foo'
export diffmsg
------------------------------
Date: Sat, 4 Dec 2004 05:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 5.3: How do I count the number of lines in a file?
Message-Id: <corge5$ojn$1@reader1.panix.com>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
5.3: How do I count the number of lines in a file?
One fairly efficient way is to count newlines in the file. The following
program uses a feature of tr///, as documented in perlop. If your text
file doesn't end with a newline, then it's not really a proper text
file, so this may report one fewer line than you expect.
$lines = 0;
open(FILE, $filename) or die "Can't open `$filename': $!";
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/\n//);
}
close FILE;
This assumes no funny games with newline translations.
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
------------------------------
Date: Sat, 04 Dec 2004 13:08:15 +0800
From: Alont <end@dream.life>
Subject: how I can get more error info from virtual machine?
Message-Id: <41b1434a.19576875@news.individual.de>
My CGI script can running correctly on my own IIS machine, but when I
execute it from remote virtual machine(support Perl CGI script), it
can't work correctly(it be executed, but the result isn't correct),
and it don't give me any error infomation.
I've use:
use CGI::Carp qw(fatalsToBrowser);
but it still haven't any error information, how I can get more
infomation?
--
Your fault as a Government is My failure as a Citizen.
------------------------------
Date: Sat, 04 Dec 2004 00:31:11 GMT
From: Glenn <user@example.net>
Subject: installing dbd::mysql via cygwin on windows
Message-Id: <jz7sd.6230$ue2.745@news01.roc.ny>
Hi,
I'm having an issue installing the DBD::mysql; it's complaining about
not being able to find mysql_config.
I've seen some posts regarding installing mysql-devel.. I'm unsure what
this means.
I have installed MySQL in Program Files _prior_ to my install of
cygwin.. is this an issue in and of itself?
Is it possible for someone to provide a template mysql_config file that
I can plus in one of my cygwin dir's somewhere?
Thanks
Glenn
------------------------------
Date: 3 Dec 2004 19:23:34 -0800
From: awkster@yahoo.com (JRoot)
Subject: parse hash by array element seems to run slow
Message-Id: <9efa6216.0412031923.6a588454@posting.google.com>
Below I posted my program in it's entirity and I hope that is within
the guidelines ...
The program works perectly but it seems to run slower than I had
expected based on other perl programs I've authored with much more
data involved.
I've been through the faq's and every web site that focuses on perl
and to be honest, I'm overwhelmed by the number of ways it's possible
to use perl so I'm asking if anyone could just look over this program
and see if it is obvious where the overhead lies so I can remove the
baggage and speed it up a bit. It runs in about 3-5 seconds on a 1 ghz
machine but my gut tells me it could be faster or more efficient if
you will. I'm not asking anyone to do my work for me ... just for a
pointer from anyone that deals with arrays and hashes a lot.
The roadmap is ...
I create an array from a file (one word per line)
I create a hash from a 2nd file, combining duplicate key words and
assign the duplicate quantity as the value. A key-val pair would look
like ... net_name 5
I iterate the array and search the hash keys to find a match and then
report the key and val to different reports depending on the val
(quantity) which is either 0 (not found) 1 (single occurance) or
multiple which need to be verified.
I suspect I've arrayed and hashed one too many times when it could
have been done in a more simple manner.
Any help is appreciated -- thank you
snip<------ cut here -------->snip
#!/app/util/perl/bin/perl -w
BEGIN { push(@INC, "/app/util/perl5.6.1/lib/5.6.1") }
use strict;
my @nets = ();
my %count = ();
my %list = ();
my ($net, $x, $y, $netName, $side, $junk, $line, $checkHashVal);
my ($sortedNet, $tpList, $key, $val, $list, $checkHashKey, $found);
my ($oneArr, $verifyArr, $zeroArr, $line2Push);
my $lineCnt = 0;
my @tpList = ();
my @sortedNets = ();
my @zeroArr = ();
my @oneArr = ();
my @verifyArr = ();
my $cnt = 0;
print "\n\n... Working .... \n\n";
open (TL, "tp_list") || die "Can't open tp_list for reading: $!";
while(<TL>){
chomp($_);
push(@tpList, $_);
}
close(TL);
open (TC, "testpoint_chart") || die "Can't open testpoint_chart for
reading: $!";
while(<TC>){
$line = $_;
++$lineCnt;
if($lineCnt > 9){
($x, $y, $netName, $side, $junk) = split(' ', $line, 5);
if($side eq "Top" || $side eq "Bottom"){
push(@nets, $netName);
}
}
}
close(TC);
@sortedNets = @nets;
foreach $sortedNet(@sortedNets) {
$count{$sortedNet} = ++$count{$sortedNet};
}
foreach $sortedNet (keys %count) {
($key, $val) = ($sortedNet, $count{$sortedNet});
$list{$key} = $val;
}
foreach $tpList (@tpList) {
$found = "no";
++$cnt;
foreach $key (keys %list) {
if ($key eq $tpList) {
$found = "yes";
$line2Push = "ARR: $key -- HASHKEY: $tpList VAL:
$list{$tpList}\n";
if($list{$tpList} eq "1"){
push(@oneArr, $line2Push);
}
elsif($list{$tpList} ne "1"){
push(@verifyArr, $line2Push);
}
last;
}
}
if($found eq "no"){
push(@zeroArr, $tpList);
}
}
print "\n===== ONES =====\n";
print "------------------\n";
foreach $oneArr(@oneArr){print "ONES: $oneArr";}
print "\n===== VERIFY =====\n";
print "--------------------\n";
foreach $verifyArr(@verifyArr){print "VERIFY: $verifyArr";}
print "\n===== ZERO =====\n";
print "--------------------\n";
foreach $zeroArr(@zeroArr){print "ZERO: $zeroArr\n";}
print " ... D O N e ... \n\n";
------------------------------
Date: Sat, 04 Dec 2004 05:22:12 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: parse hash by array element seems to run slow
Message-Id: <x7brdazlh7.fsf@mail.sysarch.com>
>>>>> "J" == JRoot <awkster@yahoo.com> writes:
J> BEGIN { push(@INC, "/app/util/perl5.6.1/lib/5.6.1") }
use lib "/app/util/perl5.6.1/lib/5.6.1" ;
J> use strict;
J> my @nets = ();
J> my %count = ();
J> my %list = ();
J> my ($net, $x, $y, $netName, $side, $junk, $line, $checkHashVal);
J> my ($sortedNet, $tpList, $key, $val, $list, $checkHashKey, $found);
J> my ($oneArr, $verifyArr, $zeroArr, $line2Push);
J> my $lineCnt = 0;
J> my @tpList = ();
J> my @sortedNets = ();
J> my @zeroArr = ();
J> my @oneArr = ();
J> my @verifyArr = ();
J> my $cnt = 0;
don't declare variables until you need them. and there is no need to
initialize the arrays and hashes to () as they start out empty
J> open (TL, "tp_list") || die "Can't open tp_list for reading: $!";
J> while(<TL>){
J> chomp($_);
J> push(@tpList, $_);
J> }
J> close(TL);
use File::Slurp ;
chomp( my @tp_list = read_file( 'tp_list' ) ) ;
style point: use _ and not studly caps for names.
J> open (TC, "testpoint_chart") || die "Can't open testpoint_chart for
J> reading: $!";
J> while(<TC>){
J> $line = $_;
needless extra copy. either work with $_ or assign to $line in the
while:
while( my $line = <TC> ) {
J> ++$lineCnt;
J> if($lineCnt > 9){
J> ($x, $y, $netName, $side, $junk) = split(' ', $line, 5);
J> if($side eq "Top" || $side eq "Bottom"){
J> push(@nets, $netName);
J> }
J> }
J> }
i don't want to get into that logic but i bet you could slurp that in
also and then process the lines.
J> close(TC);
J> @sortedNets = @nets;
why this copy? i see no reason for it. if this is a large list it will
slow you down.
J> foreach $sortedNet(@sortedNets) {
J> $count{$sortedNet} = ++$count{$sortedNet};
J> }
huh???? why bump a hash element and then assign it to ITSELF?
$count{$_}++ for @sortedNets ;
J> foreach $sortedNet (keys %count) {
J> ($key, $val) = ($sortedNet, $count{$sortedNet});
J> $list{$key} = $val;
J> }
needless use of temp variables
foreach my $sorted_net (keys %count) {
$list{$sortedNet} = $count{$sortedNet} ;
}
but that is just a copy of the hash. is that what you really want?
you seem to do lots of extra and needless copying. that will kill any
program which handles large amounts of data.
J> foreach $tpList (@tpList) {
J> $found = "no";
J> ++$cnt;
J> foreach $key (keys %list) {
J> if ($key eq $tpList) {
J> $found = "yes";
J> $line2Push = "ARR: $key -- HASHKEY: $tpList VAL:
J> $list{$tpList}\n";
J> if($list{$tpList} eq "1"){
J> push(@oneArr, $line2Push);
J> }
J> elsif($list{$tpList} ne "1"){
J> push(@verifyArr, $line2Push);
J> }
J> last;
J> }
J> }
J> if($found eq "no"){
J> push(@zeroArr, $tpList);
J> }
instead of flag variables ($found) just do a next or last as
needed. flag variables are fortran, not perl. i am too fried to analyze
the logic in that loop so i can't optimize it now but i sense it can be
easily be done. i leave that for you and other posters.
J> print " ... D O N e ... \n\n";
well, the prompt coming back from the shell should be fine for that,
even though it isn't a cpu waster.
so your code does plenty of needless copies and slow loops. plenty of
room for speedups.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Sat, 04 Dec 2004 11:46:03 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Perl / MySql / Shopping cart
Message-Id: <31cj6rF39ff6hU1@individual.net>
Tony M M wrote:
> "Gregory Toomey" <nospam@bigpond.com> wrote in message
> news:31a25qF39i01hU1@individual.net...
>> Tony M M wrote:
>> > Has anyone on this message group ever thought of modifying, or building
> a
>> > shopping cart that is real world and usable then putting it out to the
>> > populous?
>> > What I see is bloated, non working perl scripts that are missing the
> mark
>> > in the area.
>> There are a few ones like http://www.ratite.com/Perl/WebStore.shtml
>> However most LAMP shopping cards tend to be php eg oscommerce
>
> oscommerce is bloated beyond belief (imo). It does so many quiries that it
> puts quite a workload on the server. But beyond being slow on our server
> it is too cookie cutter. Go to one oscommerce site and you have seen most.
> That is one of the reasons I started this thread. The digitalpressworks
> cart (the one I refered to as a good starting point) is database driven
> (autogenerated) or static page (or both). This allows one shop to look
> different from another. It is simple in its design (even I can modify it
> easliy). But they seem to have stopped development on it. (Again in my
> opinion - it needs a database backend, and real-time cc processing and it
> would be pretty complete).
>
>> > I asked this question after looking at perl shopping carts for the last
> 6
>> > months and seeing what I indicated above.... If this has been tackled
> in
>> > the past could anyone point me to the thread? If not, is this the type
> of
>> > newsgroup that participates like this?
>> >
>> > Thank you,
>> >
>> > Tony <Mike> M.
>>
>> I didnt like anything I saw so I rolled my own.
>> It took me about a month to write Phase 1 of www.pchq.com.au in
> Perl/mysql,
>> complete with an interface to a wholesaler to automatically update
>> prices. I added a Perl html whitespace remover and installed a Squid
>> reverse proxy so hopefully the site loads fast. Graphics are done with
>> ImageMagick. Phase 2 will add checkout/credit card payments.
>> gtoomey
>
> I went to the site you listed. Did I miss the cart? I didn't see a place
> to put anything in as an order.
>
Its "brochureware" at the moment.
I'm working on the shopping cart/shipping/credit card interface.
> That is what I am talking about though... One month to build phase one (if
> I knew perl better I would attempt it but I can just test/hack/trail and
> error modify). The hard part is (imo) is phase 2 - the return information
> from the payment processor in the background, update the database, send
> confirmation e-mails, and save the order.
>
> If this newsgroup does projects it would be great. Maybe an old concept -
> but used to do it (on old BBS's) back in the old basic/pascal days and we
> made some pretty good useful freeware and with perl being an open type of
> language it seem to be in the spirit.
>
> Tony <Mike> M
There seem to be plenty of shopping carts already. But if you want specific
featureyou may need to build it yourself.
gtoomey
------------------------------
Date: Sat, 04 Dec 2004 03:14:22 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Perl Books
Message-Id: <x71xe621rm.fsf@mail.sysarch.com>
>>>>> "BVA" == Binny V A <binnyva@hotmail.com> writes:
BVA> Anyway, thanks for the feedback. I will try to polish and then
BVA> publish the tutorial. If some of you could help me by proofreading
BVA> and by posting your suggetions, I would be very grateful.
my main suggestion is that you don't try to write a tutorial at all. it
is much more difficult than you realize especially if you are not
writing it in your native language. it is admirable that you tried and
you will actually get more respect from me if you stop working on
it. you have so much perl to learn before you could even think about
doing this well. even people who know perl very well have trouble
writing books and tutorials. technical writing is a very difficult skill
to master.
BVA> One good thing is that I have learnt the meaning of the pharse
BVA> "If you really want to make an impression about something,
BVA> do it wrong." ;-)
and what makes you think that you can do it right? and when? not any
time soon IMO. please just drop it for your sake and for those who may
stumble upon your page. it is better to have no tutorial than one full
of mistakes. learn the lesson of matt's scripts where he was first and
too many kiddies copied his broken code for many years.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Sat, 04 Dec 2004 01:26:36 GMT
From: "Steve Butler" <jodox@sbcglobal.net>
Subject: Perl crashes
Message-Id: <gn8sd.38110$6q2.16464@newssvr14.news.prodigy.com>
Hi all,
I have not posted here before. I am compelled to ask about this code snipet
crashing perl.exe when attempting a perl -c:
#!/usr/bin/perl -w
use strict;
sub &total {
my $sum;
foreach (@_){
sum+=$_;
}
Basically I know the code is bad, but I never expected perl to crash on a
syntax check. Should I send this bug somewhere or just not write bad code?
Thanks!
Steve
------------------------------
Date: 4 Dec 2004 05:03:04 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Perl crashes
Message-Id: <Xns95B58663325asu1cornelledu@132.236.56.8>
"Steve Butler" <jodox@sbcglobal.net> wrote in
news:gn8sd.38110$6q2.16464@newssvr14.news.prodigy.com:
> I have not posted here before. I am compelled to ask about this code
> snipet crashing perl.exe when attempting a perl -c:
>
> #!/usr/bin/perl -w
> use strict;
>
> sub &total {
> my $sum;
> foreach (@_){
> sum+=$_;
> }
>
> Basically I know the code is bad, but I never expected perl to crash
> on a syntax check. Should I send this bug somewhere or just not write
> bad code?
C:\Home\asu1> perl -v
This is perl, v5.8.4 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)
...
C:\Home\asu1> perl -c t9.pl
Illegal declaration of anonymous subroutine at t9.pl line 4.
Sinan.
--
A. Sinan Unur
1usa@llenroc.ude.invalid
(remove '.invalid' and reverse each component for email address)
------------------------------
Date: Sat, 04 Dec 2004 05:18:30 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Perl crashes
Message-Id: <GMbsd.590$FW1.558@trnddc08>
Steve Butler wrote:
> I have not posted here before. I am compelled to ask about this code
> snipet crashing perl.exe when attempting a perl -c:
>
> #!/usr/bin/perl -w
> use strict;
>
> sub &total {
> my $sum;
> foreach (@_){
> sum+=$_;
> }
>
> Basically I know the code is bad, but I never expected perl to crash
> on a syntax check. Should I send this bug somewhere or just not write
> bad code?
Good catch. Does it repro with the latest perl version?
It does on my machine, but I am still running 5.6.1 which is a bit outdated
and definitely not the right reference platform.
If this bug is repro in the latest version, then see "perldoc perlbug".
jue
------------------------------
Date: Fri, 03 Dec 2004 23:27:28 GMT
From: Jim Keenan <jkeen_via_google@yahoo.com>
Subject: Re: print FILE truncating input
Message-Id: <AD6sd.60$Ye3.15@trndny02>
Spin wrote:
> And this is the sub of interest:
>
> sub get_lab_header {
> # print "1. ",$_[0];
> my $data = $_[0];
> # my $data = unpack("a16"x(length($_[0])/16)."a*",$_[0]);
> # print "2. ",$data;
> # $data =~ tr/\0-\37\177-\377/./;
> # print "3. ",$data;
> my $visit_id = substr($data,22,20);# get/create Visit ID
> $visit_id =~ s/^\s+//; # strip whitespace off each end
> $visit_id =~ s/\s+$//;
> $visit_id = printf("%-10s", $visit_id); # pad string (right justified)
At this point you are printing to standard output.
> my $header_string = $visit_id; # start header string
> my $req_id = substr($data,124,7);
> $req_id =~ s/^\s+//;
> $req_id =~ s/\s+$//;
> $req_id = printf("%-5s", $req_id);
And at this point as well. You probably wanted 'sprintf'.
perldoc -f sprintf
perldoc -f printf
> $header_string .= $req_id;
> my $date = "20".substr($data,13,2);
> $date .= substr($data,11,2);
> $date .= substr($data,9,2);
> $header_string .= $date;
> my $time = substr($data,15,2).":";
> $time .= substr($data,17,2).":";
> $time .= substr($data,19,2);
> $header_string .= $time;
> return $header_string;
> }
The code above is probably unnecessarily verbose, but without sample
input data I can't pinpoint desirable revisions.
Jim Keenan
------------------------------
Date: Sat, 04 Dec 2004 01:08:16 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: print FILE truncating input
Message-Id: <31ce1uF35ccd3U1@individual.net>
Spin wrote:
> When including "FILE" in the print statement the output to screen is:
>
> Result Type: C
> BLOATY REQID
>
> And to file:
>
> 112004120316:56:42
>
> Conversely, when not including "FILE", this prints to screen:
>
> Result Type: C
> BLOATY REQID112004120316:58:50
>
> And nothing prints to file.
>
> What do I need to change to have
>
> BLOATY REQID112004120316:58:50
>
> written to the file?
Try:
print FILE ( get_lab_header($loc), "\n");
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 03 Dec 2004 23:44:56 GMT
From: Jim Keenan <jkeen_via_google@yahoo.com>
Subject: Re: Question on loops and return values
Message-Id: <YT6sd.61$Ye3.13@trndny02>
delfuego wrote:
> I have a script below that is supposed to read in a list of microsoft
> servers, and output the disk space (current and last), dates
> (beginning and ending), and compute space growth in value and
> percentage; to output file. The question is this: how do I get the
> correct looping going on in my for loops (one is not getting the
> machines, the other is not getting the drive letters; as well as any
> other problems you can point out. The files are listed below the
> scriipt, although I plan to write out two additional columns, growth
> value and growth rate.
> ALL help is appreciated.
> Thanks,
>
> James
Am I correct in suspecting that this is a script that someone else
originally wrote and you have to fix? There's a lot of
less-than-optimal code below (e.g., use of 'our' where 'my' would almost
certainly suffice).
>
> <script beow inline>
>
> use lib "$ENV(HOME)/site/lib"; # occasionally perl whines about not
> in a lib
> no lib ".";
> use warnings;
> use Time::localtime; # needed for tm and using that to get dates
> use Time::tm;
> use Win32API::Resources; # needed for drives and disk space
>
> our @drive = Win32API::Resources::GetDrives();
> our %space = Win32API::Resources::GetDriveSpace("$let:\\");
Here a USENETtiquette problem arises. In order for us to troubleshoot
your code, we need to be on Windows, because the Win32API modules only
work on that OS. If your problem is a Windows-specific problem, then
you should probably post it on a Windows-specific list such as
perl-win32-users@activestate.com. But I'm not reading this on Windows,
so I can't test your code. My hunch is that your problem is *not*
Windows-specific, but is located in how to read directory and file
information.
Wherever you post, you should try to post the smallest possible amount
of code needed to illustrate the problem. Most of the people who read
this list don't have enough time to work through all of it --
particularly if it requires a particular OS. If you try to post the
smallest possible amount you will, in the process, probably locate your
error yourself.
> our $file = (<SD>,"growth.cvs"); # output data file
> our $file2 = (<SD>,"machines.cvs"); # input data file
> our $let = ['A-Z']; # character value for drive letters
[snip balance]
Jim Keenan
------------------------------
Date: Fri, 3 Dec 2004 20:22:18 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Question on loops and return values
Message-Id: <slrncr27qp.2ob.tadmc@magna.augustmail.com>
delfuego <djcameron60616@yahoo.com> wrote:
><script beow inline>
Please show *real code*!
> use lib "$ENV(HOME)/site/lib"; # occasionally perl whines about not
^ ^
^ ^ {HOME}
> our $file = (<SD>,"growth.cvs"); # output data file
> our $file2 = (<SD>,"machines.cvs"); # input data file
Where is the SD filehandle open()ed?
Why are you using package variables instead of lexical variables?
> our $let = ['A-Z']; # character value for drive letters
You never make use of this variable, and it almost certainly
is not what you think it is.
It is a reference to an anonymous array with 1 element in it.
> our @line = ""; # was going to be used for file looping
How may elements are you expecting to be in @line right here?
Were you expecting zero elements, an empty array?
That isn't what you are getting there...
> open (file2,"+<"); # open machines input file for read
You are opening for read AND write, the comment is a trick!
Did you mean to put a filename in there somewhere?
You should use UPPER CASE for filehandles.
You should always, yes *always*, check the return value from open().
> while <file2> # loop while not eof in machines.cvs
Syntax error.
Show *real code*!
> foreach $machine (<file2>)
You are reading from the _same_ filehandle in 2 different places
in your code. Is that what you want to do?
The while loop (if it compiled) would only execute one time.
The foreach provides list context, so it will slurp to the end of file.
> chop ($last);
That is how we removed newlines 8 years ago. Where did you learn that?
Nowadays you should use chomp() to remove newlines.
> $curr = %DRVSpace;
This most certainly does not do what you think it does.
The value of a hash in scalar context is not of interest to
a Perl programmer, it is only of interest to a perl programmer.
> $last = eval { $curr / 5 }; # bogus value for testing, should be
> $gvalue = eval { $curr - $last }; # growth rate difference
> $grate = eval { ($gvalue / $last) * 100 }; # growth rate percentage
Can you tell us why you are using eval() there?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 04 Dec 2004 05:32:25 GMT
From: "DeepDiver" <no-spam@sonic.net>
Subject: RegEx Help Needed
Message-Id: <JZbsd.9270$_3.108493@typhoon.sonic.net>
I'm trying to parse a string of HTML that contains a mix of tags and text.
My goal is to match and replace double quote marks in the text (but not
within the tags) and replace them with the equivalent html character entity
(i.e., ").
For example, this string:
The "slow" red fox.<div class="test">The "quick" brown fox.</div>
would become this:
The "slow" red fox.<div class="test">The "quick"
brown fox.</div>
TIA!!!
------------------------------
Date: Sat, 04 Dec 2004 00:49:14 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: RegEx Help Needed
Message-Id: <SOydnYD65MRH0izcRVn-tg@adelphia.com>
DeepDiver wrote:
> I'm trying to parse a string of HTML
Have a look at HTML::Parser on CPAN.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Sat, 04 Dec 2004 06:55:03 GMT
From: "DeepDiver" <no-spam@sonic.net>
Subject: Re: RegEx Help Needed
Message-Id: <bbdsd.9312$_3.108687@typhoon.sonic.net>
"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:SOydnYD65MRH0izcRVn-tg@adelphia.com...
>
> Have a look at HTML::Parser on CPAN.
>
Thanks, but I'm in need of a pure RegEx solution.
------------------------------
Date: Sat, 04 Dec 2004 01:23:53 -0600
From: Lars Eighner <eighner@io.com>
Subject: Re: RegEx Help Needed
Message-Id: <slrncr2p9p.1br1.eighner@goodwill.io.com>
In our last episode, <JZbsd.9270$_3.108493@typhoon.sonic.net>, the lovely
and talented DeepDiver broadcast on comp.lang.perl.misc:
> I'm trying to parse a string of HTML that contains a mix of tags and text.
> My goal is to match and replace double quote marks in the text (but not
> within the tags) and replace them with the equivalent html character entity
> (i.e., ").
> For example, this string:
> The "slow" red fox.<div class="test">The "quick" brown fox.</div>
> would become this:
> The "slow" red fox.<div class="test">The "quick"
> brown fox.</div>
> TIA!!!
I can't do it in one, but --
WARNING! Those offended by brute force ugliness should look away now!
WARNING!
goodwill~/test$perl -wpi -e '$/=undef;while( s/\"([^<>]*<)/"\;$1/g ){}
;' test.html
This won't work if you have unbalanced <s and/or > anywhere in the
document such as a script with something like document.write("<")
or simply unclosed tags. If you actually run this as a one-liner,
beware of what your shell may do with $1 if you double quote the
executable.
--
Lars Eighner -finger for geek code- eighner@io.com http://www.io.com/~eighner/
War on Terrorism: Camp Follower
"I am ... a total sucker for the guys ... with all the ribbons on and stuff,
and they say it's true and I'm ready to believe it. -Cokie Roberts,_ABC_
------------------------------
Date: Sat, 4 Dec 2004 07:28:28 +0000 (UTC)
From: "David H. Adler" <dha@panix.com>
Subject: Re: RegEx Help Needed
Message-Id: <slrncr2pos.j2i.dha@panix2.panix.com>
On 2004-12-04, DeepDiver <no-spam@sonic.net> wrote:
> "Sherm Pendley" <spamtrap@dot-app.org> wrote in message
> news:SOydnYD65MRH0izcRVn-tg@adelphia.com...
>>
>> Have a look at HTML::Parser on CPAN.
>>
>
> Thanks, but I'm in need of a pure RegEx solution.
This of course raises the question: Why?
We can probably help you better if we have some idea of why you reject
the generally accepted solution...
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
[Insert Angus Prune Tune here]
------------------------------
Date: Sat, 04 Dec 2004 08:03:17 GMT
From: "DeepDiver" <no-spam@sonic.net>
Subject: Re: RegEx Help Needed
Message-Id: <9besd.9328$_3.108813@typhoon.sonic.net>
"David H. Adler" <dha@panix.com> wrote in message
news:slrncr2pos.j2i.dha@panix2.panix.com...
> On 2004-12-04, DeepDiver <no-spam@sonic.net> wrote:
> > "Sherm Pendley" <spamtrap@dot-app.org> wrote in message
> > news:SOydnYD65MRH0izcRVn-tg@adelphia.com...
> >>
> >> Have a look at HTML::Parser on CPAN.
> >>
> >
> > Thanks, but I'm in need of a pure RegEx solution.
>
> This of course raises the question: Why?
A few reasons:
1. I'm not programming in Perl. In fact, my experience with Perl was a long
time ago (and not very extensive even then). I came here because I believe
that Perl programmers are generally the most proficient with regular
expressions.
2. I'm writing the current routine in C#. But I would still prefer a "pure"
RegEx solution so that I have something that is concise and (higher-level)
language independent.
3. I'm trying to improve my RegEx skills, so the more I can learn how to do
things like this in RegEx (without "massaging" in a higher-level language)
the better.
I hope this addresses your concerns.
Thanks,
Michael
------------------------------
Date: 3 Dec 2004 17:11:10 -0800
From: jingchai@gmail.com (Jing)
Subject: write error message to a log file while processing the ftp command
Message-Id: <815c0a36.0412031711.7a5578a7@posting.google.com>
The system admin. has a problem to configure the Makefile.pl for
Net::FTP module. my program need to ftp a local files to a remote
server. Since i can't use Net::FTP, i try to use ftp command instead.
I got trouble to write error message to a log file while processing
the ftp command. Here is my code for ftp files.
#!/opt/perl/bin/perl -w
use File::Copy;
$remotehost1 ="a";
$remotepath ="/b";
$remoteuser ="c";
$remotepass ="d";
$fileftp = "test.txt";
$dirfrom="/e";
$cmd="ftp -n";
my $ftp_commands =
" open $remotehost1
user $remoteuser $remotepass
lcd $dirfrom
cd $remotepath
asc
put $fileftp
bye
";
open (CMD, "|$cmd");
print CMD $ftp_commands;
close (CMD);
print "Ftp commands : $ftp_commands";
print "File $fileftp has been transferred \n";
$finish = 'temp';
copy ("$fileftp","./$finish/$fileftp");
thanks
------------------------------
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 7482
***************************************