[31467] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2719 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 13 18:09:44 2009

Date: Sun, 13 Dec 2009 15:09:08 -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           Sun, 13 Dec 2009     Volume: 11 Number: 2719

Today's topics:
    Re: 787 rollt <jurgenex@hotmail.com>
    Re: Come on Perl, rescue me one more time! (Randal L. Schwartz)
    Re: How big do your programs get before you modularise  <justin.0912@purestblue.com>
        newbie question <dirkmb@gmail.com>
    Re: newbie question <tadmc@seesig.invalid>
    Re: newbie question <dirkmb@gmail.com>
    Re: newbie question <jurgenex@hotmail.com>
    Re: newbie question <dirkmb@gmail.com>
    Re: newbie question <spamtrap@shermpendley.com>
    Re: newbie question <jurgenex@hotmail.com>
    Re: newbie question sln@netherlands.com
    Re: newbie question <cartercc@gmail.com>
    Re: Scripts & Modules <xhoster@gmail.com>
    Re: Scripts & Modules <ben@morrow.me.uk>
        Searching all instances of a pattern across multi-lines <laredotornado@zipmail.com>
    Re: Searching all instances of a pattern across multi-l <ben@morrow.me.uk>
    Re: Searching all instances of a pattern across multi-l <derykus@gmail.com>
    Re: Simple loop error (Bradley K. Sherman)
    Re: Simple loop error <mvdwege@mail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 13 Dec 2009 14:18:59 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: 787 rollt
Message-Id: <k1qai5tvu76j71n9jal2bivv508vs4svki@4ax.com>

Jürgen Exner <jurgenex@hotmail.com> wrote:
[Sorry, wrong NG, Cancel requrest has been sent]


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

Date: Sun, 13 Dec 2009 05:00:18 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Come on Perl, rescue me one more time!
Message-Id: <86iqcb3u71.fsf@blue.stonehenge.com>

>>>>> "laredotornado" == laredotornado  <laredotornado@zipmail.com> writes:

laredotornado> What I would like to do is remove the first line immediately after any
laredotornado> new line (carriage return) and keep all the other lines.  How can I do
laredotornado> this with perl?

perl -ne 'print unless (/^$/.../^/) =~ /E/'

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Sun, 13 Dec 2009 22:56:54 +0000
From: Justin C <justin.0912@purestblue.com>
Subject: Re: How big do your programs get before you modularise most of it?
Message-Id: <m28gv6-e51.ln1@purestblue.com>

In article <56fc.4b20f157.75f97@zem>, Justin C wrote:
> I keep writing programs that (for me at least) start getting large. I
> know they're small, even by the standard of many of the modules that
> I've seen, but how do you decide when to break down your code and
> modularise?

[snip]

Thank you to all who have replied. There's certainly plenty to think
about. I definitely need to think more about my projects than I do,
before I start writing any code. Thinking more about the data that I'm
working with, and how I can better manage it, is certainly something I
should be doing. Reading about data abstraction and encapsulation was
very interesting, it's certainly made me think differently about what
I'm working on at the moment, and that I really should have created a
function (or three) for it - I can see how that would have helped, but I
think I've gone too far with this to go back... but for next time.

This thread has helped support my reading of The Alpaca, interesting
stuff, thanks again.

   Justin.

-- 
Justin C, by the sea.


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

Date: Sun, 13 Dec 2009 13:36:51 +0100
From: "scarlet" <dirkmb@gmail.com>
Subject: newbie question
Message-Id: <4b24de72$0$395$5f6aeac3@news.scarlet.nl>

Hello,
I have two files : file A.tok and file B.lst
File A contains a hash table of words an ther frequency
File B contains a list of words
I have to generate a file C that contains the list of words form file A AND 
if a word form file A matches a word from the list in file B, there has to 
come "VZ" next to those specific words in file C.
How can I do this ???

thank you 



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

Date: Sun, 13 Dec 2009 06:48:11 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: newbie question
Message-Id: <slrnhi9oe4.5oj.tadmc@tadbox.sbcglobal.net>

scarlet <dirkmb@gmail.com> wrote:


> Subject: newbie question

Please put the subject of your article in the Subject of your article.


> I have two files : file A.tok and file B.lst
> File A contains a hash table of words an ther frequency
> File B contains a list of words
> I have to generate a file C that contains the list of words form file A AND 
> if a word form file A matches a word from the list in file B, there has to 
> come "VZ" next to those specific words in file C.
> How can I do this ???


I think you may need to write a program to do this.

If you get stuck, then post what you've written so far,
and we will help you fix it.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"


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

Date: Sun, 13 Dec 2009 15:03:12 +0100
From: "scarlet" <dirkmb@gmail.com>
Subject: Re: newbie question
Message-Id: <4b24f2b2$0$396$5f6aeac3@news.scarlet.nl>

Tad,
that is just the problem :(
i don't know how to write program.

greetings,


"Tad McClellan" <tadmc@seesig.invalid> schreef in bericht 
news:slrnhi9oe4.5oj.tadmc@tadbox.sbcglobal.net...
> scarlet <dirkmb@gmail.com> wrote:
>
>
>> Subject: newbie question
>
> Please put the subject of your article in the Subject of your article.
>
>
>> I have two files : file A.tok and file B.lst
>> File A contains a hash table of words an ther frequency
>> File B contains a list of words
>> I have to generate a file C that contains the list of words form file A 
>> AND
>> if a word form file A matches a word from the list in file B, there has 
>> to
>> come "VZ" next to those specific words in file C.
>> How can I do this ???
>
>
> I think you may need to write a program to do this.
>
> If you get stuck, then post what you've written so far,
> and we will help you fix it.
>
>
> -- 
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" 



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

Date: Sun, 13 Dec 2009 06:03:24 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: newbie question
Message-Id: <pps9i5lbq01ol4u9oq22kbk44t4uvkci7e@4ax.com>

"scarlet" <dirkmb@gmail.com> wrote:
[Subject: newbie question]

The first half of your subject is irrelevant and actually may cause some
people to score down your posting.
The second half is redundant because most initial postings involve a
question.

>I have two files : file A.tok and file B.lst
>File A contains a hash table of words an ther frequency
>File B contains a list of words
>I have to generate a file C that contains the list of words form file A AND 
>if a word form file A matches a word from the list in file B, there has to 
>come "VZ" next to those specific words in file C.
>How can I do this ???

What have you tried so far? Where are you stuck? Do you have a problem
with designing the algorithm? Or do you have a problem with a specific
function or feature? Or isn't your code doing what it is supposed to do?

Actually, your question smells a little bit like homework....

jue


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

Date: Sun, 13 Dec 2009 16:35:02 +0100
From: "scarlet" <dirkmb@gmail.com>
Subject: Re: newbie question
Message-Id: <4b25083c$0$395$5f6aeac3@news.scarlet.nl>

This is what I have already:

$file="VZ.lst";
open(FILE,"$file");
while ($lijn=<FILE>){


@words=split(/\n\,$lijn);
foreach $element(@words){


$in="krantenartikel.tok";
open(IN,"$in");
while ($lijn1=<IN>){
chomp $lijn1;
($token,$freq)=split(/\t/,$lijn1);
}


if ($element=$token){
$freq="VZ";
}
else {
$freq="";}
}
}
$out='#krantenartikel.vz#';
open(OUT,">$out");
print OUT "$token\t$freq\n";

First, I open the .lst file and define the array it contains. Then, I open 
the other file and make a table of the words and their frequency. I want to 
make a new file, "krantenartikel.vz", that contains the elements I mentioned 
earlier.

I know the command "if($element=$token) is wrong, but my problem is that I 
don't know how to do it otherwise, so it could work.
"Jürgen Exner" <jurgenex@hotmail.com> schreef in bericht 
news:pps9i5lbq01ol4u9oq22kbk44t4uvkci7e@4ax.com...
> "scarlet" <dirkmb@gmail.com> wrote:
> [Subject: newbie question]
>
> The first half of your subject is irrelevant and actually may cause some
> people to score down your posting.
> The second half is redundant because most initial postings involve a
> question.
>
>>I have two files : file A.tok and file B.lst
>>File A contains a hash table of words an ther frequency
>>File B contains a list of words
>>I have to generate a file C that contains the list of words form file A 
>>AND
>>if a word form file A matches a word from the list in file B, there has to
>>come "VZ" next to those specific words in file C.
>>How can I do this ???
>
> What have you tried so far? Where are you stuck? Do you have a problem
> with designing the algorithm? Or do you have a problem with a specific
> function or feature? Or isn't your code doing what it is supposed to do?
>
> Actually, your question smells a little bit like homework....
>
> jue 



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

Date: Sun, 13 Dec 2009 13:39:20 -0500
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: newbie question
Message-Id: <m28wd6k9bb.fsf@shermpendley.com>

"scarlet" <dirkmb@gmail.com> writes:

> that is just the problem :(
> i don't know how to write program.

In that case, you have a choice - learn how, or pay someone else to 
write it for you.

For the first option, <http://learn.perl.org/> is a good starting point.
For the second, job listings can be posted at <http://jobs.perl.org/>

sherm--


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

Date: Sun, 13 Dec 2009 10:48:23 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: newbie question
Message-Id: <v6aai5ldsifnebh5oiosb0khc6jsk9tr5c@4ax.com>

[Do not stealth-CC me, I happen to read the NGs I am posting in]
[Do not top-post, that is poor style; trying to repair]

"scarlet" <dirkmb@gmail.com> wrote:
>"Jürgen Exner" <jurgenex@hotmail.com> schreef in bericht 
>news:pps9i5lbq01ol4u9oq22kbk44t4uvkci7e@4ax.com...
>> "scarlet" <dirkmb@gmail.com> wrote:
>> [Subject: newbie question]
>>
>> The first half of your subject is irrelevant and actually may cause some
>> people to score down your posting.
>> The second half is redundant because most initial postings involve a
>> question.
>>
>>>I have two files : file A.tok and file B.lst
>>>File A contains a hash table of words an ther frequency
>>>File B contains a list of words
>>>I have to generate a file C that contains the list of words form file A 
>>>AND
>>>if a word form file A matches a word from the list in file B, there has to
>>>come "VZ" next to those specific words in file C.
>>>How can I do this ???
>>
>> What have you tried so far? Where are you stuck? Do you have a problem
>> with designing the algorithm? Or do you have a problem with a specific
>> function or feature? Or isn't your code doing what it is supposed to do?
>>
>> Actually, your question smells a little bit like homework....
>>
>> jue 

>This is what I have already:
>

Missing
	use strict; use warnings;

>$file="VZ.lst";
>open(FILE,"$file");

You should always test if an open() was successful:
	open(FILE,"$file") or die("Could not open $file because $!\n");

>while ($lijn=<FILE>){
>@words=split(/\n\,$lijn);

This line causes a syntax error. I think you meant 
	@words=split(/\n/,$lijn);
instead.

But I don't think it does what you meant it do to.
You are reading the file line by line. That means there is exactly one
newline at the very end of each string. Not much sense in splitting the
line at the very end. I think all you want here is a plain chomp() on
the line itself. Or if each line can contain multiple words then a
split() on white space or whatever separates those wordsm but not on
newline.

>foreach $element(@words){
>$in="krantenartikel.tok";

Proper indentation makes the scope of a loop and in particular nested
loops much, much easier to recognize.

>open(IN,"$in");

You should always test if an open() was successful:
	open(IN,"$in") or die("Could not open $in because $!\n");

>while ($lijn1=<IN>){
>chomp $lijn1; 

Good.

>($token,$freq)=split(/\t/,$lijn1);

Nice.

>}
>if ($element=$token){

As you noted yourself this is an assignment and certainly not what you
want. Even ($element==$token) would be wrong because it would compare
the numerical values of those two strings.
To compare the textual value of two scalars use 
	($element eq $token)

>$freq="VZ";
>} else {
>$freq="";}
>}
>}
>$out='#krantenartikel.vz#';
>open(OUT,">$out");

You should always test if an open() was successful:
	open(FILE,"$out") or die("Could not open $out because $!\n");

>print OUT "$token\t$freq\n";
>
>First, I open the .lst file and define the array it contains. Then, I open 
>the other file and make a table of the words and their frequency. I want to 
>make a new file, "krantenartikel.vz", that contains the elements I mentioned 
>earlier.

There are a few more conceptual and algorithmic problems with your code.

The most obvious issue is that you are printing only one single item to
your output file. This is because the outermost while() ends before the
print(), so the print will only be called exactly once at the very end
of the program. Had you used proper indentation then this would have
been obviuos (I actually ran your code through indent-region in emacs).

Same problem with the if(). It is executed AFTER the innermost while()
loop has already terminated, thus you are testing only against the very
last line of the krantenartikel.tok file.

Both issues can be fixed with little effort, but your code is also very
inefficient: for each line in VZ.lst you are looping through the while
krantenartikel.tok file. That is very costly, with O(n*m) it's a square
algorithm. It would be easy enough to do much better than that by just
reading all of krantenartikel.tok into memory once and then loop over
the in-memory copy. 

However Perl has s data structure that makes looking for "does X exist"
really trivial and very very fast: a hash. 

So, the revised plan is: 
- create a hash where the tokens from krantenartikel.tok are the keys
- open the output file
- open VZ.lst and for each word in that file
	check if it exists in the hash
	and print the proper output line
- close and cleanup everything

All together I am getting this code which compiles but which I couldn't
test further because I don't have any test data:

use strict; use warnings;

my %tokens;

my $in="krantenartikel.tok";
open(IN,"$in") or die("Cannot open $in: $!\n");
while (my $lijn1=<IN>){
    chomp $lijn1;
    my ($tok,$freq)=split(/\t/,$lijn1);
    $tokens{$tok} = $freq; 
#we don't really need to store the frequency, but because we
#need some dummy value anyway we can just as well use that one 
}
close(IN);

my $out='#krantenartikel.vz#';
open(OUT,">$out") or die("Cannot open $out: $!\n");
my $file="VZ.lst";
open(FILE,"$file") or die("Cannot open $file: $!\n");

while (my $lijn=<FILE>){
#I am assuming VZ.lst contains one word per line
    chomp $lijn;
    if (exists($tokens{$lijn})){
	print OUT "$lijn\tVZ\n";
    } else {
	print OUT "$lijn\n";
    }
}

close FILE;
close OUT or die("Problem closing $out: $!\n");



jue


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

Date: Sun, 13 Dec 2009 10:49:51 -0800
From: sln@netherlands.com
Subject: Re: newbie question
Message-Id: <cmdai5h84got3bt7pih6h5qrpjl90aatc2@4ax.com>

On Sun, 13 Dec 2009 13:36:51 +0100, "scarlet" <dirkmb@gmail.com> wrote:

>Hello,
>I have two files : file A.tok and file B.lst
>File A contains a hash table of words an ther frequency
>File B contains a list of words
>I have to generate a file C that contains the list of words form file A AND 
>if a word form file A matches a word from the list in file B, there has to 
>come "VZ" next to those specific words in file C.
>How can I do this ???
>
>thank you 

-sln
-----
the out:
d
e
f
cVZ
d
aVZ
z
bVZ aVZ
-----

use strict;
use warnings;

my $tokstring = "a   	afreq \n b 	bfreq \n c	cfreq  ";
my $bstring   = "d \ne \nf \nc \nd \na \nz \nb a\n ";

open my $tfile, '<', \$tokstring or die "can't open tok file: $!";
my %toks = map {/\s*([^\s]+)\s+([^\s]*)/, defined $1 ? ($1,$2) : ()} <$tfile>;
close $tfile;

open my $bfile, '<', \$bstring or die "can't open bstr file: $!";
while (<$bfile>)
{
	s/([^\s]+)(?=\s+)/exists $toks{$1} ? $1.'VZ': $1/ge;
	print;
}
close $bfile;



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

Date: Sun, 13 Dec 2009 11:05:03 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: newbie question
Message-Id: <8c60c2ff-226f-4d97-82ac-d62624d83bc1@k17g2000yqh.googlegroups.com>

On Dec 13, 7:36=A0am, "scarlet" <dir...@gmail.com> wrote:
> Hello,
> I have two files : file A.tok and file B.lst

It would be helpful if you posted a sample of each file, so we would
know exactly what the files look like.

CC.


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

Date: Sun, 13 Dec 2009 12:01:25 -0800
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: Scripts & Modules
Message-Id: <4b2547ad$0$7852$ed362ca5@nr5-q3a.newsreader.com>

Ben Morrow wrote:
> Quoth Xho Jingleheimerschmidt <xhoster@gmail.com>:
>> Bigus wrote:
>> ...
>>
>>> However, mod1function1() needs to call mod2function1(), so "use module2;" is 
>>> placed inside mod1function1().
>>>
>>> At a later date script1.cgi needs to use mod2function2(). in addition to 
>>> mod1function1(), so "use module2;" is placed directly into script1.cgi.
>>>
>>> So we now have a scenario where script1.cgi is loading both modules but when 
>>> mod1function1() is called it loads module1.pm again (or does it?)
>> No, for two reasons.  One is that the "use" inside the function happens 
>> when the function is compiled, not when it is called.
>>
>> And second, the module is not loaded twice because perl keeps track of 
>> such things.  It's "import" subroutine, if any, is executed twice, however.
> 
> No, since the file is (normally) only compiled once. If you 'do' a file
> that 'use's a module twice, import will get called twice ('do' updates
> %INC but doesn't check it); similarly if you eval a 'use' twice.

I'm not sure what you are trying to correct here, but if you "use" a 
module from more than one place, which is what the OP was talking about, 
its import sub certainly does get called more than once.

Xho


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

Date: Sun, 13 Dec 2009 21:43:20 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Scripts & Modules
Message-Id: <oo3gv6-2hk.ln1@osiris.mauzo.dyndns.org>


Quoth Xho Jingleheimerschmidt <xhoster@gmail.com>:
> Ben Morrow wrote:
> > Quoth Xho Jingleheimerschmidt <xhoster@gmail.com>:
> >> Bigus wrote:
> >> ...
> >>
> >>> However, mod1function1() needs to call mod2function1(), so "use
> module2;" is 
> >>> placed inside mod1function1().
> >>>
> >>> At a later date script1.cgi needs to use mod2function2(). in addition to 
> >>> mod1function1(), so "use module2;" is placed directly into script1.cgi.
> >>>
> >>> So we now have a scenario where script1.cgi is loading both modules
> but when 
> >>> mod1function1() is called it loads module1.pm again (or does it?)
> >> No, for two reasons.  One is that the "use" inside the function happens 
> >> when the function is compiled, not when it is called.
> >>
> >> And second, the module is not loaded twice because perl keeps track of 
> >> such things.  It's "import" subroutine, if any, is executed twice, however.
> > 
> > No, since the file is (normally) only compiled once. If you 'do' a file
> > that 'use's a module twice, import will get called twice ('do' updates
> > %INC but doesn't check it); similarly if you eval a 'use' twice.
> 
> I'm not sure what you are trying to correct here, but if you "use" a 
> module from more than one place, which is what the OP was talking about, 
> its import sub certainly does get called more than once.

I think perhaps I was confused :). The situation I was envisaging was

    script.pl:
        use Module1;
        use Module1;

    Module1.pm:
        use Module2;

which will only call Module2->import once; this is not, however, what
the OP was talking about.

Ben



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

Date: Sun, 13 Dec 2009 13:22:15 -0800 (PST)
From: laredotornado <laredotornado@zipmail.com>
Subject: Searching all instances of a pattern across multi-lines
Message-Id: <72597928-dd3f-48a8-94ea-d273ba24dc1a@g1g2000pra.googlegroups.com>

Hi,

I'm using Perl 5.8.8 on Mac 10.5.6.  I found this script online for
matching a pattern across multiple lines.  The problem is, it only
prints out one instance of the expression, and I would like it to
print out all instances.  What can I change so that it will print out
all instances?


#!/usr/bin/perl
use strict;
use warnings;

open(my $file, "<", "myfile.txt")
    or die "Can't open file: $!";
my $text = do { local $/; <$file> };

if ($text =~ /(<\s*script[^<]*>.*?<\/script>)/gs) {
    print $1;
}



Thanks,  - Dave


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

Date: Sun, 13 Dec 2009 21:45:50 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Searching all instances of a pattern across multi-lines
Message-Id: <et3gv6-2hk.ln1@osiris.mauzo.dyndns.org>


Quoth laredotornado <laredotornado@zipmail.com>:
> 
> I'm using Perl 5.8.8 on Mac 10.5.6.  I found this script online for
> matching a pattern across multiple lines.  The problem is, it only
> prints out one instance of the expression, and I would like it to
> print out all instances.  What can I change so that it will print out
> all instances?
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> open(my $file, "<", "myfile.txt")
>     or die "Can't open file: $!";
> my $text = do { local $/; <$file> };
> 
> if ($text =~ /(<\s*script[^<]*>.*?<\/script>)/gs) {
>     print $1;
> }

See 'while' in perldoc perlsyn and m// under "Regexp Quote-like
Operators" in perldoc perlop.

Ben



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

Date: Sun, 13 Dec 2009 13:52:24 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Searching all instances of a pattern across multi-lines
Message-Id: <001a7d4f-1bec-45c6-aac7-5c6f6e7ef7e9@h14g2000pri.googlegroups.com>

On Dec 13, 1:22=A0pm, laredotornado <laredotorn...@zipmail.com> wrote:
> Hi,
>
> I'm using Perl 5.8.8 on Mac 10.5.6. =A0I found this script online for
> matching a pattern across multiple lines. =A0The problem is, it only
> prints out one instance of the expression, and I would like it to
> print out all instances. =A0What can I change so that it will print out
> all instances?
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> open(my $file, "<", "myfile.txt")
> =A0 =A0 or die "Can't open file: $!";
> my $text =3D do { local $/; <$file> };
>
> if ($text =3D~ /(<\s*script[^<]*>.*?<\/script>)/gs) {
> =A0 =A0 print $1;
>
> }

print $1 while $text =3D~ /(<\s*script[^<]*>.*?<\/script>)/gs;

--
Charles DeRykus



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

Date: Sun, 13 Dec 2009 13:20:57 +0000 (UTC)
From: bks@panix.com (Bradley K. Sherman)
Subject: Re: Simple loop error
Message-Id: <hg2pnp$4gf$1@reader1.panix.com>

In article <slrnhi9eva.aeu.hjp-usenet2@hrunkner.hjp.at>,
Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
>On 2009-12-12 17:52, Bradley K. Sherman <bks@panix.com> wrote:
>> In article <cQPUm.29534$3M1.8149@newsfe18.ams2>,
>>>
>>>for ($i = 0; $i <= 1; $i += 0.01) {
>>>  print "$i\n";
>>>}
>>
>> It is ancient knowledge that one does not compare IEEE Standard 754
>> floating point numbers for equality.
> ...
>
>And it's irrelevant in this case because he *didn't* compare for
>equality.
>

Yes, he did, and then complained about it.   See original
message.

    --bks



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

Date: Sun, 13 Dec 2009 14:35:14 +0100
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: Simple loop error
Message-Id: <86aaxnq9nx.fsf@gareth.avalon.lan>

dan <spam.meplease@ntlworld.com> writes:

> On Sat, 12 Dec 2009 16:49:12 +0000, dan wrote:
>
>> The following code does not print 1, because it goes from 0.81 to
>> 0.820000000000001. This is quite a big deal isn't it?
>> 
>> for ($i = 0; $i <= 1; $i += 0.01) {
>>   print "$i\n";
>> }
>> 
>> Using perl v5.10.0
>
> Over time I have posted a few questions to this newsgroup, and have been 
> impressed with the level of expertise and helpfulness of the anwers. 
> However with respect to this issue, a hint of hysteria has crept in to 
> some of the responses. Probably because to the uninitiated, it appears 
> that floating-point arithmetic is simply broken. 

Here's the problem: when it comes to floating point math and it's
implementation in an inherently fixed-point platform such as a digital
computer, a programmer is not supposed to be uninitiated.

Mart

-- 
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.


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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 2719
***************************************


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