[26125] in Perl-Users-Digest
Perl-Users Digest, Issue: 8318 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 12 11:05:42 2005
Date: Fri, 12 Aug 2005 08:05:06 -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, 12 Aug 2005 Volume: 10 Number: 8318
Today's topics:
difference in arrays/hashes <me@invalid.domain>
Re: difference in arrays/hashes <noreply@gunnar.cc>
Re: Perl numerics: EPSILON and equality between floats (Anno Siegel)
Re: Perl numerics: EPSILON and equality between floats <please_post@nomail.edu>
Re: Perl numerics: EPSILON and equality between floats (Anno Siegel)
Re: Perl numerics: EPSILON and equality between floats <please_post@nomail.edu>
Re: regex in perl (using variables) <dariosmece@yahoo.com>
Re: regex in perl (using variables) <noreply@gunnar.cc>
Re: regex in perl (using variables) <dariosmece@yahoo.com>
Re: regex in perl (using variables) <noreply@gunnar.cc>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 12 Aug 2005 14:26:21 +0200
From: Me <me@invalid.domain>
Subject: difference in arrays/hashes
Message-Id: <MISdnZ2dnZ02gPj0nZ2dneoIYd-dnZ2dRVn-z52dnZ0@is.co.za>
I have two data sets from two sources, each set of results is stored in
files which are ared into @file1 and @file2.
"man perlfaq4" gives an exampe of an intersection of the two. My
problems is, I need to know which elements of @file1 are not in @file2
and the other way around. How can I keep track of this using the example
from the man page.
------------------------------
Date: Fri, 12 Aug 2005 15:02:34 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: difference in arrays/hashes
Message-Id: <3m3kvgF158icpU1@individual.net>
Me wrote:
> I have two data sets from two sources, each set of results is stored in
> files which are ared into @file1 and @file2.
>
> "man perlfaq4" gives an exampe of an intersection of the two. My
> problems is, I need to know which elements of @file1 are not in @file2
> and the other way around. How can I keep track of this using the example
> from the man page.
The most important part of the "perldoc -q intersection" answer is the
first sentence: "Use a hash." ;-)
The rest is an example, and you need to adopt it to fit your particular
situation.
my %count;
foreach my $element (@file1, @file2) { $count{$element}++ }
my @unique1 = grep $count{$_} == 1, @file1;
my @unique2 = grep $count{$_} == 1, @file2;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 12 Aug 2005 10:28:36 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Perl numerics: EPSILON and equality between floats
Message-Id: <ddhtkk$iuq$1@mamenchi.zrz.TU-Berlin.DE>
bill <please_post@nomail.edu> wrote in comp.lang.perl.misc:
>
>
>
>
> I just recently came across this snippet:
>
> my $EPSILON = 1;
> $EPSILON /= 2 while 0.5 + $EPSILON/2 > 0.5;
>
> where $EPSILON was later used, e.g., to compare floats for equality:
>
> sub float_equal {
> my ($x, $y) = @_;
> return !($x||$y) || ($x+$y) && abs(($x-$y)/($x+$y)) < $EPSILON;
> }
>
> (The above is the gist of what I recall, not verbatim production
> code, but I think I got it right.)
>
> I'm not very knowledgeable about numerics, so I was wondering
> whether this was the best way to compare floats for equality.
>
> I thought that this would be a FAQ, but I couldn't find it.
>
> The first edition of The Perl Cookbook recommends converting both
> numbers to the same string format, and then doing a string comparison:
>
> sub equal {
> my ($A, $B, $dp) = @_;
> return sprintf("%.${dp}g", $A) eq sprintf("%.${dp}g", $B);
> }
For an ad-hoc approach I simply use "eq" on the float-point arguments
(without sprintf). "If it prints the same, it is the same."
> The main reason I don't like this approach is that, in my experience,
> some numerical objects, such as Math::BigFloat and Math::Pari,
> don't work well with sprintf.
For comparison of non-native number formats you'll have to resort to
special methods. Unless the package comes with its own sprintf(),
sprintf-based methods won't work. The epsilon-finding procedure
would probably loop forever.
> Also, I recall that in C there are pre-defined constants (e.g.
> DBL_EPSILON in limits.h) that can be used for this kind of tests.
> Is there something similar in Perl?
Those constants are in POSIX.
> P.S. If someone in Perl publishing reads this: a book on Perl
> numerics is sorely needed (fundamentals, algorithms, recipes, best
> practices) is sorely needed.
Perl doesn't have its own numerics, it uses the native operations
of the C compiler. A book about Perl numerics would have to be one
about computer numerics in general, which abound.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Fri, 12 Aug 2005 12:38:10 +0000 (UTC)
From: bill <please_post@nomail.edu>
Subject: Re: Perl numerics: EPSILON and equality between floats
Message-Id: <ddi57i$67h$1@reader2.panix.com>
In <ddhtkk$iuq$1@mamenchi.zrz.TU-Berlin.DE> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>For an ad-hoc approach I simply use "eq" on the float-point arguments
>(without sprintf). "If it prints the same, it is the same."
Another objection I have against string-based methods (other than
the fact that it doesn't work well with certain numeric classes)
is the loss of precision incurred by translating to base 10. If
there were a way to do a "binary eq", where the string representation
was in base 2 instead of base 10, your approach would be fine with
me.
>> Also, I recall that in C there are pre-defined constants (e.g.
>> DBL_EPSILON in limits.h) that can be used for this kind of tests.
>> Is there something similar in Perl?
>Those constants are in POSIX.
Thanks!
>If you want to post a followup via groups.google.com, don't use
>the broken "Reply" link at the bottom of the article. Click on
>"show options" at the top of the article, then click on the
>"Reply" at the bottom of the article headers.
Is it my imagination or did Google Groups go to the dogs after they
"overhauled it" about a year ago? It's got to be the buggiest of
all Google services.
bill
------------------------------
Date: 12 Aug 2005 12:56:14 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Perl numerics: EPSILON and equality between floats
Message-Id: <ddi69e$o30$1@mamenchi.zrz.TU-Berlin.DE>
bill <please_post@nomail.edu> wrote in comp.lang.perl.misc:
> In <ddhtkk$iuq$1@mamenchi.zrz.TU-Berlin.DE>
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>
> >For an ad-hoc approach I simply use "eq" on the float-point arguments
> >(without sprintf). "If it prints the same, it is the same."
>
> Another objection I have against string-based methods (other than
> the fact that it doesn't work well with certain numeric classes)
> is the loss of precision incurred by translating to base 10. If
> there were a way to do a "binary eq", where the string representation
> was in base 2 instead of base 10, your approach would be fine with
> me.
But you use rounding, or epsilon fuzz, to get rid of spurious precision.
If you round off only a single decimal digit, you're giving up more
precision than the conversion to decimal costs in the first place.
Something like that "binary eq" could probably be written (in Perl
or otherwise), but what would it gain? In effect, it gives you a
choice of epsilons that are powers of two, instead of powers of ten
like rounding. The practical difference is small.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
------------------------------
Date: Fri, 12 Aug 2005 14:45:28 +0000 (UTC)
From: bill <please_post@nomail.edu>
Subject: Re: Perl numerics: EPSILON and equality between floats
Message-Id: <ddicm8$lcn$1@reader2.panix.com>
In <ddi69e$o30$1@mamenchi.zrz.TU-Berlin.DE> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>bill <please_post@nomail.edu> wrote in comp.lang.perl.misc:
>> In <ddhtkk$iuq$1@mamenchi.zrz.TU-Berlin.DE>
>> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>>
>> >For an ad-hoc approach I simply use "eq" on the float-point arguments
>> >(without sprintf). "If it prints the same, it is the same."
>>
>> Another objection I have against string-based methods (other than
>> the fact that it doesn't work well with certain numeric classes)
>> is the loss of precision incurred by translating to base 10. If
>> there were a way to do a "binary eq", where the string representation
>> was in base 2 instead of base 10, your approach would be fine with
>> me.
>But you use rounding, or epsilon fuzz, to get rid of spurious precision.
>If you round off only a single decimal digit, you're giving up more
>precision than the conversion to decimal costs in the first place.
>Something like that "binary eq" could probably be written (in Perl
>or otherwise), but what would it gain? In effect, it gives you a
>choice of epsilons that are powers of two, instead of powers of ten
>like rounding. The practical difference is small.
The effect is to leave it up to the user to decide, instead of
forcing him/her to rationalize a suboptimal situation after the
fact. The phrase "practical difference" makes a huge blanket
assumption about the task at hand. What's "good enough"? 10%?
1%? 0.0001%? The answer obviously depends on the application, and
my philosophy is to give as much power to the user as possible.
In this case, the best that can be done is to allow a binary
specification of precision, and let the user avail him/herself of
this as desired.
I suppose "binary eq" could be written using pack/unpack, but even
though I've been programming Perl for 8+ years and consider myself
pretty proficient at it, I remain a pack/unpack illiterate. For
some reason, even after I study pack/unpack intensively, its
operations seem mysterious to me, and therefore my minds retains
whatever I learn about it for roughly one nanosecond. Maybe I
should just read the perl source to finally understand it.
bill
------------------------------
Date: Fri, 12 Aug 2005 13:43:49 +0200
From: "dario" <dariosmece@yahoo.com>
Subject: Re: regex in perl (using variables)
Message-Id: <ddi1v5$rsm$1@bagan.srce.hr>
Thanks everyone! I figured it out! Begginers mistake!
I didn't use "chomp";
Dario
"dario" <dariosmece@yahoo.com> wrote in message
news:ddfeaf$73h$1@bagan.srce.hr...
> How do I make this work!!!
>
> $head_ ="Subject: Get cheap v i a g r a ..... ";
>
> #$rule is a variable which I used for reading text from a file!
> open (NWRULE, "<rule.spam");
> @new_rule=<NWRULE>;
> close (NWRULE);
>
> Then I did sometning like this :
> foreach $rule(@new_rule)
> {
> if($rule =~ /(\S+) (\S+) ([^\n]+)/)
> {
> $new_id=$1;
> $dio=$2;
> $reg=$3;
> if($head_ =~ m/$reg/)
> {
> print "something\n";
> }
> ....
> Content of a file rule.spam is :
> new_1 head Subject: .*\.\.
>
>
------------------------------
Date: Fri, 12 Aug 2005 14:06:10 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex in perl (using variables)
Message-Id: <3m3hlpF1547v4U1@individual.net>
dario wrote:
> Thanks everyone! I figured it out! Begginers mistake!
> I didn't use "chomp";
Not using chomp() may be a typical beginners mistake, but I fail to see
how that would make a difference with respect to the problem you had.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 12 Aug 2005 15:21:06 +0200
From: "dario" <dariosmece@yahoo.com>
Subject: Re: regex in perl (using variables)
Message-Id: <ddi7lk$ovf$1@bagan.srce.hr>
I don't know why but on my computer it really doesn't work if I don't use
chomp(like chomp($rule)). It has something to do with how linux, debian or
perl or whatever handles the newlines! This is why it works on windows
because windows have different way of handling newlines(I think). But I
think it has to work on all platforms. I posted my output from running the
program so that's really what i got.
Dario
"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:3m3hlpF1547v4U1@individual.net...
> dario wrote:
> > Thanks everyone! I figured it out! Begginers mistake!
> > I didn't use "chomp";
>
> Not using chomp() may be a typical beginners mistake, but I fail to see
> how that would make a difference with respect to the problem you had.
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 12 Aug 2005 16:58:38 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: regex in perl (using variables)
Message-Id: <3m3rp4F15alo2U1@individual.net>
dario wrote:
> Gunnar Hjalmarsson wrote:
>> Not using chomp() may be a typical beginners mistake, but I fail to see
>> how that would make a difference with respect to the problem you had.
>
> I don't know why but on my computer it really doesn't work if I don't use
> chomp(like chomp($rule)). It has something to do with how linux, debian or
> perl or whatever handles the newlines! This is why it works on windows
> because windows have different way of handling newlines(I think).
One thought is that you didn't convert the file in question to Unix
format when copying it from Windows. In that case the chomp()ing may
accidentally serve the purpose of removing the \r character.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
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 8318
***************************************