[6921] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 546 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 30 21:07:11 1997

Date: Fri, 30 May 97 18:00:25 -0700
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, 30 May 1997     Volume: 8 Number: 546

Today's topics:
     Re: !Reading file (Tad McClellan)
     Re: !Reading file <rootbeer@teleport.com>
     Re: (Q): Printing paragraphs of a file, randomly (Chipmunk)
     (URGENT HELP!) Formating to SendMail <synchro@logos.cy.net>
     5.004 and $& - performance degradation <iancu@iil.intel.com>
     Re: any editor for perl?--any editor written in Perl? (O'Shaughnessy Evans)
     Re: any editor for perl? walker@earthworld.com
     Re: any editor for perl? <rootbeer@teleport.com>
     Error message when I use Socket <-klee@geocities.com->
     Forking in Perl on NT ?? <bhuvan@qimage.com>
     Re: Good Perl Tutorial with exercises ?? <rootbeer@teleport.com>
     Help! Scoping problems with use strict turned on :-( <sr@pc-plus.de>
     Re: How to get the PID of a system call ? <rootbeer@teleport.com>
     Re: Need vacation program in Perl! <rootbeer@teleport.com>
     Re: Need vacation program in Perl! (Nathan V. Patwardhan)
     Re: OOP & perl <habusan2@sprynet.com>
     Re: Slices of lists (Andrew M. Langmead)
     Re: Sorting by values (Tad McClellan)
     Re: Sorting by values <rootbeer@teleport.com>
     Re: using references as hash keys <rootbeer@teleport.com>
     Re: Why 10000 + 0.63 = 10000.6299999...? (Tad McClellan)
     Re: Why 10000 + 0.63 = 10000.6299999...? (Tung-chiang Yang)
     Re: Why 10000 + 0.63 = 10000.6299999...? (Tung-chiang Yang)
     Re: Why is ("a" == 0) true? dchen@interport.net
     Re: WinNT Build 306: %var = {}; fails <rootbeer@teleport.com>
     Re: WinNT Build 306: %var = {}; fails <phamlow@scic.intel.com>
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: Fri, 30 May 1997 16:43:19 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: !Reading file
Message-Id: <nlhnm5.t9b.ln@localhost>

Lauri Laakso (lauri@nettipaja.clinet.fi) wrote:
: This should be simple:


It is.


: I have file $xfile...

: --- begin ---
: This is line 1
: and there are more lines like this one
: and this third one
: --- end ---

: How I can read it...

: $data = "This is line 1\nand there are more lines like this one\nandthis
: third one";


1)
   $/ = '';     # enable slurping
   $data = <>;  # do the slurping


2)
   $data = join '', <>;



: Please email or I may miss it.

You should get a better newsreader, or you may miss it.


--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming
    tadmc@flash.net


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

Date: Fri, 30 May 1997 15:19:56 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Lauri Laakso <lauri@nettipaja.clinet.fi>
Subject: Re: !Reading file
Message-Id: <Pine.GSO.3.96.970530151559.8163H-100000@kelly.teleport.com>

On Fri, 30 May 1997, Lauri Laakso wrote:

> I have file $xfile... 

> How I can read it...
> 
> $data = "This is line 1\nand there are more lines like this one\nandthis
> third one";

Here's a good way. See the documentation for $/ in perlvar, if needed.

    open FILE, $xfile or die "Can't read '$xfile': $!";
    {
        local($/) = undef;		# set up to slurp
        $data = <FILE>;			# read entire file
    }
    close FILE;

Someone may suggest using join(), which would also work. This should be
more efficient, and more memory efficient on large files. Hope this helps! 

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: 30 May 1997 22:23:31 GMT
From: Ronald.J.Kimball@dartmouth.edu (Chipmunk)
Subject: Re: (Q): Printing paragraphs of a file, randomly
Message-Id: <5mnk13$qel$1@dartvax.dartmouth.edu>

In article <Pine.OSF.3.95.970528174138.9781A-100000@engunx.unl.edu>
Daneshjoo <eermmb@engunx.unl.edu> writes:

>         I have been trying to write a script to read a file, which
> contains paragraphs and are seperated either by a blank space
> or each paragraph is numbered, and print a paragraph randomly.  But
> unfortunately I haven't been able to do it.  Could some one please help me
> out.  TIA

Not sure what you mean by "numbered".  Could you give a little more
info about that?

This should work where paragraphs are separated by blank lines.

srand;
$/ = '';
open(IN, "<$input") || die "Unable to open $input: $!\n";
@paragraphs = <IN>;
close(IN);

open(OUT, ">$output") || die "Unable to open $output: $!\n";
print OUT $paragraphs[rand($#paragraphs)];
close(OUT);

Chipmunk


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

Date: Tue, 27 May 1997 14:17:26 -0700
From: "A. Kello" <synchro@logos.cy.net>
Subject: (URGENT HELP!) Formating to SendMail
Message-Id: <338B4F66.3A35@logos.cy.net>

(URGENT HELP!) Formating to SendMail

Hi there,

I'm new to PERL ,can someone please help me with this one:
I'm trying to send a sort of a columnar report EMail through
sendmail,but the results are anything but columnar!. I tried FORMAT but
that doesn't even output any thing and with 'printf()' it doesn't
display as its counterpart in C , the manual of Perl5 says that 'printf'
is an exact match for C's  printf. (please Help)

I'm trying to send something like this:

Column1               Column2 
----------------------------------------------
$dummy1               $dummy2


I used :     printf (MAIL,"%-40s %10s\n"$dummy1,$dummy2) ;


Thanks in advance

A.Kello 
synchro@logos.cy.net


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

Date: Sat, 31 May 1997 01:03:53 +0300
From: Igal Iancu <iancu@iil.intel.com>
Subject: 5.004 and $& - performance degradation
Message-Id: <338F4EC9.1A8D6358@iil.intel.com>

Hi,

I have a rather long perl program which uses $& "and
friends" in may places.  Though I know I should probably
do without them, but I sometimes have to.  Enter 5.004,
and seems that it is much more sensitive to their use 
than 5.003.

See this example code, and the output for the two
perl versions:
+++++++++++ cut here ++++++++++++++
#!/usr/bin/perl
print "Perl version $]\n";

#initial big string to search
$string = "0" x 50000 ;
$regexp_to_search = "00"; #these are much more complex in real
                          #life situations, but will do for this
                          #example

# >>>> watch here <<<<
$dummy = $&;      #set the program to use $&

########################
#do the search 
$found = 0;

$start = (times)[0];   #start timer

$found++ while ($string =~ /$regexp_to_search/og);

$end = (times)[0];     #stop timer
$delta = $end - $start;

print "found $found, $delta sec\n";

++++++++++++ cut here +++++++++++++

Results:

>perl-5.003 posting.pl
Perl version 5.003
found 25000, 0.25 sec

>perl-5.004 posting.pl
Perl version 5.004
found 25000, 6.89 sec

(This is HPUX-10 with Perl's malloc)
If I remove the reference to $&, I get similar performance results
from both versions.

Does anyone else see the same and can offer an explanation or
workaround?

Thanks in advance,

Igal Iancu
iancu@iil.intel.com


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

Date: 31 May 1997 00:05:56 GMT
From: shaug@callamer.com (O'Shaughnessy Evans)
Subject: Re: any editor for perl?--any editor written in Perl?
Message-Id: <5mnq14$cka$1@zinger.callamer.com>

In article <8cu3jkve8u.fsf@gadget.cscaper.com>,
	Randal Schwartz <merlyn@stonehenge.com> writes:
>>>>>> "O'Shaughnessy" == O'Shaughnessy Evans <shaug@callamer.com> writes:
> 
>> me asking where to find a perl-ish vi, followed by...
> 
> It wouldn't be that much of a surprise if you had actually been
> looking through the CPAN from time to time as we "goo roos" keep
> tellin' y'all to do. :-)
> 
> It's been in the CPAN for quite a long time...
> 
> 	http://www.perl.com/CPAN/src/misc/nvi.tar.gz

 Oops.  Well, this makes me feel really stupid.  I apologize for not looking
 around for an answer before asking the (effective) world.  I really should
 have known better.  I must have been caught up in the euphoria of thinking 
 that a perl-ish vi was available (blame it on the other programmer I work 
 with, who keeps trying to talk me into switching to emacs; then blame it 
 on me for not thinking).
 
 If nothing else, I 'spose that checking for versions of vi in Yahoo, and 
 then looking at the home pages for any of those versions, would have been 
 a decent solution.  And then there's DejaNews, which I just tried out, and 
 it really didn't take long to find a reference to nvi when searching for 
 "vi or editor" throughout the c.l.p.m archives.  
 
 I've got one comment regarding CPAN, though:  reading the ROADMAP.html 
 file doesn't really make it obvious that you're gonna find real tools like 
 vi there.  The link for the src subdirectory says "source code and patches 
 for Perl itself (and for some auxiliary utilities)".  I've always thought 
 that this meant:  source for Perl, patches, and sources for other things 
 having to do with "Perl itself".  So, vi doesn't intuitively fit in there
 for me.  Maybe a rewording of the link would be useful.  Nonetheless, it's
 no excuse -- I could have found what I was looking for in DejaNews.

 So, I've learned my lesson... look before I leap.  Regardless of that,
 thanks for the help   *8)=

 - Shaug


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

Date: Fri, 30 May 1997 21:53:05 GMT
From: walker@earthworld.com
Subject: Re: any editor for perl?
Message-Id: <33934c23.16891506@news.earthworld.com>

On Fri, 30 May 1997 02:54:43 -0700, TRG Custom Graphics
<customdesign@nstate.net> wrote:

>Mark Mills wrote:
a bunch of stuff about editors....
on windoze95 I use UltraEdit-32. You might be able to find it at:
http://www.idmcomp.com
It supports wordfile lists(syntax highlighting) for various
programming languages. Perl is not included but there should be one
around on this newsgroup. (thats where I got mine) (if you need it,
e-mail me and I'll send it).
Aside from a few minor annoyances (it should take you awhile to find
them) UltraEdit is outstanding. You will never use notepad again :-)



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

Date: Fri, 30 May 1997 16:03:52 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Hope this helps! <scribble@wwa.com>
Subject: Re: any editor for perl?
Message-Id: <Pine.GSO.3.96.970530160238.8163P-100000@kelly.teleport.com>

On 30 May 1997, Hope this helps! wrote:

> Since you do use vi, your best bet is to get better used to it,
> approaching it with unprejudiced eye and without negative attitude.
> With a few macros, it's simply the best editor for Perl, in fact
> most of the time all I have to do in vi is hit a few well-timed
> tabs and it writes the code I wanted.

So, you have a "do what I mean" tab key? Must be a "smart terminal". 

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: Fri, 30 May 1997 10:00:20 -0700
From: Andrew Lee <-klee@geocities.com->
Subject: Error message when I use Socket
Message-Id: <338F07A4.61C9@geocities.com->

Hi,

Anybody knows how to fix this? It comes up when I use certain modules
like Socket and Fcntl.

Can't load
'/usr/local/lib/perl5/sun4-solaris/5.003/auto/Socket/Socket.so' for
module Socket: ld.so.1: perl: fatal: relocation error: symbol not found:
main: referenced in
/usr/local/lib/perl5/sun4-solaris/5.003/auto/Socket/Socket.so at
/usr/local/lib/perl5/DynaLoader.pm line 140.

 at /usr/local/lib/perl5/Socket.pm line 272
BEGIN failed--compilation aborted at -e line 1.

Thanks. perldiag doesn't explain this.

Andrew Lee


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

Date: 30 May 1997 23:18:56 GMT
From: "Bhuvanesh Jani" <bhuvan@qimage.com>
Subject: Forking in Perl on NT ??
Message-Id: <01bc6d4f$466ca320$2cbe93cc@delta.qimage.com>

Hi,

I have a perl script which uses fork.  I am trying execute it on NT.  
Seems like NT doesn't support fork.  Does any one out there know the way
around it ?!

Please reply directly to bhuvan@qimage.com.

Thanks
-- 
Bhuvan Jani



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

Date: Fri, 30 May 1997 15:26:09 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Jim Garrison <jhg@austx.tandem.com>
Subject: Re: Good Perl Tutorial with exercises ??
Message-Id: <Pine.GSO.3.96.970530152333.8163J-100000@kelly.teleport.com>

On Fri, 30 May 1997, Jim Garrison wrote:

> Has anyone written a good Perl tutorial that has
> well-written, meaningful exercises?  

Would you be looking for the Llama book? Oh, you said "meaningful", so
maybe you meant its second edition. :-)  That should be out soon, for
sufficiantly large values of "soon". But I think you'd be fine with the
current edition, which has helped at least a few dozen people to learn
Perl. Or maybe it's a few dozen every week? 

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: Sat, 31 May 1997 01:34:10 +0200
From: Stephen Riehm <sr@pc-plus.de>
Subject: Help! Scoping problems with use strict turned on :-(
Message-Id: <338F63F2.41C6@pc-plus.de>

Hi Perl Hackers,

I've been writing perl scripts for quite a while now, but most of
them have been straight forward and happily self contained in
individual files etc, no real need for packages etc. I've also
gotten quite used to using the strict module, which I quite like,
but now it's causing me problems, because it want's every variable
to have a fully qualified name, or that I use "my".

The problem is that I'm defining an array in my main program, and
then I'm trying from a required module (no other package is defined!) 
from the other file. The problem is that the array is visible in the
the main part of the program where it was defined, but not in the
included file. Then I tried using @main::array to get at the array
which was defined in the main file, using "my" - but it still comes up
empty.

If I turn off strict, and define the array as being a global variable,
then everything works just fine.

Dose anyone know how I can continue to work with strict turned on, and
yet still bandy these variables around without having to do
summersaults?

(I also gave the Exporter a quick try, but it didn't work, I still got
warnings that the things I was exporting weren't available in the
opposing packages.)

Thanks in advance,

Steve

and here's a quick example to demonstrate what I want:
--- file1 ---
#!/usr/local/bin/perl

use strict;
require "file2";

my @list = ( qw( this is a test list ) );

&do_stuff();

--- file2 ---
#!/usr/local/bin/perl

use strict;

sub do_stuff
{
    print "here is the only list I know: @list\n";
}

can I do this sort of thing, and still use strict?

//   __________________________ ._ o  ________________________________
\\\\ Stephen Riehm             /  //\.        Stephen.Riehm@pc-plus.de
//// pc-plus                  '  \>> |         Phone: +49 89 45566 148
  \\ 81539 Munich, Germany        \\ `           Fax: +49 89 45566 113


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

Date: Fri, 30 May 1997 16:01:55 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Georgios Pappas <p-joann@uiuc.edu>
Subject: Re: How to get the PID of a system call ?
Message-Id: <Pine.GSO.3.96.970530155917.8163O-100000@kelly.teleport.com>

On Fri, 30 May 1997, Georgios Pappas wrote:

> 	The question is how to get the process ID of a child created by
> the system call. 

When you use system(), it waits for the process to exit. By the time Perl
is running again, the child's ID is moot, since the child is already dead.

Maybe you mean that you are using a command that starts another process of
its own before it returns. In which case, you'll need to ask the author of
that command to help you out. :-)

Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: Fri, 30 May 1997 15:22:21 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Mongkol Horburapa <q13382@bang.cig.mot.com>
Subject: Re: Need vacation program in Perl!
Message-Id: <Pine.GSO.3.96.970530152125.8163I-100000@kelly.teleport.com>

On Fri, 30 May 1997, Mongkol Horburapa wrote:

> It doesn't have vacation program on it.

You can probably install the GNU vacation program, which should be found
on many Linux archive sites. Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: 30 May 1997 23:59:53 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: Need vacation program in Perl!
Message-Id: <5mnplp$nj3@fridge-nf0.shore.net>

Tom Phoenix (rootbeer@teleport.com) wrote:

: You can probably install the GNU vacation program, which should be found
: on many Linux archive sites. Hope this helps!

Come to think of it, mailagent, a mail filtering program written in Perl
seems to support a "vacation mode" if memory serves correctly.  Mailagent
isn't the fastest mail filter known to person, but if Randal says he likes
it, that's good enough for me.  :-)

Even if mailagent doesn't support a vacation mode, you could always write
a regexp in the rules file for all recevied messages, and send a reply to 
the sender.  You probably could do the same with elm's filter or procmail.

Hope this helps!

--
Nathan V. Patwardhan
nvp@shore.net



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

Date: 30 May 1997 22:40:20 GMT
From: "Geoffrey Broadwell" <habusan2@sprynet.com>
Subject: Re: OOP & perl
Message-Id: <01bc51d0$3f0b64d0$0286aec7@gjblap2>

Geez, and I thought that was Randal's list of problems . . . what's your
lawsuit?

-'f


Tom Christiansen <tchrist@mox.perl.com> wrote in article
<5jq8fv$ijq$1@csnews.cs.colorado.edu>...
> In the next 6-8 weeks, I have publisher's deadlines
> for one existing book to update, another one to write from scratch,
> plus three books to tech-edit, and to top it off I have a bogus 7-figure
> nuisance lawsuit to try to get dismissed.  



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

Date: Fri, 30 May 1997 00:57:46 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Slices of lists
Message-Id: <EAyyoA.1wK@world.std.com>

brian.orpin@gecmX.com (Brian Orpin) writes:

>I used a grep to find if the item (string) exists in the array but
>could not find a function to tell me it's position other than by going
>through the array and incrementing a counter.  Is there a better way?

If you are going to be doing it frequently, maybe setting up a
parallel hash that has the string as the key and the keys index in the
array as the value. See the FAQ entry "How can I tell whether an array
contains a certain element?"

If you populate the array like this:

while(<>) {
  # do something to set $scalar to a value
  push @old, $scalar;
  $where{$scalar} = $#old;
}

Then you can say: 

@old[ $where{$starting_string} .. $where{$ending_string} ];

Of course, thats assuming that you can determine each string element
by equality and that they are unique.

>So I can find the start and end points of the slice I want to remove.

>@new = @old[$start..$end];

>However, how do I replace the slice with a different array e.g. or
>even delete those elements I have identified.

>@old = ("one", "two", "three");
>@new = ("twoa", "twob");

>to get  an array ("one", "twoa", "twob", "three")

use the splice function.

If @new contains the new elements, @old contains the old elements, 
and you want the result to be in @array, say:

@array = @old;
splice @array, $start, $end-$start, @new;

Everyone who learned perl from the first edition of "Programming Perl"
probably knows this very well for three reasons.

1. They've been programming perl for years by now.
2. It was probably the most confusing function to introduce in chapter 1.
3. The big shoe.
-- 
Andrew Langmead


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

Date: Fri, 30 May 1997 16:48:51 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Sorting by values
Message-Id: <30inm5.t9b.ln@localhost>

min-woong sohn (soh3@midway.uchicago.edu) wrote:

: I'd like to know if there is a way of printing
: the contents of an associative array sorted by
: values (in descending order) rather than sorted
: by keys as documented in the Programming Perl book.
: Any help will be greatly appreciated.


see the Perl FAQ, part 4:

How do I sort a hash (optionally by value instead of key)?


I guess you must have missed that when you checked the Perl FAQ
before posting your question to the Perl newsgroup...


--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming
    tadmc@flash.net


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

Date: Fri, 30 May 1997 16:11:18 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: min-woong sohn <soh3@midway.uchicago.edu>
Subject: Re: Sorting by values
Message-Id: <Pine.GSO.3.96.970530160943.8163R-100000@kelly.teleport.com>

On Fri, 30 May 1997, min-woong sohn wrote:

> I'd like to know if there is a way of printing the contents of an
> associative array sorted by values (in descending order) rather than
> sorted by keys as documented in the Programming Perl book. 

Do you mean, besides what's in the FAQ? :-)

    http://www.perl.org/CPAN/doc/FAQs/FAQ/html/perlfaq.html
    http://www.perl.com/CPAN/doc/FAQs/FAQ/html/perlfaq.html

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: Fri, 30 May 1997 15:34:30 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: dagoch@ibm.net
Subject: Re: using references as hash keys
Message-Id: <Pine.GSO.3.96.970530153010.8163L-100000@kelly.teleport.com>

On Thu, 29 May 1997, Charles Henkel wrote:

> From: Charles Henkel <csh@henktech.com.REMOVE>

Okay, your address is removed from the list of destinations for this
message. :-) 

> Newsgroups: comp.lang.perl

If your news administrator still carries comp.lang.perl, please encourage
him or her to check out the frequent posting about bogus newsgroup names
in news.announce.newgroups. You'll be doing yourself and many others a
favor to use comp.lang.perl.misc (and other valid Perl newsgroups)
instead.

    news:news.announce.newgroups

> You can store hard refs as *values* in a hash, so add an additional
> key to your hash:
> 
> $myhash($x,ref} = $x;

That's not even going to compile, and there's no obvious way to fix
it. Maybe you meant this?

    $my_hash{$some_ref} = [ $some_ref, $my_value ];

Now, although the keys of the hash are strings, the live hard reference is
still available to you, as well as the original value that was to be
stored into the hash. Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: Fri, 30 May 1997 16:57:46 -0500
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Why 10000 + 0.63 = 10000.6299999...?
Message-Id: <qginm5.ceb.ln@localhost>

Tian Chi (tian@esd.sgi.com) wrote:
: I was testing the following Perl script, 
: but got a wrong result? 

Well, what makes you say that it is wrong?

That is, what did you expect it to do?


: Why is that?

Because that's how floating point numbers work on computers...


: #!/bin/perl

You should use the -w switch you know.


[ snip code ]


: Any help would be appreciated.

$ccc = sprintf("%10.2f", $aaa + $bbb);



Some numbers cannot be represented _exactly_.

You already know this for the usual base 10 number system:


1/3 = .3333333333333333333333333333333333333333333333333333333...

and it is still not exact.


Similar things happen in base 2 number systems too...


--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming
    tadmc@flash.net


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

Date: Fri, 30 May 1997 21:12:33 GMT
From: tcyang@netcom.com (Tung-chiang Yang)
Subject: Re: Why 10000 + 0.63 = 10000.6299999...?
Message-Id: <tcyangEB0Iwx.I2L@netcom.com>

This applies for any computer algorithm -- decimal floating point numbers
are not expressed exactly in binary, and never compared two floating
point numbers by "==".

But I wonder why your $aaa still prints out 0.63.  One of the possibilities
is that the 100 increased the dynamic range of the number under finite
precision, so 100.63 becomes 100.62999999 while 0.63 is still 0.63.

This is like 1e-12 is still 1e-12, but (1 + 1e-12) - 1 could become 0 if
the precision you use is not good enough.

There is no "comp.lang.perl".

===============================
Tian Chi (tian@esd.sgi.com) wrote:
: I was testing the following Perl script, 
: but got a wrong result? Why is that?

: #!/bin/perl

: $aaa = 0.63;
: $bbb = 10000;
: $ccc = $aaa + $bbb;

: print "aaa = ", $aaa, "\n";
: print "bbb = ", $bbb, "\n";
: print "ccc = ", $ccc, "\n";

: The result is

: aaa = 0.63
: bbb = 10000
: ccc = 10000.629999999999

: Any help would be appreciated.

--
Tung-chiang Yang                       tcyang@netcom.com

soc.culture.taiwan, soc.culture.china (by SCC FAQ Team) FAQ's:
   http://www.iglou.com/tcyang/Taiwan_faq.shtml, China_faq.shtml


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

Date: Fri, 30 May 1997 22:06:12 GMT
From: tcyang@netcom.com (Tung-chiang Yang)
Subject: Re: Why 10000 + 0.63 = 10000.6299999...?
Message-Id: <tcyangEB0LEC.LKI@netcom.com>

Actually, some decimal floating point numbers can be represented
exactly, though they are in the extreme minorities, like

   0.125 (decimal) = 0.001 (binary)
   0.375 (decimal) = 0.011 (binary)

Any decimal numbers not able to be represented in the form

   p/2^n

with p being an integer will not be exactly represented with binary
numbers.

==================================
Tung-chiang Yang (tcyang@netcom.com) wrote:
: This applies for any computer algorithm -- decimal floating point numbers
: are not expressed exactly in binary, and never compared two floating
: point numbers by "==".

: (deleted)

--
Tung-chiang Yang                       tcyang@netcom.com

soc.culture.taiwan, soc.culture.china (by SCC FAQ Team) FAQ's:
   http://www.iglou.com/tcyang/Taiwan_faq.shtml, China_faq.shtml


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

Date: 30 May 1997 19:38:04 -0400
From: dchen@interport.net
Subject: Re: Why is ("a" == 0) true?
Message-Id: <5mnocs$bba@interport.net>

Thanks for all the responses.

Clive Newall <crn@bby.com.au> put it best:
	"... read p 528 of the blue camel"
where it says "any non-numeric string is numerically equal to zero."

I was getting confused staring at page 21 in the blue camel (sept 96),
where it says:  Any number is true except for 0 and
                Any string is true except for "" and "0".
So I assumed, erroneously, that "a" is true and 0 is false,
thus ("a" != 0).  

Next time I use 'eq'.

Thanks, Dave


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

Date: Fri, 30 May 1997 16:12:00 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Charles Henkel <csh@henktech.com.REMOVE>
Subject: Re: WinNT Build 306: %var = {}; fails
Message-Id: <Pine.GSO.3.96.970530161137.8163S-100000@kelly.teleport.com>

On Wed, 28 May 1997, Charles Henkel wrote:

> I sometimes initialize my hashes to the null hash before using them:
> 
> %hash = {};

You want this. Hope this helps!

    %hash = ();

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



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

Date: Fri, 30 May 1997 13:59:38 -0700
From: Paul Hamlow <phamlow@scic.intel.com>
To: csh@henktech.com
Subject: Re: WinNT Build 306: %var = {}; fails
Message-Id: <338F3FBA.FF6@scic.intel.com>

Charles Henkel wrote:
> I sometimes initialize my hashes to the null hash before using them:
> %hash = {};
> This is giving me the following error under 5.003 build 306:
> 
> F:\BOC\uds>perl -e "%hash = {};"
> Odd number of elements in hash list at -e line 1.

Isn't {} the anonymous hash composer construct?  It looks to me like
you're trying to initialize the hash with a reference to another
anonymous hash.  I think what you want is:

%hash = ();

-- 
Paul Hamlow                                 Phone...........503 591-6190
Senior CAD Engineer                         Pager...........503 604-8966 
Intel Corporation, MS AL4-51                Fax.............503 591-4862
5200 NE Elam Young Parkway                  Locator.............AL4-2-H2
Hillsboro, OR 97124                         Email phamlow@scic.intel.com


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

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 546
*************************************

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