[19952] in Perl-Users-Digest
Perl-Users Digest, Issue: 2147 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 17 03:10:31 2001
Date: Sat, 17 Nov 2001 00:10:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1005984612-v10-i2147@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 17 Nov 2001 Volume: 10 Number: 2147
Today's topics:
Median of Three (Mark Jason Dominus)
Re: Median of Three <joe+usenet@sunstarsys.com>
Re: Median of Three <godzilla@stomp.stomp.tokyo>
Re: Median of Three (Logan Shaw)
Re: Median of Three (Logan Shaw)
Re: Median of Three <blp@cs.stanford.edu>
Re: Median of Three (Keith Keller)
Re: ranged arrays (Mark Jason Dominus)
The Web Displays "Content-type: text/html". Help! <sj88@cornell.edu>
Re: The Web Displays "Content-type: text/html". Help! <slytobias@home.com>
Re: Which ISPs support perl scripts? <nobody@nowhere.com>
Re: Which ISPs support perl scripts? <nobody@nowhere.com>
Re: Which ISPs support perl scripts? (David Efflandt)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 17 Nov 2001 04:18:22 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Median of Three
Message-Id: <3bf5e50b.3d71$11@news.op.net>
In an early article about programming style, Kernighan and Plaugher
look at an example where the programmer is trying to compute the
minimum of three values, approximately as follows:
if ($x > $y) {
if ($y > $z) {
$min = $z;
} else {
$min = $y;
}
} else {
if ($x > $z) {
$min = $z;
} else {
$min = $x;
}
}
They point out that you can make the code much shorter and simpler
like this:
$min = $x;
if ($min > $y) { $min = $y }
if ($min > $z) { $min = $z }
This also generalizes better to more than three items.
Suppose you want to find the median of three values instead of the
minimum. Is there an analogous short version?
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: 17 Nov 2001 01:51:46 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Median of Three
Message-Id: <m3itc928l9.fsf@mumonkan.sunstarsys.com>
[clp.moderated dropped from Newsgroups]
mjd@plover.com (Mark Jason Dominus) writes:
> They point out that you can make the code much shorter and simpler
> like this:
>
> $min = $x;
> if ($min > $y) { $min = $y }
> if ($min > $z) { $min = $z }
>
> This also generalizes better to more than three items.
>
> Suppose you want to find the median of three values instead of the
> minimum. Is there an analogous short version?
$med = $x;
($Y,$Z) = ($y < $z) ? ($y,$z) : ($z,$y); # ensures $Y <= $Z
if ($med < $Y) { $med = $Y }
if ($med > $Z) { $med = $Z }
I don't see any obvious generalization of this to more than 3 items;
nor do I think this is any different from pulling out the middle term
of a 3-item sort().
--
Joe Schaefer "Life is just one damn thing after another."
--Mark Twain
------------------------------
Date: Fri, 16 Nov 2001 23:27:25 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Median of Three
Message-Id: <3BF6115D.97063DE1@stomp.stomp.tokyo>
Mark Jason Dominus wrote:
(snipped)
> Suppose you want to find the median of three values instead of the
> minimum. Is there an analogous short version?
For a number set containing three entries, the median is always
the second number in the set. No math is needed to find it.
This applies to any odd numbered number set. The median is
the middle number; the entry with an equal number of entries
before and after the entry.
No complicated math is involved nor are complicated formulas
needed. A median solution can be found for either a number set
with an odd number of entries or, a number set with an even
number of entries using simple arithmetic; one liners.
In almost all cases, save for complex decimal numbers,
a median can be calculated mentally by simply visualizing
a typical number line. Same is nearly true for histograms.
This simple visualization does not hold quite so true for
geometric figure medians unless, you are imaginative.
I have removed your cross-posting to various groups.
My personal opinion is this wastes bandwidth and leads
to spamming of groups not interested in an article.
Additionally it is inappropriate to cross-post to a
moderated newsgroup. This is unfair to a moderator;
she or he is instantly mail bombed.
I do not condone mail bombing a newsgroup moderator.
Godzilla!
--
TEST SCRIPT:
____________
#!perl
@Array = (3, 5, 7);
$median = $Array[($#Array / 2)];
print $median;
print "\n\n";
@Array = (2, 4, 6, 8);
$median = $Array[int($#Array / 2)] + (($Array[int($#Array / 2) + 1] - $Array[int($#Array / 2)]) / 2);
print $median;
exit;
------------------------------
Date: 17 Nov 2001 01:41:24 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Median of Three
Message-Id: <9t54b4$all$1@starbuck.cs.utexas.edu>
[ Note: This time I removed comp.lang.perl.moderated from
the Newsgroups: line. ]
In article <3bf5e50b.3d71$11@news.op.net>,
Mark Jason Dominus <mjd@plover.com> wrote:
>They point out that you can make the code much shorter and simpler
>like this:
>
> $min = $x;
> if ($min > $y) { $min = $y }
> if ($min > $z) { $min = $z }
>
>This also generalizes better to more than three items.
>
>Suppose you want to find the median of three values instead of the
>minimum. Is there an analogous short version?
This is certainly analogous in a way, although I'm pretty sure it only
works for three numbers and it's ugly:
$average = ($x + $y + $z) / 3;
$median = $x;
if (abs ($y - $average) < abs ($median - $average)) { $median = $y; }
if (abs ($z - $average) < abs ($median - $average)) { $median = $z; }
This relies on the idea the median of three numbers is the closest
number to the average of those three numbers. So what it's really
doing is finding the minimum difference between the average and the
median.
The proof that this is correct is probably something like this:
* The average always falls between the two extreme points on
the number line.
* The average falls on one side or the other of the median.
* If the average falls to the left of the median, it falls
closer to the median than to the minimum. The average of all
three points must be greater than the average of the median and
the minimum (because the average of the three points also
includes the maximum which is greater than either). So the
average of all three points is to the right of the midpoint
between the minimum and medium, meaning the average is closer to
the median than to the minimum.
* Similar (symmetrical) reasoning applies if the average falls
to the right side of the median.
* Special cases (median == average, some of the numbers equal,
etc.) are omitted, but they're fairly straightforward.
I can't imagine that this code is something you'd want to use in a real
program...
- Logan
--
"In order to be prepared to hope in what does not deceive,
we must first lose hope in everything that deceives."
Georges Bernanos
------------------------------
Date: 17 Nov 2001 01:46:45 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Median of Three
Message-Id: <9t54l5$aoh$1@starbuck.cs.utexas.edu>
In article <9t54b4$all$1@starbuck.cs.utexas.edu>,
Logan Shaw <logan@cs.utexas.edu> wrote:
>[ Note: This time I removed comp.lang.perl.moderated from
> the Newsgroups: line. ]
This refers to my original post, which will never show up, since I'm
not that excited about bothering to register so my post can make it to
comp.lang.perl.moderated.
So I'll just say here again what I said in that post.
One simple way to do it is just write this:
$median = (sort $x, $y, $z)[1];
I'm not sure if that's in the spirit of the original challenge or not.
If not, see my previous post.
- Logan
--
"In order to be prepared to hope in what does not deceive,
we must first lose hope in everything that deceives."
Georges Bernanos
------------------------------
Date: 16 Nov 2001 22:25:18 -0800
From: Ben Pfaff <blp@cs.stanford.edu>
Subject: Re: Median of Three
Message-Id: <87elmx6hip.fsf@pfaff.stanford.edu>
mjd@plover.com (Mark Jason Dominus) writes:
> In an early article about programming style, Kernighan and Plaugher
> look at an example where the programmer is trying to compute the
> minimum of three values, approximately as follows:
>
> if ($x > $y) {
> if ($y > $z) {
> $min = $z;
> } else {
> $min = $y;
> }
> } else {
> if ($x > $z) {
> $min = $z;
> } else {
> $min = $x;
> }
> }
>
> They point out that you can make the code much shorter and simpler
> like this:
>
> $min = $x;
> if ($min > $y) { $min = $y }
> if ($min > $z) { $min = $z }
>
> This also generalizes better to more than three items.
>
> Suppose you want to find the median of three values instead of the
> minimum. Is there an analogous short version?
I think the answer to this question is "no". I will try to back
up that answer with some less-than-airtight arguments. In the
process, I'll assume that x, y, and z all have different values.
This is mostly a mathematical or computer science question, not a
programming or Perl question, so I'll use language-neutral
notations in my discussion.
First: Suppose that we compare x and y, as in the outer
comparison in the long form above. Afterward, we are able to
eliminate either x or y as the minimum value. But regardless of
the result, either could still be the median, so we don't learn
as much from a single comparison in the median case.
Second (really just a restatement of the first): There are six
possible orderings for a triple of different real numbers:
x < y < z x < z < y z < x < y
y < x < z y < z < x z < y < x
Suppose again that we first compare x and y, as in either form
above. Whether we are looking for the minimum or the median,
this comparison eliminates either the first or the second line of
possibilities. If x < y, then there are only two possibilities
left for the minimum (either x or z), so we can decide in one
more comparison; but there are still three possibilities left for
the median, so we need two more comparisons. So the median is
inherently more difficult to compute than the minimum, even if
there are only three values, and we can't really expect that the
code for it will be as simple.
Third: Suppose that we're using an array `p' of three elements
p(0), p(1), p(2), instead of three discrete values. Let each of
the Boolean variables a, b, c the value 1 if the expression p(0)
< p(1), p(0) < p(2), p(1) < p(2), respectively, is true, 0
otherwise. Finally, let the output be the 2-bit index into `p',
one of 00, 01, or 10 (representing 0, 1, or 2), of the minimum or
the median, as desired. Adopting the typical digital logic text
notation of juxtaposition as logical AND, suffix of ' as logical
NOT, and addition as logical OR, we come up with the following as
minimal sum-of-products solutions for the MSB and LSB of the
minimum:
MSB: b'c'
LSB: a'c
For the median, on the other hand:
MSB: b'c + bc'
LSB: a'b'c' + abc
This looks to me like further evidence that median is more
complex than minimum, even among only three choices.
*shrug* At this point I'm pretty convinced that there's no
simple solution. Maybe someone cleverer than me can come up with
one. I'd be surprised, but in a pleasant way.
--
<blp@cs.stanford.edu> <pfaffben@msu.edu> <pfaffben@debian.org> <blp@gnu.org>
Stanford PhD Student - MSU Alumnus - Debian Maintainer - GNU Developer
Personal webpage: http://www.msu.edu/~pfaffben
------------------------------
Date: Fri, 16 Nov 2001 23:40:24 -0800
From: kkeller@speakeasy.net (Keith Keller)
Subject: Re: Median of Three
Message-Id: <8945t9.vbs.ln@wombat.san-francisco.ca.us>
Hash: SHA1
In article <3bf5e50b.3d71$11@news.op.net>,
mjd@plover.com (Mark Jason Dominus) writes:
>
> In an early article about programming style, Kernighan and Plaugher
> look at an example where the programmer is trying to compute the
> minimum of three values, approximately as follows:
[snip]
> They point out that you can make the code much shorter and simpler
> like this:
>
> $min = $x;
> if ($min > $y) { $min = $y }
> if ($min > $z) { $min = $z }
>
> This also generalizes better to more than three items.
>
> Suppose you want to find the median of three values instead of the
> minimum. Is there an analogous short version?
This should work, but I don't think it's easily generalizable:
$i = $x; $j = $y;
if ($x > $y) {$i = $y; $j = $x;}
if ($i > $z) { $med = $i }
if ($j > $z) { $med = $z }
$med = $j;
The difficulty lies in the difference between the median and the
minimum: the minimum is less than everything, so it's easier to
generalize than the median, which is less than some items but
greater than others. Thus the extra comparison (and an additional
implicit comparison) and dummy variables.
I believe the above will work if any or all of the values are
equal, which is quite nice.
- --keith
- --
kkeller@speakeasy.net
public key: http://wombat.san-francisco.ca.us/kkeller/public_key
alt.os.linux.slackware FAQ: http://wombat.san-francisco.ca.us/perl/fom
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iEYEARECAAYFAjv2FFcACgkQhVcNCxZ5ID/sHQCfVGHc33EtbZJbeeZ1E6o8FkfP
pk4AmgInRaQ0INa4aWpAcVhDi0x7axqi
=S7jm
-----END PGP SIGNATURE-----
------------------------------
Date: Fri, 16 Nov 2001 18:18:02 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: ranged arrays
Message-Id: <3bf55858.2e1b$368@news.op.net>
In article <3bf2ddc2.6d2c$d6@news.op.net>,
Mark Jason Dominus <mjd@plover.com> wrote:
>In article <9su1ms$c5u$00$1@news.t-online.com>,
>Steffen Müller <tsee@gmx.net> wrote:
>> This and most of the rest work fine, but there's something that makes
>>the whole attempt futile: Instead of passing negative index values to the
>>appropriate methods, perl translates them to non-negatives the way we're
>>used to with arrays starting at 0 by using FETCHSIZE.
>
>Yes, that's a drag, isn't it? As i recall this came up on p5p at a
>time and a decision to make it work the way it does was taken. That
>is, it was not an oversight.
Nick Ing-Simmons says it was not an oversight:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-05/msg00454.html
I now have a patch that allows the use of negative indices in tied
arrays, without breaking backward compatibility, but some of the
DB_File tests are failing when I put it in, so I haven't sent it to
p5p for inclusion. Here it is, if you are interested:
--- av.c 2001/11/15 15:03:05 1.1
+++ av.c 2001/11/15 16:42:50
@@ -176,14 +176,20 @@
Perl_av_fetch(pTHX_ register AV *av, I32 key, I32 lval)
{
SV *sv;
+ unsigned char neg_index = 0;
if (!av)
return 0;
if (key < 0) {
- key += AvFILL(av) + 1;
- if (key < 0)
- return 0;
+ if (SvRMAGICAL(av)) {
+ neg_index = MGp_NEG_INDEX;
+ key += mg_size((SV *) av) + 1;
+ } else {
+ key += AvFILLp(av) + 1;
+ }
+ if (key < 0)
+ return 0;
}
if (SvRMAGICAL(av)) {
@@ -192,6 +198,7 @@
{
sv = sv_newmortal();
mg_copy((SV*)av, sv, 0, key);
+ SvMAGIC(sv)->mg_private |= neg_index;
PL_av_fetch_sv = sv;
return &PL_av_fetch_sv;
}
--- mg.c 2001/11/15 16:46:02 1.1
+++ mg.c 2001/11/15 17:04:31
@@ -1240,6 +1240,10 @@
if (n > 2) {
PUSHs(val);
}
+ if (mg->mg_private) {
+ EXTEND(SP, 1);
+ PUSHs(newSViv((IV)mg->mg_private));
+ }
PUTBACK;
return call_method(meth, flags);
--- mg.h 2001/11/15 16:38:55 1.1
+++ mg.h 2001/11/15 16:39:49
@@ -36,6 +36,9 @@
#define MGf_MINMATCH 1
+/* MGp: Flags set in mg_private 20011115 mjd-perl-patch+@plover.com */
+#define MGp_NEG_INDEX 1
+
#define MgTAINTEDDIR(mg) (mg->mg_flags & MGf_TAINTEDDIR)
#define MgTAINTEDDIR_on(mg) (mg->mg_flags |= MGf_TAINTEDDIR)
#define MgTAINTEDDIR_off(mg) (mg->mg_flags &= ~MGf_TAINTEDDIR)
--- pod/perltie.pod 2001/11/15 17:09:01 1.1
+++ pod/perltie.pod 2001/11/15 17:12:04
@@ -258,7 +258,21 @@
If a negative array index is used to read from an array, the index
will be translated to a positive one internally by calling FETCHSIZE
-before being passed to FETCH.
+before being passed to FETCH and a special third argument will be
+passed to FETCH. The third argument is a flag vector. If the low bit
+of this argument is set, it means the original index was negative.
+You can detect and handle negative indices as follows:
+
+ sub FETCH {
+ my $self = shift;
+ my $index = shift;
+ my $flags = shift;
+ if (defined $flags && $flags & 1) {
+ # the index was originally negative
+ $index -= $self->FETCHSIZE;
+ }
+ # Now do something with $self and $index.
+ }
As you may have noticed, the name of the FETCH method (et al.) is the same
for all accesses, even though the constructors differ in names (TIESCALAR
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Fri, 16 Nov 2001 22:22:29 -0500
From: "Shi Jin" <sj88@cornell.edu>
Subject: The Web Displays "Content-type: text/html". Help!
Message-Id: <9t4l5h$r55$1@news01.cit.cornell.edu>
It's very strange that my web using Perl as CGI displays the
"Content-type:text/html" header.
This is the case under linux. But in windows, it works fine.
Anyone would help me?
Thanks.
------------------------------
Date: Sat, 17 Nov 2001 04:38:46 GMT
From: David Sletten <slytobias@home.com>
Subject: Re: The Web Displays "Content-type: text/html". Help!
Message-Id: <3BF5EA13.9080000@home.com>
You presumably are using Apache on Linux? Apache may include this header
line for you depending on your configuration. The 'Content-type' that
your script outputs is then redundant. It becomes part of the body of
the HTTP response and is rendered by the browser. Just get rid of it if
this is happening.
David Sletten
Shi Jin wrote:
> It's very strange that my web using Perl as CGI displays the
> "Content-type:text/html" header.
> This is the case under linux. But in windows, it works fine.
> Anyone would help me?
> Thanks.
>
>
>
------------------------------
Date: Sat, 17 Nov 2001 13:12:14 +1000
From: "Gregory Toomey" <nobody@nowhere.com>
Subject: Re: Which ISPs support perl scripts?
Message-Id: <txkJ7.331581$8x1.93736@newsfeeds.bigpond.com>
"Chris Clarke" <mad-biker@couplands-well.freeserve.co.uk> wrote in message
news:9t4duk$f9k$1@news8.svr.pol.co.uk...
> As an absolute perl beginner... how do I find an ISP which supports perl
> scripts? I am currently with Freeserve, and the ISP would have to be
free.
http://hostsearch.com/
http://webhostingtalk.com/
> I tried a search with Ask Jeeves for ISPs, to no avail. I know you guys
are
> WAY ahead of me technical-wise, but I've done some programming, and would
> like to learn perl. Freeserve doesn't have the facility for CGI or perl
> scripts. Are there any (free) ISPs that you know of who do?
I don't know of any free ones.
I use http://aussiehosts.com/hosting.html & they offer introductory $USD20
p.a. Linux/MS
gtoomey
------------------------------
Date: Sat, 17 Nov 2001 13:15:23 +1000
From: "Gregory Toomey" <nobody@nowhere.com>
Subject: Re: Which ISPs support perl scripts?
Message-Id: <qAkJ7.331584$8x1.93615@newsfeeds.bigpond.com>
There are also books that come with free Activestate Perl that can be
installed on your PC e.g. Perl for Dummies.
gtoomey
------------------------------
Date: Sat, 17 Nov 2001 03:55:43 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Which ISPs support perl scripts?
Message-Id: <slrn9vbntv.kgn.efflandt@typhoon.xnet.com>
On Sat, 17 Nov 2001, Chris Clarke
<mad-biker@couplands-well.freeserve.co.uk> wrote:
> As an absolute perl beginner... how do I find an ISP which supports perl
> scripts? I am currently with Freeserve, and the ISP would have to be free.
> I tried a search with Ask Jeeves for ISPs, to no avail...
Maybe because you are searching for the wrong thing. An ISP is Internet
Service Provider (dialup, dsl, cablemodem, etc.). What you want to search
for is "web hosting".
You can install a web server and Perl on your PC (www.activestate.com).
Although, I prefer Linux or FreeBSD (which both come with apache and
Perl and can do things Windows doesn't).
--
David Efflandt - All spam is ignored - http://www.de-srv.com/
http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 2147
***************************************