[24903] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7153 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 17 14:07:12 2004

Date: Fri, 17 Sep 2004 11:05:11 -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, 17 Sep 2004     Volume: 10 Number: 7153

Today's topics:
        "getgrent" call in kernel 2.6.x /perl 5.8.3 ? <me@privacy.net>
    Re: $| (undocumented) magic? <bik.mido@tiscalinet.it>
    Re: $| (undocumented) magic? <bik.mido@tiscalinet.it>
        anonymous hash notation question (wana)
    Re: anonymous hash notation question <noreply@gunnar.cc>
        cron manipulation using perl (siddhartha mulpuru)
        Help with a program (Theo James)
    Re: hv_iterinit has side effects - who cares about PL t (Ozgun Erdogan)
    Re: hv_iterinit has side effects - who cares about PL t <joe+usenet@sunstarsys.com>
    Re: Is my algorithm wrong? (Anno Siegel)
    Re: Need more efficient use of the substitution operato <1usa@llenroc.ude.invalid>
    Re: Need more efficient use of the substitution operato (Niall Macpherson)
    Re: Need more efficient use of the substitution operato <noreply@gunnar.cc>
    Re: Protecting passwords in Perl scripts? <bik.mido@tiscalinet.it>
    Re: Rounding error in program <spamtrap@dot-app.org>
    Re: Rounding error in program <HelgiBriem_1@hotmail.com>
    Re: Rounding error in program <mritty@gmail.com>
    Re: Rounding error in program <shawn.corey@sympatico.ca>
    Re: Rounding error in program <HelgiBriem_1@hotmail.com>
    Re: Rounding error in program <1usa@llenroc.ude.invalid>
    Re: Rounding error in program <HelgiBriem_1@hotmail.com>
    Re: Rounding error in program <pinyaj@rpi.edu>
    Re: Rounding error in program <1usa@llenroc.ude.invalid>
    Re: script portability problem (Larry Felton Johnson)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 17 Sep 2004 18:09:47 +0200
From: Michael Faltmann <me@privacy.net>
Subject: "getgrent" call in kernel 2.6.x /perl 5.8.3 ?
Message-Id: <414b0c4b$0$26105$9b4e6d93@newsread4.arcor-online.net>

Can somebody help?Hello NG,

I have a problem here with a perl script which make use of the "getgrent"
system call. 
This script has been working perfectly under various kernel revisions,
starting with 2.2.x. After upgrading to 2.6.4 in conjunction with system
update suse9.0 -->suse9.1, however, it fails.

It have the idea I has to with the length of the longest line in "group"
which is some 5000 Bytes long (1500 users approx.) . When I shorten this
line to around 3000 Bytes, the script seems to work.

System: suse9.1
kernel: 2.6.4 / 2.6.5
perl  : 5.8.3



The script lines of interest:
-------------------
setgrent;
while (@groups=getgrent) {
$grname{$groups[2]}=$groups[0];
$grmitglieder{$groups[0]}=$groups[3];
$grgid{$groups[0]} = $groups[2];
}

the errormessage simply reads "Out of memory!"


Can somebody help?
MF



------------------------------

Date: Fri, 17 Sep 2004 15:53:36 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: $| (undocumented) magic?
Message-Id: <ttnlk0l3eqfti0evlo4qchncfl3fm9uvg5@4ax.com>

On Thu, 16 Sep 2004 15:47:54 -0600, Eric Schwartz <emschwar@pobox.com>
wrote:

>> It seems you don't waste your time with JAPHs, because you only spend
>> your efforts in "productive code", but just at the same time this must
>> have caused you some comprehension problems e.g. with understanding
>> what the OP was talking about, i.e. *Python* programs, not *Perl*.
>
>I believe the OP was simply pointing out that you can only write
>*some* programs in four lines; you can't write *any* program in 4
>lines, because some won't fit.

I *do* know what the OP was talking about. And I don't think I've
written anything your last cmt could possibly apply to. But maybe I
didn't express myself clearly, since I'm not a native English speaker.

I think you don't have clear the context of what's going on. To
summarize things up:


On Wed, 15 Sep 2004 16:25:05 GMT, Ala Qumsieh <notvalid@email.com>
*jokingly* wrote:

>> Also, JAPHs as sigs tend to give a bad impression of the language to 
>> those who've never seen any other Perl. They promote the idea of an 
>> insular community that delights in writing obscure code that's difficult 
>> for newbies and outsiders to grok.
>
>They're just jealous because they can't write any valid Python program 
                                                         ^^^^^^
>in just four lines (/me ducks and runs for cover).


To which


On 16 Sep 2004 01:33:05 -0700, hydroxide@inorbit.com (T. Ogawa) wrote
(still jokingly):

>> They're just jealous because they can't write any valid Python program 
>> in just four lines (/me ducks and runs for cover).
>Well we can't write _any_ valid program in four lines, but we can
      ^^
>manage a JAPy or 30.  Some of them are even unreadable.
          ^^^^
>http://www.python.org/cgi-bin/moinmoin/JustAnotherPythoneer
            ^^^^^^


From which it is clear that *we* refers implicitly to "us Python
programmers". Doesn't it?

And then in reply


On 16 Sep 2004 09:16:49 -0700, krakle@visto.com (krakle) wrote
(*intendedly* jokingly):

>#!/usr/bin/perl -w
>print "What's your name?";
>my $name = <STDIN>;
>print "Hi $name I just wanted to show you a valid Perl program in 4
>lines. Isnt this valid?\n";


And I'm contending that his wannabe-joke is misplaced for we *all*
know here that we can write valid *Perl* programs in 0..4 and more
lines, including Ala Qumsieh and T. Ogawa. It was a joke about
*Python*, because...

>Of course, Perl and Ruby (and I think Python, though I'm not sure) let
>you put multiple statements on a line, so I suppose technically you

 ...(even if I don't know Python in detail myself, it is well known
that) Python, differently from Perl and many other languages is not a
"free form language", i.e. whitespace is mostly significant in it and
a correct indentation is enforced as part of the syntax of the
language.

Said this, I may have seamlessly skipped over this "mistake". Instead
I've deliberately made an ad hominem attack on "krakle" because it
made me upset to read what I judge to be an idiot's cmt from him, and
in the form of a badly written perl program, whereas from what he
wrote previously I got the idea that he must think he's such a smart
guy. It was not fair of me, I must admit it!

I just couldn't resist the temptation for I can't stand arrogant
superficial people...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Fri, 17 Sep 2004 15:53:40 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: $| (undocumented) magic?
Message-Id: <60olk0d79evnjqgkb8rcegha97bvfv09iv@4ax.com>

On 16 Sep 2004 21:48:53 -0700, krakle@visto.com (krakle) wrote:

>> It seems you don't waste your time with JAPHs, because you only spend
>> your efforts in "productive code"
>
>My point: Programs can be written in 4 lines of code. The rest of your
>post was ignored.

I bet it was! Thank you so much for having informed us all that
"programs can be written in 4 lines of code". I can't imagine where we
could be without your contribution!

Said this... *plonk!*


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: 17 Sep 2004 09:42:43 -0700
From: ioneabu@yahoo.com (wana)
Subject: anonymous hash notation question
Message-Id: <bf0b47ca.0409170842.6f195504@posting.google.com>

what is the difference between:

{a=>b, c=>d}

and

{-a=>b, -c=>d}

?

Specifically, what is the significance of the dash?


------------------------------

Date: Fri, 17 Sep 2004 18:56:42 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: anonymous hash notation question
Message-Id: <2r0jbvF14lkjjU1@uni-berlin.de>

wana wrote:
> what is the difference between:
> 
> {a=>b, c=>d}
> 
> and
> 
> {-a=>b, -c=>d}
> 
> ?

None of them compiles if you have strictures enabled (which you really 
should have!).

Why are you asking?

> Specifically, what is the significance of the dash?

Now I understand even less why you are asking. Isn't it obvious how 
the string 'a' differs from the string '-a'?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 17 Sep 2004 10:17:40 -0700
From: justkule@yahoo.com (siddhartha mulpuru)
Subject: cron manipulation using perl
Message-Id: <78e9cb22.0409170917.22cf7f11@posting.google.com>

Hi,

I am trying to develop a webpage with login,password and Unix box name
as input fields which will post to a perl script that would do the
following in CRON:
-	Allow you to access and LOGIN to specific UNIX boxes
-	Display the entries
-	Allow Editing of an entry
-	Allow searching for a specific entry
-	The returned page for each entry must display the page in "laymen's"
english, not cryptically as found in CRON
	(In other words a conversion must take place to and from the format
in CRON)
-	Upon entering the data it should submit convert the entry from
laymens' display, send it to the system, and
	issue the "CRONTAB Filename" command to upload it for that specific
USERID/PASSWORD.

I need to know ideas on how i can go about implementing this. Any
sample code or just some ideas how this can be done ?

Please advise and i appreciate your time a lot

Thanks
Sid


------------------------------

Date: 17 Sep 2004 09:27:06 -0700
From: zippy@sanft.net (Theo James)
Subject: Help with a program
Message-Id: <68d2887.0409170827.2856c116@posting.google.com>

I am a perl newbie working on a program.  I have two problems (Code
and design to follow):

1) The program works fine, unless two connections are made at the same
time, if two connections are made, both sessions are handled
correctly, but once both complete, the parent dies as well.  I do not
know what.

2) I now can write to the socket, but I need to do a read from the
socket, I do not know how to do that.

Here is the design:

I am a newbie perl programmer and I have an idea for a perl program,
but I want to know if it is possible before I attempt to write it. 
Here is what I need:

I have a client, talking to my server via TCP/IP to port 1234.  The
client is creating many seperate conversations that are a simple
request/response pairing. All requests from the client go to port
1234.  Here is how I would like it to flow:

(main loop)
Server.pl accepts connection on port 1234 ->
locks port 1234 from any other new connection ->
redirects conversation to lesser know port (ex. 2345) ->
calls subserver.pl on port 2345 to handle request response
conversation

as soon as Server.pl fires off subserver.pl ->
unblock port 1234 and allow another connection  (main loop)->

subserver.pl handles all communications with client and upon
completion (or time out) imports Server.pl that port 2345 is avilabile
for communication and then dies (or optionally wait for Server.pl to
send another session to it).


Here is the code so far:

#!/usr/bin/perl -Tw
use strict;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
use Socket;
use Carp;
my $CRLF = "\015\012";

sub spawn;  # forward declaration
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }

my $port = shift || 6666;
my $proto = getprotobyname('tcp');

($port) = $port =~ /^(\d+)$/                        or die "invalid
port";

socket(Server, PF_INET, SOCK_STREAM, $proto)    || die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR,
                                        pack("l", 1))   || die
"setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY))    || die "bind: $!";
#listen(Server,4)                               || die "listen: $!";
listen(Server,SOMAXCONN)                                || die
"listen: $!";

logmsg "server started on port $port";

my $waitedpid = 0;
my $paddr;

use POSIX ":sys_wait_h";
   sub REAPER {
      my $child;
      while (($waitedpid = waitpid(-1,WNOHANG)) > 0) {
         logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
        }
   $SIG{CHLD} = \&REAPER;  # loathe sysV
   }

$SIG{CHLD} = \&REAPER;

for ( $waitedpid = 0;
  ($paddr = accept(Client,Server)) || $waitedpid;
  $waitedpid = 0, close Client)
{
next if $waitedpid and not $paddr;
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);

logmsg "connection from $name [",
        inet_ntoa($iaddr), "]
        at port $port";

spawn sub {
my $counter=1;
#my $etx="\03";

    $|=1;
while ($counter<10){
print "testing $counter $CRLF";
sleep 1;
$counter=$counter + 1;
   };
print "ba-bye\n";
};

}

sub spawn {
my $coderef = shift;

unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE') {
    confess "usage: spawn CODEREF";
}

my $pid;
if (!defined($pid = fork)) {
    logmsg "cannot fork: $!";
    return;
} elsif ($pid) {
    logmsg "begat $pid";
    return; # I'm the parent
}
# else I'm the child -- go spawn

open(STDIN,  "<&Client")   || die "can't dup client to stdin";
open(STDOUT, ">&Client")   || die "can't dup client to stdout";
## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr";
exit &$coderef();
}


------------------------------

Date: 17 Sep 2004 09:59:22 -0700
From: ozgune@gmail.com (Ozgun Erdogan)
Subject: Re: hv_iterinit has side effects - who cares about PL theory
Message-Id: <48b43181.0409170859.21ca67f2@posting.google.com>

> when control is returned, you should be able to patch
> Data::Structure::Util to get it working.  You may need to muck 
> with HvLAZYDEL before restoring, but that seems relatively 
> staightforward.
> 
> I suspect you tried this already, so I'm wondering what
> sort of problems you encountered.

This works perfectly fine. I took a pretty similar approach and got
Data::Structure::Util to work. I think you need to pay special
attention when  doing graph walks with loops in them (I don't know,
maybe file system implementations with sym. links, certain networking
problems?).

> Personally I don't think lazy deletion is much of a big deal.
> IMO the iterator belongs in the hash because you can't
> implement Perl's C<each %foo> without associating an iterator
> to the hash itself.  If you don't think it belongs in the
> struct, where else would you put it?

From my perspective, lazy deletion is the biggest argument against
taking the iterator out. First, taking it out means a big change to
Perl's internals. Second, when you're walking over a hash, if you
don't pay attention and delete the element you're walking on, then
you'd get a seg. fault. Not a typical Perl behavior. Even though C++
STL clearly warns against doing this, it happened to me once or twice
- just because I was careless.

One alternative would be to have an independent iterator outside the
hash. Have an iterator struct that contains two elements, RITER and
EITER, and implement three functions: associate_to_hash(),
hv_iterinit_special(), hv_iternext_special().  Again, this all depends
on your perspective and your user base.


------------------------------

Date: 17 Sep 2004 13:28:56 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: hv_iterinit has side effects - who cares about PL theory
Message-Id: <87acvo249z.fsf@gemini.sunstarsys.com>

ozgune@gmail.com (Ozgun Erdogan) writes:


[...]

> One alternative would be to have an independent iterator outside the
> hash. Have an iterator struct that contains two elements, RITER and
> EITER, and implement three functions: associate_to_hash(),
> hv_iterinit_special(), hv_iternext_special().  Again, this all depends
> on your perspective and your user base.

Parrot is the right place to look for thread-safe implementations.
Dan Sugalski is the architect and he's well aware of the limitations
of perl5. Perhaps it'd be a good idea for you to get involved with the
perl6.internals list...

Best wishes.

-- 
Joe Schaefer


------------------------------

Date: 17 Sep 2004 13:55:52 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Is my algorithm wrong?
Message-Id: <cieqd8$bpa$1@mamenchi.zrz.TU-Berlin.DE>

David K. Wall <dwall@fastmail.fm> wrote in comp.lang.perl.misc:
> Looking <adsense@whitehouse.com> wrote:

[...]

> Just to redeem myself a little, here's a slightly altered version of 
> the single regex solution. (Although I suspect there's a more 
> succinct way to express it.)
> 
> 
> my $string = 'thisistheoriginalstringblahblahblah';
> 
> my ($part1, $part2) = grep defined, 
>     $string =~ /^
>                 (?:  (.{1,24}\S) \s  (.*)  )
>                 |
>                 (?:  (.{1,24}\S) \s? (.*)  )
>                /sx;

    pos( $string) = 24;
    my ($part1, $part2) = /(.*\s|.*)(\S*\G.*)/;

That's more compact, but not necessarily better than other solutions.

Anno


------------------------------

Date: 17 Sep 2004 14:01:27 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Need more efficient use of the substitution operator
Message-Id: <Xns956765F8E582Aasu1cornelledu@132.236.56.8>

niall.macpherson@moneyline.com (Niall Macpherson) wrote in 
news:a3376e0d.0409170219.5fdbf5ce@posting.google.com:

> I am trying to find the first occurence of anything between a '[' and
> a ']' and return that string 

In addition to the useful responses by others, consider reading the faq 
entry

perldoc -q match

Also, for simple string matches, keep in mind the index function:

perldoc -f index

> use strict;
> use warnings;
> use diagnostics;
> 
> sub GetString
> {
>      my ($teststring) = @_;
>      
>      if ($teststring =~ /\[.*\]/)
>      {
>           my $match  = $&;

Have you read perldoc perlvar?

 $&      The string matched by the last successful pattern match
 ...
         The use of this variable anywhere in a program imposes a
         considerable performance penalty on all regular expression
         matches. See "BUGS".

If you wanted to do what you are doing above in a better way, you could 
do this:

#! perl

use strict;
use warnings;

my $s = 'Hello [ insert planet name here ]';

print scalar find_bracketed_string($s), "\n";

sub find_bracketed_string {
    my ($s) = @_;

    my ($l, $r);
    
    if(($l = 1 + index $s, '[') > $[
            and ($r = index $s, ']', $l) >= $[) {
        my $rs = substr $s, $l, $r - $l;
        return wantarray ? ($rs, $r + 1) : $rs;
    }

    return;
}	
	
Sinan.


------------------------------

Date: 17 Sep 2004 07:35:08 -0700
From: niall.macpherson@moneyline.com (Niall Macpherson)
Subject: Re: Need more efficient use of the substitution operator
Message-Id: <a3376e0d.0409170635.5ad286aa@posting.google.com>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote in message news:<2qvtjpF151obkU1@uni-berlin.de>...
> 
> If you are trying to *find* something, it's not substitution you
> should do, but you'd rather use the m// (matching) operator with
> capturing parentheses (see "perldoc perlop").
> 

Thanks Gunnar . The reason that I was doing the substitution was that
I didn't fully understand the concept of the capturing parentheses in
a regexp.

Therefore all I had to work with was the string [STRING] returned from
via the $& variable which needed the '[' and ']' removed.

In your example you use the return value from the expression. Am I
right in thinking that this value will also be in $1 ?

And if I have multiple regexps inside my expression then the matches
will be in $1, $2, $3 ?


------------------------------

Date: Fri, 17 Sep 2004 18:15:26 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Need more efficient use of the substitution operator
Message-Id: <2r0guiF144m9kU1@uni-berlin.de>

Niall Macpherson wrote:
> Gunnar Hjalmarsson wrote:
>> 
>>     my $input = " foo [STRING] bar ";
>>     print "Result = '", $input =~ /\[(.*?)\]/, "'\n";
> 
> In your example you use the return value from the expression. Am I
> right in thinking that this value will also be in $1 ?

If there is a match: yes, otherwise: no. Consequently, if you want to 
work with $1, $2 etc., you need to first check if the match succeeded, 
and only use those variables if it did.

> And if I have multiple regexps inside my expression then the matches
> will be in $1, $2, $3 ?

No. The dollar-digit variables contain what was captured from the last 
succeeded match.

Or did you mean multiple pairs of capturing parentheses inside the 
regex? If you had asked that, the answer would have been yes. (Again 
provided that the match succeeded.)

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Fri, 17 Sep 2004 15:53:41 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Protecting passwords in Perl scripts?
Message-Id: <g6olk094j4haok7948vb171bofc0u9ha0c@4ax.com>

On Fri, 17 Sep 2004 09:05:02 +0100, Brian McCauley <nobull@mail.com>
wrote:

>> How can I shield the database (ftp, etc) password from prying eyes?
>
>You cannot.  You are looking for a perpetual motion machine.  It is 
>impossible to write a program fragment that generates prescribed secret 
>output (e.g. a password) but which a programmer given the program and 
>the environment in which it is run cannot find out what secret is.
>This has nothing to do with Perl.

<OT>
True. But I think the OP may find interesting the article "Password
Authentication with Insecure Communication", by Leslie Lamport. I
haven't read it myself, but I saw a description of the algorithm in
Tanenbaum's "Modern Operating Systems".
</OT>


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


------------------------------

Date: Fri, 17 Sep 2004 09:10:43 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Rounding error in program
Message-Id: <Y-ydnYjvifvOf9fcRVn-qg@adelphia.com>

Dol wrote:

> When I execute the following program, $d is set to 18 instead of 19 as
> I would expect. Any ideas why this happens?

Have a look in 'perldoc -q trig' for a discussion of rounding errors and 
why this happens.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: Fri, 17 Sep 2004 13:11:14 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: Rounding error in program
Message-Id: <46olk096ruu12k2spgno4jvjpsgiusvvdo@4ax.com>

On 17 Sep 2004 05:56:05 -0700, dolgoldur@yahoo.com (Dol) wrote:

>When I execute the following program, $d is set to 18 instead of 19 as
>I would expect. 

Why would you expect that?  It is the wrong expectation.

>Any ideas why this happens?

Yes, because 18.5 should be rounded to 18, not 19.  

Always rounding X.5 *up* as you probably learned in grade school
introduces a systematic bias.  This was changed by the International
Standards Organisation in 1992, to rounding to the nearest even
integer.

>#!/yourpathtoperl/perl
>
>$a = 50;
>$b = 0.37;
>
>$c = $a*$b
>$d = sprintf("%.0f",$c);
>
>print "$c\t$d\n";

for (1..30)
{
  $c = $_+0.5;
  $d = sprintf("%.0f",$c);

  print "$c\t$d\n";
}

--
Helgi Briem  hbriem AT simnet DOT is

Never worry about anything that you see on the news. 
To get on the news it must be sufficiently  rare 
that your chances of being involved are negligible!


------------------------------

Date: Fri, 17 Sep 2004 14:33:39 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Rounding error in program
Message-Id: <7BC2d.70$%42.57@trndny08>

"Chris Mattern" <matternc@comcast.net> wrote in message
news:R5udnZaoWM7QbNfcRVn-og@comcast.com...
> Dol wrote:
>
> > Hi all,
> >
> > When I execute the following program, $d is set to 18 instead of 19
as
> > I would expect. Any ideas why this happens?
>
> Your error was in expecting to get rounding.  Using sprintf like that
> doesn't result in the your number being rounded; the fractional part
> is simply truncated.  If you want rounding, add 0.5 to the number
> first before sending it to sprintf.

Please check your assumption before posting a reply to a question like
this.  You are regrettably wholly incorrect.

printf "%.0f\n", 3.8;

outputs "4".

Paul Lalli




------------------------------

Date: Fri, 17 Sep 2004 10:32:00 -0400
From: Shawn Corey <shawn.corey@sympatico.ca>
Subject: Re: Rounding error in program
Message-Id: <7zC2d.25157$0h7.1764334@news20.bellglobal.com>

Helgi Briem wrote:
>>#!/yourpathtoperl/perl
>>
>>$a = 50;
>>$b = 0.37;
>>
>>$c = $a*$b
>>$d = sprintf("%.0f",$c);
>>
>>print "$c\t$d\n";
> 
> 
> for (1..30)
> {
>   $c = $_+0.5;
>   $d = sprintf("%.0f",$c);
> 
>   print "$c\t$d\n";
> }
> 

My output is:
1.5     2
2.5     2
3.5     4
4.5     4
5.5     6
6.5     6
7.5     8
8.5     8
9.5     10
10.5    10
 ...

Why?

	--- Shawn
--
perl -v

This is perl, v5.8.0 built for darwin

Copyright 1987-2002, Larry Wall

Perl may be copied only under the terms of either the Artistic License 
or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.



------------------------------

Date: Fri, 17 Sep 2004 14:57:17 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: Rounding error in program
Message-Id: <plulk0pfcer4350ej5ugo8sk7u9698ns72@4ax.com>

On Fri, 17 Sep 2004 10:32:00 -0400, Shawn Corey
<shawn.corey@sympatico.ca> wrote:

>My output is:
>1.5     2
>2.5     2
>3.5     4
>4.5     4
>5.5     6
>6.5     6
>7.5     8
>8.5     8
>9.5     10
>10.5    10
>...
>
>Why?

????

I just told you why.  Didn't you read my post?

perldoc -q trig 

a.k.a. Does Perl have a round() function?  What about ceil() and 
          floor()?  Trig functions?

--
Helgi Briem  hbriem AT simnet DOT is

Never worry about anything that you see on the news. 
To get on the news it must be sufficiently  rare 
that your chances of being involved are negligible!


------------------------------

Date: 17 Sep 2004 14:57:20 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Rounding error in program
Message-Id: <Xns95676F729A812asu1cornelledu@132.236.56.8>

Shawn Corey <shawn.corey@sympatico.ca> wrote in
news:7zC2d.25157$0h7.1764334@news20.bellglobal.com: 

> My output is:
> 1.5     2
> 2.5     2
> 3.5     4
> 4.5     4
> 5.5     6
> 6.5     6
> 7.5     8
> 8.5     8
> 9.5     10
> 10.5    10
> ...
> 
> Why?
> 
>      --- Shawn

You snipped the relevant part of Helgi's post. Reading what you are 
responding to helps:

Helgi Briem <HelgiBriem_1@hotmail.com> wrote in 
news:46olk096ruu12k2spgno4jvjpsgiusvvdo@4ax.com:

>> Always rounding X.5 *up* as you probably learned in grade school
>> introduces a systematic bias.  This was changed by the International
>> Standards Organisation in 1992, to rounding to the nearest even
>> integer.

Sinan.


------------------------------

Date: Fri, 17 Sep 2004 14:57:59 +0000
From: Helgi Briem <HelgiBriem_1@hotmail.com>
Subject: Re: Rounding error in program
Message-Id: <3rulk0hl7vtu34cb625o2kfqni4g28rjrh@4ax.com>

On Fri, 17 Sep 2004 09:10:43 -0400, Sherm Pendley
<spamtrap@dot-app.org> wrote:

>Dol wrote:
>
>> When I execute the following program, $d is set to 18 instead of 19 as
>> I would expect. Any ideas why this happens?
>
>Have a look in 'perldoc -q trig' for a discussion of rounding errors and 
>why this happens.

It's not a rounding "error".  It is correct rounding.

--
Helgi Briem  hbriem AT simnet DOT is

Never worry about anything that you see on the news. 
To get on the news it must be sufficiently  rare 
that your chances of being involved are negligible!


------------------------------

Date: Fri, 17 Sep 2004 11:43:37 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: Rounding error in program
Message-Id: <Pine.SGI.3.96.1040917114152.55590A-100000@vcmr-64.server.rpi.edu>

On 17 Sep 2004, A. Sinan Unur wrote:

>You snipped the relevant part of Helgi's post. Reading what you are 
>responding to helps:
>
>Helgi Briem <HelgiBriem_1@hotmail.com> wrote in 
>news:46olk096ruu12k2spgno4jvjpsgiusvvdo@4ax.com:
>
>>> Always rounding X.5 *up* as you probably learned in grade school
>>> introduces a systematic bias.  This was changed by the International
>>> Standards Organisation in 1992, to rounding to the nearest even
>>> integer.

Does anyone know the ISO's reasoning behind it?  It seems to create a new
bias, which doesn't seem to make sense to me, at least.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
  Senior Dean, Fall 2004    %  have long ago been overpaid?
RPI Corporation Secretary   %
http://japhy.perlmonk.org/  %    -- Meister Eckhart




------------------------------

Date: 17 Sep 2004 16:25:48 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Rounding error in program
Message-Id: <Xns95677E71DE52Basu1cornelledu@132.236.56.8>

Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote in 
news:Pine.SGI.3.96.1040917114152.55590A-100000@vcmr-64.server.rpi.edu:

> On 17 Sep 2004, A. Sinan Unur wrote:
> 
>>You snipped the relevant part of Helgi's post. Reading what you are 
>>responding to helps:
>>
>>Helgi Briem <HelgiBriem_1@hotmail.com> wrote in 
>>news:46olk096ruu12k2spgno4jvjpsgiusvvdo@4ax.com:
>>
>>>> Always rounding X.5 *up* as you probably learned in grade school
>>>> introduces a systematic bias.  This was changed by the International
>>>> Standards Organisation in 1992, to rounding to the nearest even
>>>> integer.
> 
> Does anyone know the ISO's reasoning behind it?  It seems to create a new
> bias, which doesn't seem to make sense to me, at least.

It is in 

ISO 1995 Guide to the Expression of Uncertainty in Measurement 1st edn 
(Geneva: ISO)

but the following ought to give you a good enough idea:

http://www.crystalkeen.com/articles/crystalreports/rounding.htm

Hope that helps.

Sinan.


------------------------------

Date: 17 Sep 2004 07:41:28 -0700
From: larryj@gsu.edu (Larry Felton Johnson)
Subject: Re: script portability problem
Message-Id: <4ae7bf57.0409170641.48f6caa4@posting.google.com>

The best place to start thinking about your options here is the
man page "perlrun".  It has a good discussion of approaches to
running perl in different environments.  I may be just stating the
obvious, but perlrun is a good starting point.

Larry


develop@gistenson.com wrote in message news:<cichs6$s56$1@new7.xnet.com>...
> Hi,
> 
> At the beginning of each perl cgi script is the #! line. I'm assuming that 
> this is an http directive that executes perl and indicates thedirectory in 
> which perl is found.
> 
> I'm developing scripts for a client. The problem is that he is using a 
> different web host than I am, and perl is found in different directories, 
> so I need to use different #! directives at the beginning of the scripts.
> 
> I don't want to have to change the scripts when I move them from my host 
> to the client's host. Is there a '#if' '#else' type of directive pair, and 
> if there is, what is the syntax and what test could I use to have the 
> script know whether it's running on my host or the client's host? 
> (Possibly the environment, $ENV{'???'}.)
> 
> Is there another way to accomplish this?
> 
> Thanks,
> Dan


------------------------------

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 7153
***************************************


home help back first fref pref prev next nref lref last post