[19955] in Perl-Users-Digest
Perl-Users Digest, Issue: 2150 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 17 18:05:36 2001
Date: Sat, 17 Nov 2001 15:05:09 -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: <1006038309-v10-i2150@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 17 Nov 2001 Volume: 10 Number: 2150
Today's topics:
Re: Algorithm question -- need help <nobody@nowhere.com>
Re: Binary issues.. <bart.lateur@skynet.be>
Directory problem... again (BUCK NAKED1)
Re: Fast Portable Logical Shift Right in Perl ? <shelorm@earthlink.net>
Re: Fast Portable Logical Shift Right in Perl ? <shelorm@earthlink.net>
Re: Fast Portable Logical Shift Right in Perl ? (Logan Shaw)
Re: Fast Portable Logical Shift Right in Perl ? (Logan Shaw)
Re: Fast Portable Logical Shift Right in Perl ? <shelorm@earthlink.net>
Re: How can I use LWP::Simple inside a safe compartment (BUCK NAKED1)
Re: Median of Three (Peter J. Acklam)
Re: Median of Three <godzilla@stomp.stomp.tokyo>
Re: Median of Three <feistatduke@yahoo.com>
Re: Median of Three (Logan Shaw)
Re: Median of Three <shelorm@earthlink.net>
Re: Median of Three (Peter J. Acklam)
Re: Median of Three <godzilla@stomp.stomp.tokyo>
Re: Median of Three (Keith Keller)
Re: Median of Three <Al_Dunbar@HoTMaiL.com>
Re: Median of Three <Al_Dunbar@HoTMaiL.com>
Re: no more than 128 symbles <Laocoon@eudoramail.com>
Re: no more than 128 symbles <dolgopa@co.ru>
Re: no more than 128 symbles <stevenjm@scatercreek.com>
please help: good OOP guide for perl <toyshop@cm1.ethome.net.tw>
Re: Please Kindly Help With DBI <jeff@vpservices.com>
reading null terminated string from a file <mkmazie>
send HTTP response (paul)
Re: send HTTP response (David Efflandt)
Re: Which ISPs support perl scripts? (BUCK NAKED1)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 18 Nov 2001 07:47:45 +1000
From: "Gregory Toomey" <nobody@nowhere.com>
Subject: Re: Algorithm question -- need help
Message-Id: <gTAJ7.346585$8x1.98827@newsfeeds.bigpond.com>
"Arkady" <apinkhasov@hotmail.com> wrote in message
news:30d03e1.0111170540.196d9838@posting.google.com...
> I have a list of machines and their respective parents. One machine
> is a root, and the others follow from it. However, I cannot get the
> right idea for a kind of data sttructure to work properly and
> algorithm to populate it.
>
> If anybody has any suggestions, please do not hesistate to help.
>
Directed Acyclic Graph (or partial ordering)
gtoomey
------------------------------
Date: Sat, 17 Nov 2001 19:55:55 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Binary issues..
Message-Id: <f4gdvt063o4jb24qf3255pljgr7qjdut1h@4ax.com>
Clinton A. Pierce wrote:
>Perl has no problems with binary data. Since you mention AIM, is this Win32?
>If so, remember to binmode() filehandles that might be involved in reading/
>writing binary data. That's Windows' fault, not Perl's.
That's not enough. Sure, binmode() will help, you just have to get rid
of the "\r"'s yourself. But IU think the OP is aiming at mixing <FH> and
read() style calls. Which makes for a very good question.
--
Bart.
------------------------------
Date: Sat, 17 Nov 2001 16:02:05 -0600 (CST)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Directory problem... again
Message-Id: <26900-3BF6DE5D-85@storefull-244.iap.bryant.webtv.net>
I'm trying to change the last modified time on all files and directories
under $tmpdir. Why is my code below only changing the last mod time on
the 1st and 2nd level of directory "$tmpdir"? I've worried with this for
hours! Please tell me what am I doing wrong?
sub mod_time {
chdir "/home/public_html" or die "Cant chgdirec: $!\n";
opendir TDIR, "$tmpdir" or die "can't Open TDIR: $!\n";
my $now = time;
@modtime = grep { /^\./ && "$tmpdir/$_" } readdir TDIR;
foreach $file(@modtime) {
utime $now, $now, "@modtime" or die
"can't change time on $tmpdir: $!\n"; }
closedir TDIR or die "Cant close TDIR: $!\n";
}
Regards,
Dennis
------------------------------
Date: Sat, 17 Nov 2001 19:54:45 GMT
From: "Mark Shelor" <shelorm@earthlink.net>
Subject: Re: Fast Portable Logical Shift Right in Perl ?
Message-Id: <9gzJ7.49271$hZ.4691389@newsread2.prod.itd.earthlink.net>
"Joe Schaefer" <joe+usenet@sunstarsys.com> wrote in message
news:m3snbe174k.fsf@mumonkan.sunstarsys.com...
> "Mark Shelor" <shelorm@earthlink.net> writes:
>
> > use integer;
> >
> > sub lshr { # returns $x >>> $n, where >>> means logical shift right
> > my $x = shift;
> > my $n = shift;
> >
> > $x = ($x >> 1) & 0x7fffffff if ($n--);
> > return($x >> $n);
> > }
>
> Have you tried using
>
> no integer;
>
> in your shift sub?
Yes. You're correct: that works. But, it returns the numeric
representations back to floating point, which tends to slow things down.
Mark
------------------------------
Date: Sat, 17 Nov 2001 20:11:47 GMT
From: "Mark Shelor" <shelorm@earthlink.net>
Subject: Re: Fast Portable Logical Shift Right in Perl ?
Message-Id: <7wzJ7.49351$hZ.4694379@newsread2.prod.itd.earthlink.net>
"David Sletten" <slytobias@home.com> wrote in message
news:3BF5E968.4090207@home.com...
>
> I'm curious what tests you conducted to conclude that Perl performs an
> arithmetic right shift?
Here's a fragment of Perl code running under Cygwin on a Pentium:
use integer;
$a = ~0;
printf("%x\n", $a);
$a = $a >> 1;
printf("%x\n", $a);
The output is:
ffffffff
ffffffff
This only happens when "use integer" is invoked. With "no integer", the
output is
ffffffff
7fffffff
Of course, "use integer" is much faster--especially in crypto
applications--so that's what I'd like to stick with if possible.
Mark
------------------------------
Date: 17 Nov 2001 14:40:58 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Fast Portable Logical Shift Right in Perl ?
Message-Id: <9t6i0q$1js$1@starbuck.cs.utexas.edu>
In article <%mjJ7.47110$hZ.4479174@newsread2.prod.itd.earthlink.net>,
Mark Shelor <shelorm@earthlink.net> wrote:
>Crypto algorithms often require a "logical right shift" operation. Perl's
>right shift operator (>>) appears to be arithmetic:
>
>A function such as
>
>use integer;
>
>sub lshr { # returns $x >>> $n, where >>> means logical shift right
> my $x = shift;
> my $n = shift;
>
> $x = ($x >> 1) & 0x7fffffff if ($n--);
> return($x >> $n);
>}
>
>isn't portable since it assumes a 32-bit integer.
If you're doing cryptographic computations, don't you have to know that
the integers are at least 32 bits anyway? Otherwise, you're going to
have overflow problems. So what does it matter?
>Also, it doesn't seem
>particularly efficient.
It's not that bad. It's not wonderful either. But it's not going to
be exceptionally speedy in Perl anyway.
- 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 15:15:30 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Fast Portable Logical Shift Right in Perl ?
Message-Id: <9t6k1i$2vu$1@starbuck.cs.utexas.edu>
In article <9t6i0q$1js$1@starbuck.cs.utexas.edu>,
Logan Shaw <logan@cs.utexas.edu> wrote:
>In article <%mjJ7.47110$hZ.4479174@newsread2.prod.itd.earthlink.net>,
>Mark Shelor <shelorm@earthlink.net> wrote:
>>Crypto algorithms often require a "logical right shift" operation. Perl's
>>right shift operator (>>) appears to be arithmetic:
>>
>>A function such as
>>
>>use integer;
>>
>>sub lshr { # returns $x >>> $n, where >>> means logical shift right
>> my $x = shift;
>> my $n = shift;
>>
>> $x = ($x >> 1) & 0x7fffffff if ($n--);
>> return($x >> $n);
>>}
>>
>>isn't portable since it assumes a 32-bit integer.
Of course, right after I posted that previous article, I thought of a
way to do it which should work OK:
sub lshr
{
my ($x, $n) = @_;
return ($x >> $n) & ~(0x80000000 >> $n << 1);
}
How does it work?
If the implementation adds leading ones, you start with the most
significant bit turned on and then you shift it by N places, giving a
number whose first N+1 bits are 1. Then you left shift it so that only
the first N bits are 1. Now you have a mask you can use to clear the
unwanted leading 1 bits on $x >> $n.
If the implementation doesn't add leading ones when shifting, you
should still be OK because $x >> $n will have leading zeros, and there
is no possible bitmask you could AND with that will turn those to
ones.
It's not super fast, but at least it avoids having an if statement and
updating a variable. And it does everything in terms of simple integer
arithmetic operations, which would be fast on most machines if this
were a compiled language. (Avoiding branches is good!)
On the other hand, it's very ugly and confusing code...
By the way, your implementation does weird stuff if $n == 0.
Presumably, lshr ($x, 0) should equal $x, but with your implementation,
lshr ($x, 0) equals 0.
- 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: Sat, 17 Nov 2001 22:05:53 GMT
From: "Mark Shelor" <shelorm@earthlink.net>
Subject: Re: Fast Portable Logical Shift Right in Perl ?
Message-Id: <5bBJ7.49959$hZ.4713840@newsread2.prod.itd.earthlink.net>
"Logan Shaw" <logan@cs.utexas.edu> wrote in message
news:9t6k1i$2vu$1@starbuck.cs.utexas.edu...
>
> Of course, right after I posted that previous article, I thought of a
> way to do it which should work OK:
>
> sub lshr
> {
> my ($x, $n) = @_;
>
> return ($x >> $n) & ~(0x80000000 >> $n << 1);
> }
Excellent. That's much better now for all the reasons you cite. However,
it's still not portable to machines that use underlying 64-bit integers.
To handle this, one could compute the value $MASK at script startup:
$MASK = 1;
$MASK = $MASK << 1 while ($MASK > 0);
and then use it in "sub lshr" in place of "0x80000000".
That is, $MASK would end up having the value "0x80000000" for 32-bit
integers, and "0x8000000000000000" for 64-bit integers.
Hmm... is the above code for $MASK portable? I suppose so, as long as the
underlying machine uses a 2's-complement representation for negative
numbers. But, not all machines do that, so it's not entirely portable.
Mark
------------------------------
Date: Sat, 17 Nov 2001 16:30:57 -0600 (CST)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Re: How can I use LWP::Simple inside a safe compartment?
Message-Id: <26901-3BF6E521-41@storefull-244.iap.bryant.webtv.net>
Shouldn't that be
use LWP::Simple;
NOT use LWP::Simple();
--Dennis
------------------------------
Date: 17 Nov 2001 20:23:40 +0100
From: pjacklam@online.no (Peter J. Acklam)
Subject: Re: Median of Three
Message-Id: <k7wpp5fn.fsf@online.no>
"Godzilla!" <godzilla@stomp.stomp.tokyo> wrote:
> Peter J. Acklam wrote:
>
> > Godzilla! wrote:
>
> > > For a number set containing three entries, the median is
> > > always the second number in the set. No math is needed to
> > > find it.
> >
> > No. That is only true if the set is ordered.
>
> Your enrollment in and successful completion of an
> elementary Algebra class could benefit you greatly.
Proving that your statement is wrong is trivial. Here is a set
of three numbers
@a = (5, 3, 7);
Clearly, the second number is not the median.
However, as I said, if you sort the set, the median will always be
the second number.
Peter
--
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
------------------------------
Date: Sat, 17 Nov 2001 11:43:48 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Median of Three
Message-Id: <3BF6BDF4.48FA7CD2@stomp.stomp.tokyo>
Peter J. Acklam wrote:
> Godzilla! wrote:
> > Peter J. Acklam wrote:
> > > Godzilla! wrote:
> > > > For a number set containing three entries, the median is
> > > > always the second number in the set. No math is needed to
> > > > find it.
> > @Array = (3, 5, 7);
> > > No. That is only true if the set is ordered.
> > Your enrollment in and successful completion of an
> > elementary Algebra class could benefit you greatly.
> Proving that your statement is wrong is trivial. Here is a set
> of three numbers
> @a = (5, 3, 7);
> Clearly, the second number is not the median.
> However, as I said, if you sort the set, the median will always be
> the second number.
"For another reader, this word "median" directly indicates
an ordered number set is being used. This is a logical
presumption, a mathematical caveat, amongst those of us
familiar with the language of mathematics, clever or not."
Reading comprehension problem?
So why are you trolling me? Is yours an ego problem?
Godzilla!
------------------------------
Date: Sat, 17 Nov 2001 15:08:16 -0500
From: Andrew Feist <feistatduke@yahoo.com>
Subject: Re: Median of Three
Message-Id: <3BF6C3B0.2FE12D97@yahoo.com>
Sorry if this shows up twice ... been having newsgroup difficulties.
Just to make you feel better, I added some new stuff. (Oh, and not
including cplm either.)
Mark Jason Dominus wrote:
> 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?
>
Well, I think the following works:
if ($x<$y) {
if ($z<$x) {$med=$x} else {$y<$z?$med=$y:$med=$z}
}
else {
if ($z<$y) {$med=$y} else {$x<$z?$med=$x:$med=$z}
}
Of course, it's been a while since I've written any Perl. (Hey, I'm
reading this
on comp.programming.)
Highly non-generalizable, I think. (Perhaps I'm being silly, but the
only really
generalizable algorithm I can think of right now is to sort, then take
the right
element (or average the right elements, as the case may be). I'm not
sure
whether you'd call that "short", even if a sorter is built into Perl.)
Here's my other take on it. Try generalizing the minimum algorithm
given (for arbitrary number of elements) to give you the second-lowest
item. You'd compare the first two, putting lowest in $min and other in
$smin, and then running through the rest of the items. Now generalize
again to find the third-lowest item, etc. This (to me) is more in the
spirit of the quick algorithm given, but would be hell to write.
>
> --
> @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
--
Andrew Feist http://www.math.duke.edu/~andrewf
If I could put time in a bottle, I wouldn't be able to read the
articles.
------------------------------
Date: 17 Nov 2001 14:27:50 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Median of Three
Message-Id: <9t6h86$1dg$1@starbuck.cs.utexas.edu>
In article <3BF6BDF4.48FA7CD2@stomp.stomp.tokyo>,
Godzilla! <godzilla@stomp.stomp.tokyo> wrote:
>Peter J. Acklam wrote:
>> However, as I said, if you sort the set, the median will always be
>> the second number.
>
>
> "For another reader, this word "median" directly indicates
> an ordered number set is being used. This is a logical
> presumption, a mathematical caveat, amongst those of us
> familiar with the language of mathematics, clever or not."
>
>
>Reading comprehension problem?
Yes, apparently you do have a reading comprehension problem, Godzilla.
If you went back to the original article in the thread, you'd
see this code:
if ($x > $y) {
if ($y > $z) {
$min = $z;
} else {
$min = $y;
}
} else {
if ($x > $z) {
$min = $z;
} else {
$min = $x;
}
}
Let's do a little experiment and see how that code looks if you assume
an ordered set (i.e. that $x < $y and $y < $z). It would seem like
exceptionally bad code, since the conditional expression for the first
if statement ("$x > $y") would never be true. Since "$x > $y" would
always be false, we could reduce that if statement to the code in its
else branch:
if ($x > $z) {
$min = $z;
} else {
$min = $x;
}
There, that's much cleaner, isn't it? Well, "$x > $z" is never true
either, so let's eliminate the remaining if statement and take only the
else statement again:
$min = $x;
Wow, now the code is *really* simple.
Does this seem fishy to you yet? If the original poster's set is
ordered, then he posted a 13-line mess of nested "if" statements
instead of just a simple assignment statement.
I have to conclude that any reasonable person reading the original
article in this thread would have to assume that the set for the min
problem is unordered. And since the original poster asked for a
solution to the median problem that was analogous to the one for the
min problem, I think you'd have to conclude that the original poster is
seeking a solution to the problem of finding the median of an unordered
set.
Then there is the fact that the word "median" simply DOES NOT IMPLY
that an ordered set is being used. Suppose, for a second, that a
teacher has a set of scores from an exam and wants to find the median
and the mean of the scores. Does this imply that the scores are
ordered? No. Indeed, ONE way of finding the median is to order them
and then look in the middle, but in fact there are other ways of
finding the median that do not involve ordering the numbers.
If you want to look at it from a mathematical point of view, then one
way to define the median is to use an ordered set. But that's not the
only way to define it. Here's another way:
Given a set S on which a full ordering F is defined,
the median is any member of the set
{ a : cardinality(L) == cardinality(R) }
where
L == { l : memberof((l,a),F) }
and
R == { r : memberof((a,r),F) }
Actually, that only works for sets S with an odd cardinality, but it's
easy enough to come up with another definition that works for sets with
an even cardinality.
The point is that, mathematically, there is no requirement that any
ordered sets are involved. The only requirement is that an ordering is
DEFINED for the set.
HTH. HANW.
- 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: Sat, 17 Nov 2001 20:39:22 GMT
From: "Mark Shelor" <shelorm@earthlink.net>
Subject: Re: Median of Three
Message-Id: <_VzJ7.49487$hZ.4698848@newsread2.prod.itd.earthlink.net>
"Mark Jason Dominus" <mjd@plover.com> wrote in message
news:3bf5e50b.3d71$11@news.op.net...
>
> Suppose you want to find the median of three values instead of the
> minimum. Is there an analogous short version?
Here's some median-of-three code taken from Jon Bentley's implementation of
quicksort in C:
static char *med3(char *a, char *b, char *c, int (*cmp) ())
{ return cmp(a, b) < 0 ?
(cmp(b, c) < 0 ? b : cmp(a, c) < 0 ? c : a)
: (cmp(b, c) > 0 ? b : cmp(a, c) > 0 ? c : a);
}
Of course, this is more analogous to Kernighan and Plaugher's approach to
finding the minimum, but its transliteration to Perl would be quite
straightforward.
Mark
------------------------------
Date: 17 Nov 2001 21:56:40 +0100
From: pjacklam@online.no (Peter J. Acklam)
Subject: Re: Median of Three
Message-Id: <u1vtnmk7.fsf@online.no>
"Godzilla!" <godzilla@stomp.stomp.tokyo> wrote:
> "For another reader, this word "median" directly indicates
> an ordered number set is being used. This is a logical
> presumption, a mathematical caveat, amongst those of us
> familiar with the language of mathematics, clever or not."
>
> Reading comprehension problem?
I fully understand what you write, but it's simply nonsense to say
"this word "median" directly indicates an ordered
number set is being used"
The median of a data set is the value that splits the set in two
in the sense that one half of the values are less than the median
and the other half is larger than the median. The median is a
property of a set and is independent of whether the set is sorted
or not.
> So why are you trolling me?
Correcting people when they're wrong is hardly trolling.
> Is yours an ego problem?
Take a look at yourself. I'm not the one being rude.
Peter
--
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
------------------------------
Date: Sat, 17 Nov 2001 13:02:35 -0800
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Median of Three
Message-Id: <3BF6D06B.8FC4E4EE@stomp.stomp.tokyo>
Logan Shaw wrote:
> Godzilla! wrote:
> >Peter J. Acklam wrote:
(snipped)
> Does this seem fishy to you yet?
Oh yes. It most sointly does.
Smells like troll bait to me.
Godzilla!
------------------------------
Date: Sat, 17 Nov 2001 12:00:56 -0800
From: kkeller@speakeasy.net (Keith Keller)
Subject: Re: Median of Three
Message-Id: <olf6t9.43t.ln@wombat.san-francisco.ca.us>
Hash: SHA1
In article <9t637a$rhc$1@panix2.panix.com>,
jwjr@panix.com (James Wetterau) writes:
> In article <8945t9.vbs.ln@wombat.san-francisco.ca.us>,
> Keith Keller <kkeller@speakeasy.net> wrote:
> ...
>>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;
>
> This code gives $med = 5 for $x = 3, $y = 5, $z = 4, so it's wrong.
Serves me right for trying to write math proofs late at night.
It also fails for x=6,y=5,z=4. The way to fix it is to change
the last line to
else { $med = $j }
I'd implemented it as a Perl sub initially, with return statements,
then decided to make it less Perl-ish. I'll never do it again. :)
- --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
iEYEARECAAYFAjv2wfUACgkQhVcNCxZ5ID8peACfa9X9l3GSdxoWAV6qmQWML56d
ZJMAniUlY5vWx6eteEBJ7R2DebYCx9ch
=CnxC
-----END PGP SIGNATURE-----
------------------------------
Date: Sat, 17 Nov 2001 15:48:12 -0700
From: "Al Dunbar" <Al_Dunbar@HoTMaiL.com>
Subject: Re: Median of Three
Message-Id: <tvdqbukle9q37a@corp.supernews.com>
"Logan Shaw" <logan@cs.utexas.edu> wrote in message
news: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.
<proof snipped>
> I can't imagine that this code is something you'd want to use in a real
> program...
I agree. One problem the logic has is that it applies only to the case of
three numbers. Consider, for example, the sequence 1,2,3,4,90: total=100,
average=20, median=3, closest number in sequence to average=4. Closer to
home, consider 1,2,3,4, where NONE of the members is equal to the median,
which, if memory serves, is the average of the two numbers closest to the
median when dealing with an even number of items.
I'm not exactly sure what the original point of this post was, to calculate
the median of three numbers in the fewest lines, with the smallest number of
operations, or in the quickest time on some particular architecture. Given
the mathematical triviality, I would probably code this in a way that was
direct enough to require no proof, and not worry too much about
optimization.
Conversely, if optimization was going to be an operational advantage, this
would likely be because this code was bottlenecking in some oft-traversed
loop. In that case, I would expect that a higher level of optimization would
result from an understanding of the relationship of each iteration with the
next than from treating the median calculation in isolation.
/Al
------------------------------
Date: Sat, 17 Nov 2001 15:49:23 -0700
From: "Al Dunbar" <Al_Dunbar@HoTMaiL.com>
Subject: Re: Median of Three
Message-Id: <tvdqe5fi4guja6@corp.supernews.com>
"Logan Shaw" <logan@cs.utexas.edu> wrote in message
news: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.
Depends on whether the objective is to determine the /value/ of the median,
or find out which member of the set /is/ the median.
/Al
------------------------------
Date: Sat, 17 Nov 2001 20:27:08 +0100
From: Laocoon <Laocoon@eudoramail.com>
Subject: Re: no more than 128 symbles
Message-Id: <Xns915CD01576437Laocooneudoramailcom@62.153.159.134>
if ( length($string) <= 128 ) { ... }
------------------------------
Date: Sat, 17 Nov 2001 23:37:52 +0300
From: "Godhead" <dolgopa@co.ru>
Subject: Re: no more than 128 symbles
Message-Id: <9t6hmr$85d$1@octopus.co.ru>
Is it OK:
if ($domain =~ /^.{129,}(\.ru)$/){ }
...........................................................................
"Laocoon" <Laocoon@eudoramail.com> ???????/???????? ? ???????? ?????????:
news:Xns915CD01576437Laocooneudoramailcom@62.153.159.134...
> if ( length($string) <= 128 ) { ... }
------------------------------
Date: Sat, 17 Nov 2001 13:29:36 -0800
From: "Steven M." <stevenjm@scatercreek.com>
Subject: Re: no more than 128 symbles
Message-Id: <3BF6D6C0.80301@scatercreek.com>
Laocoon wrote:
> if ( length($string) <= 128 ) { ... }
>
Or if the poster decided to truncate overlength strings...
(tested)
#########
# test string
$string = '1234567890';
# change to 128, or whatever
my $wanted = 8;
if( length $string > $wanted ){
$string = substr( $string, 0, $wanted);
}
# print results and verify as expected
print "$string\n";
##########
Just a thought :-)
s.
------------------------------
Date: Sun, 18 Nov 2001 06:12:09 +0800
From: "ETHOME USER" <toyshop@cm1.ethome.net.tw>
Subject: please help: good OOP guide for perl
Message-Id: <9t6n8j$kkb$1@news.ethome.net.tw>
I enjoy programming with perl because it is so efficent and elegant but i
am so confused with the OOP concept...
could anyone please tell me where to find good resouces on perl OOP
programming ? (network on line free resouces are prefered)
many thanks for your help.
------------------------------
Date: Sat, 17 Nov 2001 11:41:17 -0800
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: Please Kindly Help With DBI
Message-Id: <3BF6BD5D.EA99007A@vpservices.com>
Alex Bright wrote:
>
> prog = the C program executable (i.e. prog.exe)
> employer = the database used with the program
Umm, it's already in a database and what you want to do is use C to dump
it from the database into text and then use Perl DBI to treat the text
as a database? Sounds a bit circular to me. Why can't you just use DBI
to go directly into the database? But if the circular route is really
the way you want to go, read on.
> The C program will create an output file named prog.txt,
> which is formatted as:
> enum elname efname
>
> Each field is separated by space, and each horizontal line is a record
With that format, you can use DBI and SQL to query prog.txt using the
DBD::AnyData module that handles many different kinds of flat files,
like this:
use DBI;
my $dbh = DBI->connect('dbi:AnyData:');
$dbh->func( 'employeeFile','CSV','prog.txt', {
col_names => 'enum,elname,efname',
sep_char => ' ',
},'ad_catalog');
print join ", ", $dbh->selectrow_array(qq~
SELECT elname, efname
FROM employeeFile
WHERE enum = 123
~);
That gets a specific row. To get all rows or all rows in a range, see
the DBI docs on placeholders.
--
Jeff
------------------------------
Date: Sat, 17 Nov 2001 15:58:45 -0600
From: "MK Mazie" <mkmazie>
Subject: reading null terminated string from a file
Message-Id: <tvdnd0lh6oh6a2@corp.supernews.com>
I want to read data from a file. The part I want ends with a null (but it's
not EOF). Is there a better way to do this then: getc, check if its not
null, append the byte to the variable, loop back to the getc. It seems that
there should be a Better Way.
------------------------------
Date: 17 Nov 2001 13:41:54 -0800
From: paulthomson@hotmail.com (paul)
Subject: send HTTP response
Message-Id: <464e478f.0111171341.bfced63@posting.google.com>
Hi,
I have a Perl script which works great. It recieves an http request
from another script and does all its stuff fine. The other script
requires an http 200 response otherwise it resends the data every 15
mins. This is where I get stuck as even though my script seems to work
perfectly the data keeps coming. Does anyone have any pointers or
example http responses?
P.S. I am already using LWP in the script to 'make' an http request. I
am just too dumb right now to get my head round sending a response...
Thanks in advance,
Paul
------------------------------
Date: Sat, 17 Nov 2001 22:19:03 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: send HTTP response
Message-Id: <slrn9vdoin.2su.efflandt@typhoon.xnet.com>
On 17 Nov 2001 13:41:54 -0800, paul <paulthomson@hotmail.com> wrote:
> Hi,
>
> I have a Perl script which works great. It recieves an http request
> from another script and does all its stuff fine. The other script
> requires an http 200 response otherwise it resends the data every 15
> mins. This is where I get stuck as even though my script seems to work
> perfectly the data keeps coming. Does anyone have any pointers or
> example http responses?
>
> P.S. I am already using LWP in the script to 'make' an http request. I
> am just too dumb right now to get my head round sending a response...
Not sure what 200 response indicates, but you apparently need to output
some sort of status header. Some examples:
CGI.pm function method:
print header(-status=>'204 No Content');
Raw:
print "Status: 204 No Content\n\n";
--
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: Sat, 17 Nov 2001 16:19:12 -0600 (CST)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Re: Which ISPs support perl scripts?
Message-Id: <26900-3BF6E260-87@storefull-244.iap.bryant.webtv.net>
Well, some ISP's also offer webhosting; but you don't want that. If they
are also an ISP, their webserver will probably be slow. Good luck at
finding a good CGI webhost. I'm still looking for one that supports
cron, SSH, mod_perl, lets me run scripts under my own UID, has CGI-Wrap
installed, allows chroot or has a sandbox, lets me use GCC compiler,
supports Java applets, WAP, allows me to add any module, lets me run an
ftp server, 300 meg diskspace, unmetered bandwidth, and has quick
24-hour support with a telephone number... for $20/mo. or less. Let me
know if you find one. :)
Aletia.com is the closest that I've found, but I haven't tried it out
yet. Remember most webhosts are like politicians... they promises far
more than they give. IOW, their support stinks, and that is what's most
important. Steer clear of these that I've tried... weinbar.com,
nethollywood.com, internetconnection.com(.net). They all have bad
support and other problems.
hope that helps ya some
Regards,
--Dennis
------------------------------
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 2150
***************************************