[21967] in Perl-Users-Digest
Perl-Users Digest, Issue: 4189 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 27 11:11:14 2002
Date: Wed, 27 Nov 2002 08:10:16 -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 Wed, 27 Nov 2002 Volume: 10 Number: 4189
Today's topics:
Re: rounding numbers <jurgenex@hotmail.com>
Re: rounding numbers (Tad McClellan)
Re: rounding numbers (Helgi Briem)
scripts run by cron.sh script can't open files <tomweeks@charter.net>
Re: SQLite help please <bobx@linuxmail.org>
trigger internal iis 5.0 services <clandos@web.de>
Re: trigger internal iis 5.0 services (Helgi Briem)
Re: trigger internal iis 5.0 services <bernard.el-hagin@DODGE_THISlido-tech.net>
Win32::OLE and excel? <spikey-wan@bigfoot.com>
Re: Wow! Differences between "unless" and if (!) (Jay Tilton)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 27 Nov 2002 15:29:30 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: rounding numbers
Message-Id: <ux5F9.5156$Sb.4938@nwrddc04.gnilink.net>
Kevin Brownhill wrote:
> But it is sometimes wrong, eg.
> sprintf("%.2f", 4.005)
> returns "4.00"
> when the correct answer is "4.01"
Really? You may want to double check.
A '5' is rounded down if
- this 5 is the last non-zero digit in the number or
- is the result of a previous rounding up
A '5' is rounded up if
- this '5' is not the last non-zero digit (i.e if there are more digits 1-9
coming after the 5) or
- if the 5 is the result of a previous rounding down
Of course this has little relevance in programming because
- programs don't keep track if or how a number was rounded before
- real numbers have no exact representation to begin with (unless you are
using special packages for symbolic calculations).
And because of the last fact you don't even know if your nice 4.005 is
stored internally as an ugly 4.0049999999999999999 or
4.0050000111111111111111111. Now, how do you round that?
Real numbers and accuracy is a contradition in terms.
> I am not re-answering any FAQ, I am answering the original question
> which was "does anyone know how to round numbers with lots of
> decimal places to a certain amount. eg 2 decimal places or 0 decimal
> places"
>
> The question others seem to be answering is "how do you print a string
> representation of a number rounded to n places". This may be what is
> really wanted, but is not the question asked.
Well, Perl does not know about numbers or strings. It knows about scalars.
If you got a string then got a number. It's all the same in Perl.
jue
------------------------------
Date: Wed, 27 Nov 2002 09:03:10 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: rounding numbers
Message-Id: <slrnau9nld.2la.tadmc@magna.augustmail.com>
Kevin Brownhill <BROWNHIK@Syntegra.Bt.Co.Uk> wrote:
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnau7cc7.47d.tadmc@magna.augustmail.com...
>> Kevin Brownhill <BROWNHIK@Syntegra.Bt.Co.Uk> wrote:
>> > "Andras Malatinszky" <nobody@dev.null> wrote in message
>> > news:3DE389F8.5070805@dev.null...
>> >> RC wrote:
>> >>
>> >> > does anyone know how to round numbers
>> >> I'd explain how, but it is so much more
>> >> eloquently done in the perlfaq4 manpage.
>
> Is that sarcasm or just plain unhelpfulness?
FAQ answers are written by experts, reviewed by experts and
then seen by thousands of people.
If there is a mistake in there, it gets discovered and repaired.
The answer you get from a single person in a newsgroup is
much more likely to contain a mistake, only one person
(who is very close to it) has reviewed it.
Referring someone to a peer-validated answer is more helpful
than trying to help directly and then happening to make a mistake.
Mistakes happen, we are human. They are to be expected.
We can catch many of them with widespread review by others.
It appears that your followup was both of the choices proffered above...
>> perldoc -f sprintf
>>
>> it clearly says the sprintf can round floating point numbers.
>
> it clearly says that it returns a string which is a series of characters - a
> rounded number is a number which is a binary representaion of a decimal
> number.
If you then use the stringified scalar as a number, perl
will convert it into a "binary representaion of a decimal
number". Just what the OP, and you, want to get!
You could even force the conversion to happen right away,
though I don't see that that is very useful, because the
"difference between a number and a string" is nearly
always inconsequential in Perl.
$rounded = + sprintf "%.2f", $number;
>> sprintf() works for all numbers.
>>
>
> But it is sometimes wrong, eg.
> sprintf("%.2f", 4.005)
> returns "4.00"
Try this:
print sprintf("%25.20f", 4.005), "\n";
You should get output similar to:
4.00499999999999989342
> when the correct answer is "4.01"
So that isn't really the correct answer.
This is yet a different FAQ.
Why am I getting long decimals (eg, 19.9499999999999)
instead of the numbers I should be getting (eg, 19.95)?
The most important part of the answer is where it says:
This affects all computer languages that represent decimal
floating-point numbers in binary, not just Perl.
This is standard Computer Science fare.
>> Please do not try to reanswer a FAQ, you are likely to get it
>> wrong, as you did here.
>>
>> That increases confusion rather than decreasing it.
>>
>
> I am not re-answering any FAQ, I am answering the original question which
> was "does anyone know how to round numbers with lots of decimal places to a
> certain amount. eg 2 decimal places or 0 decimal places"
If you use the rounded number that you get from the FAQ's answer
as a number, then you _do_ get the rounded numeric representation.
So it _does_ answer the OP's question.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 27 Nov 2002 16:03:02 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: rounding numbers
Message-Id: <3de4a382.3656581255@news.cis.dfn.de>
On Wed, 27 Nov 2002 10:35:15 -0000, "Kevin Brownhill"
<BROWNHIK@Syntegra.Bt.Co.Uk> wrote:
>> sprintf() works for all numbers.
>>
>But it is sometimes wrong, eg.
No, it is not.
> sprintf("%.2f", 4.005)
>returns "4.00"
>when the correct answer is "4.01"
No it is not. See the" ANSI/IEEE Std 754-1985, IEEE
Standard for Binary Floating-Point Arithmetic, IEEE, New
York, 1985." for example:
http://www.psc.edu/general/software/packages/ieee/ieee.html
>I am not re-answering any FAQ, I am answering the original question which
>was "does anyone know how to round numbers with lots of decimal places to a
>certain amount. eg 2 decimal places or 0 decimal places"
But you are answering it incorrectly, which is *ungood*.
>The question others seem to be answering is "how do you print a string
>representation of a number rounded to n places". This may be what is really
>wanted, but is not the question asked.
>
>Has anyone read the phrase - "there's more than one way to do it"?
Yes, but in this case, there is a right way ( sprintf or
printf) and a wrong way (yours). Make the call.
--
Regards, Helgi Briem
helgi AT decode DOT is
A: Top posting
Q: What is the most irritating thing on Usenet?
- "Gordon" on apihna
------------------------------
Date: Wed, 27 Nov 2002 10:41:31 -0800
From: "Tom Weeks" <tomweeks@charter.net>
Subject: scripts run by cron.sh script can't open files
Message-Id: <uu9prap3qu0kf0@corp.supernews.com>
All:
This is a long post, but I hope someone will take the time to read it and
can help me. I am getting desperate.
I have files on a site I maintain that need to be backed up every night. The
web host has set up a cron job to run every night. Using their template I
wrote the following shell script (cron.sh):
_____________________________
#!/bin/sh
/u/web/(our_username)/cgi-local/artwork_daily_backup.pl
_______________________________
artwork_daily_backup.pl resides in /cgi-local/. Here is the code:
_____________________________
#!/usr/local/bin/perl
open W, "$ENV{DOCUMENT_ROOT}/artwork_database/workorders/artindex.dat";
flock (W,2);
seek (W,0,0);
@wbak = <W>;
close W;
open WOBAK,
">$ENV{DOCUMENT_ROOT}/artwork_database/workorders/workorders_daily_backup.da
t";
flock (WOBAK,2);
seek (WOBAK,0,0);
print WOBAK @wbak;
close WOBAK;
open M, "$ENV{DOCUMENT_ROOT}/artwork_database/master/artindex.dat";
flock (M,2);
seek (M,0,0);
@mbak = <M>;
close M;
open MBAK,
">$ENV{DOCUMENT_ROOT}/artwork_database/master/master_daily_backup.dat";
flock (MBAK,2);
seek (MBAK,0,0);
print MBAK @mbak;
close MBAK;
print "\n\n";
___________________________________
According to tech support cron.sh is running every night as scheduled, but
the backup script is not doing its job, and there are no entries in the
error log for the site. When I run cron.sh from my browser, the files are
updated just as they should be, but cron.sh returns a "premature end of
scriipt headers" error. (I would have expected a "malformed header" error).
To check that the cron daemon was actually calling cron.sh, I wrote the
following test script (crontest.pl) which writes to a .dat file if
successful, and writes returned HTML to cronlog.log.
______________________________________
#!/usr/local/bin/perl
if (!open T, ">>$ENV{DOCUMENT_ROOT}/crontest.dat"){
print "Content-type: text/html\n\n";
print "Couldn't Open File!\n";
exit;
}
print T "Cron Ran".`date`."\n";
close T;
print "Content-type: text/html\n\n";
print "done ".`date`."\n";
___________________________________________
I rewrote cron.sh as follows:
__________________________________
#!/bin/sh
/u/web/(our_usename)/cgi-local/crontest.pl
>>/u/web/(our_username)/cgi-local/cronlog.log
___________________________________
When I run this cron.sh from my browser, "Cron Ran (the date & time)" is
written to crontest.dat, and, "done (date & time)" is written to
cronlog.log. (The "premature end of script headers" error is written to the
site's errror.log file as expected--ostensibly because the shell script was
run from a browser and the shell script returned either no data or improper
CGI data).
When the cron daemon runs cron.sh my little error trap above writes
"Couldn't open file" to my cronlog.log file. Of course, nothing is written
to crontest.dat because it couldn't be opened. And that is the
question---WHY can crontest.pl open and write to crontest.dat when it is
called by cron.sh run by a browser, but NOT when it is called by cron.sh run
by my server's cron daemon? I have tried it using both
$ENV{DOCUMENT_ROOT}/etc. as the path to crontest.dat and the literal system
path (/u/web/etc./etc.). I believe if I figure out how to make this "test"
script work I will have figured out how to make the "real" backup script
work.
HELP!!
Webmaster / Webslave
------------------------------
Date: Wed, 27 Nov 2002 15:21:12 GMT
From: "Bob X" <bobx@linuxmail.org>
Subject: Re: SQLite help please
Message-Id: <Ip5F9.2984$ov6.1531542@news2.news.adelphia.net>
"Bart Lateur" <bart.lateur@pandora.be> wrote in message
news:3fu8uucqh89gq3p71d7d5ndqbd4r5df42v@4ax.com...
<snip>
> --
> Bart.
Thanks for joggin my noggin...
------------------------------
Date: Wed, 27 Nov 2002 13:42:39 +0100
From: "Carsten L." <clandos@web.de>
Subject: trigger internal iis 5.0 services
Message-Id: <3de4bdc0$0$191$4d4ebb8e@read.news.de.uu.net>
Hello!
I want to start and stop, resume "internal-web-site"s within the iis
service.
What I do now is to start and stop the W3SVC . This stops unfortenuately all
web sites at once.
Can anyone give me a hint a I can do this?
Thanks
Carsten
------------------------------
Date: Wed, 27 Nov 2002 13:00:11 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: trigger internal iis 5.0 services
Message-Id: <3de4c11b.3664159002@news.cis.dfn.de>
On Wed, 27 Nov 2002 13:42:39 +0100, "Carsten L."
<clandos@web.de> wrote:
>I want to start and stop, resume "internal-web-site"s within
>the iis ervice.
>What I do now is to start and stop the W3SVC . This stops
>unfortenuately all eb sites at once.
Why on earth are you posting this to a Perl newsgroup ?!?
>Can anyone give me a hint a I can do this?
Try a web server newsgroup or the fine manual.
I suggest the newsgroup
comp.infosystems.www.servers.ms-windows
or MS's own IIS web page:
www.microsoft.com/windows2000/community/centers/iis/default.asp
--
Regards, Helgi Briem
helgi AT decode DOT is
A: Top posting
Q: What is the most irritating thing on Usenet?
- "Gordon" on apihna
------------------------------
Date: Wed, 27 Nov 2002 13:08:42 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: trigger internal iis 5.0 services
Message-Id: <as2g4q$c95$1@korweta.task.gda.pl>
In article <3de4bdc0$0$191$4d4ebb8e@read.news.de.uu.net>, Carsten L.
wrote:
> Hello!
Howdy!
> I want to start and stop, resume "internal-web-site"s within the iis
> service.
Good for you. Everyone should have a hobby.
> What I do now is to start and stop the W3SVC.
Cool.
> This stops unfortenuately all web sites at once.
Bummer.
> Can anyone give me a hint a I can do this?
I'm sure there are many ones that can give you a hint a you can do
this.
Hope that helps.
Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'
------------------------------
Date: Wed, 27 Nov 2002 11:59:51 -0000
From: "Richard S Beckett" <spikey-wan@bigfoot.com>
Subject: Win32::OLE and excel?
Message-Id: <as2c54$kql$1@newshost.mot.com>
Chaps,
I'm using win32, active state perl, and Win32::OLE. I start Excel, and use
it to modify and create spreadsheets (As you would expect!).
I'm trying to trap error conditions, and in order to do this I need to know
if Excel is running, and what spreadsheets are open. Are there any commands
that will give me this information?
Thanks.
R.
------------------------------
Date: Wed, 27 Nov 2002 12:05:29 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Wow! Differences between "unless" and if (!)
Message-Id: <3de4b0d9.236250070@news.erols.com>
Arvin Portlock <apollock11@hotmail.com> wrote:
: I always knew you were supposed to have only a single return
: in a subroutine
Feh. Being able to put multiple exit points in a sub is extremely
useful.
: and that it should be last.
That's less arguable. An explicit return of some default value at the
end would have prevented what you experienced, but there's no need to
jump through hoops just to have only one return statement.
: Now I know why.
: Something like "$return = $param2 unless $param1; return $return;"
: would probably work.
:
: This also explains why the "unless" script works if I add an
: } else { return 0; } to it.
or even
return $param2 unless $param1;
return 0;
------------------------------
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 4189
***************************************