[12033] in Perl-Users-Digest
Perl-Users Digest, Issue: 5633 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 11 20:07:16 1999
Date: Tue, 11 May 99 17:00:19 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 11 May 1999 Volume: 8 Number: 5633
Today's topics:
Re: [Fwd: my hands are tied] <gbartels@xli.com>
Re: Anyone willing to spare a shoulder ? <swarren@www.wwwdotorg.org>
Re: File::Find non-functional when passing arguments to (Andrew Allen)
Re: HASH AND ARRAY (Andrew Johnson)
Re: HASH AND ARRAY <juex@my-dejanews.com>
Re: HASH AND ARRAY (Larry Rosler)
Re: HASH AND ARRAY <swarren@www.wwwdotorg.org>
Re: hash and array <tchrist@mox.perl.com>
Help? <rp5280@email.sps.mot.com>
Re: Help? <andrew-johnson@home.com>
Java bindings <mad96@hamp.hampshire.edu>
Re: Java bindings <swarren@www.wwwdotorg.org>
Re: Learning Perl - help, please ! (Bart Lateur)
Re: Need a little looping help todd_b_smith@my-dejanews.com
Re: Need a little looping help todd_b_smith@my-dejanews.com
Re: pattern matching - confused.... (Alastair)
Perl, C financial San Francisco <sunday@ix.netcom.com>
Re: Seizing the Bull by the Horns <tchrist@mox.perl.com>
Re: Seizing the Bull by the Horns (Charles R. Thompson)
Re: Sorting is too slow for finding top N keys... - BEN (Larry Rosler)
sub return undef @array ? <swarren@www.wwwdotorg.org>
Re: sub return undef @array ? <tchrist@mox.perl.com>
Re: sub return undef @array ? <emschwar@rmi.net>
Re: sub return undef @array ? <swarren@www.wwwdotorg.org>
system "cmd" for NT <kd@idpubs.com>
Re: Ten Tips toward *DIVERSITY COMPLIANCE* in Web Desig <tchrist@mox.perl.com>
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 11 May 1999 18:25:08 -0400
From: Greg Bartels <gbartels@xli.com>
Subject: Re: [Fwd: my hands are tied]
Message-Id: <3738AE44.9562EE23@xli.com>
Andrew Allen wrote:
>
> Greg Bartels (gbartels@xli.com) wrote:
> : my $first; tie $first, 'stoplight', \$first, 4;
> : is about as ugly as you can get.
>
> : is there anyway to write a subroutine that I could
> : say something like:
> : my $first = magic_routine(4);
>
> Yes. Just write it:
>
> sub magic_routine
> {
> my $tievar;
> tie $tievar,'stoplight',\$tievar,@_;
> return $tievar;
> }
>
> Andrew
I've already tried a version of your magic_routine, the problem
is $tie_var is tied, but $first is not.
something gets lost when you return $tievar.
my $first = stoplight::magic_routine(7);
# put magic_routine in stoplight module.
I dont know what happens, but $first isn't tied to anything.
Greg
------------------------------
Date: Tue, 11 May 1999 22:48:44 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: Anyone willing to spare a shoulder ?
Message-Id: <gt2_2.256$6x6.196@news.rdc1.sfba.home.com>
Carol Herbert <carol@herb4.freeserve.co.uk> wrote in message
news:7ha2i7$6d$1@news4.svr.pol.co.uk...
> I have tried 'z' and 'd'.
>
> I have a feeling that Active Perl may not be working properly.
>
> I have just tried a combination of ctrl, z and return - and the script
> worked.
> However, I have just spent 10 minutes trying to repeat the action - to no
> avail.
>
> Stephen Warren wrote in message ...
> >Carol Herbert <carol@herb4.freeserve.co.uk> wrote in message
> >news:7ha1dc$v57$1@news4.svr.pol.co.uk...
> >> Using Dos to check perl programmes : when entering a list of strings,
the
> >> command to finish on UNIX is CTRL+D. Other systems states CTRL+Z - but
> >> neither of these work. Is there a different command for DOS ?
> >
> >Did you try typing CTRL-D, then return? Assuming that you mean entering a
> >list of strings by typing...
I've just tried the following, using ActiveState 515 (Win98):
--- cut
#!/usr/local/bin/perl -w
use strict ;
while (<>)
{
print "You typed: $_" ;
}
--- cut
And simply typing CTRL-Z (no return) appears to exit the program... How are
you reading your input?
This works 100% under a standard command.com and nearly OK under Cygwin32
B20.1 bash (it sees an extra blank line when it first runs... anyone know
why?)
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: 11 May 1999 22:25:20 GMT
From: ada@fc.hp.com (Andrew Allen)
Subject: Re: File::Find non-functional when passing arguments to &wanted
Message-Id: <7haaog$gcn$1@fcnews.fc.hp.com>
RayG (rgoldber@eb.com) wrote:
: > : Andrew Allen wrote:
er.... below is what Bart Lateur wrote. Careful with that quoting.
: >
: > : sub foo { shift };
: > : print \foo; # undef
<snip> rest
: you could also use *foo{CODE}
excellent point, but rather verbose for \&foo. And if we had
lexically-scoped subs (which we don't) it wouldn't work for those.
Andrew
------------------------------
Date: Tue, 11 May 1999 22:24:49 GMT
From: andrew-johnson@home.com (Andrew Johnson)
Subject: Re: HASH AND ARRAY
Message-Id: <R62_2.3072$LL3.76214@news2.rdc1.on.home.com>
In article <7ha6cf$l9s$1@nnrp1.deja.com>,
bababozorg@aol.com <bababozorg@aol.com> wrote:
! hi
! can any one please tell me how to do this? :
! i have a array like this:
! @array = ("aaa","bbb","ccc");
! i would like to add each element of this array az a key into a hash.
! like this:
! %hash = ("aaa","",
! "bbb","",
! "ccc","");
my @array = qw/aaa bbb ccc/;
my %hash;
# do it in a loop:
foreach my $key (@array) {
$hash{$key} = '';
}
# or, do it with a map{}:
%hash = map{$_ => ''} @array;
# or, do it with a hash slice:
@hash{@array} = ('') x @array;
regards
andrew
------------------------------
Date: Tue, 11 May 1999 15:41:56 -0700
From: "J|rgen Exner" <juex@my-dejanews.com>
Subject: Re: HASH AND ARRAY
Message-Id: <7habjt$f5c@news.dns.microsoft.com>
<bababozorg@aol.com> wrote in message news:7ha6cf$l9s$1@nnrp1.deja.com...
[...]
> AND ALSO DOES ANY ONE KNOWS HOW TO SORT a HASH?
Please define, what in your opinion is the first and the next element in a
hash, i.e. what kind of sequence relation do you intent to use?
Or in other words: Your question is meaningless, because by definition a
hash does not have an order.
jue
--
J|rgen Exner
------------------------------
Date: Tue, 11 May 1999 16:24:25 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: HASH AND ARRAY
Message-Id: <MPG.11a25c0450635b6a989a30@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <7ha6cf$l9s$1@nnrp1.deja.com> on Tue, 11 May 1999 21:10:44
GMT, bababozorg@aol.com <bababozorg@aol.com> says...
...
> AND ALSO DOES ANY ONE KNOWS HOW TO SORT a HASH?
Why are you shouting at us?
Not so surprisingly, this is a question that gets asked frequently! So
you might find its answer among the answers to the Frequently Asked
Questions.
perlfaq4: "How do I sort a hash (optionally by value instead of key)?"
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 11 May 1999 23:24:41 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: HASH AND ARRAY
Message-Id: <Z_2_2.262$6x6.199@news.rdc1.sfba.home.com>
J|rgen Exner <juex@my-dejanews.com> wrote in message
news:7habjt$f5c@news.dns.microsoft.com...
>
> <bababozorg@aol.com> wrote in message news:7ha6cf$l9s$1@nnrp1.deja.com...
> [...]
> > AND ALSO DOES ANY ONE KNOWS HOW TO SORT a HASH?
>
> Please define, what in your opinion is the first and the next element in a
> hash, i.e. what kind of sequence relation do you intent to use?
>
> Or in other words: Your question is meaningless, because by definition a
> hash does not have an order.
You can access the elements of a hash in a sorted manner, though:
foreach $key ( sort keys %someHash )
{
process $someHash{ $key } ;
}
and all the various varieties around that...
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: 11 May 1999 17:30:08 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: hash and array
Message-Id: <3738bd80@cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, bababozorg@aol.com writes:
:AND ALSO DOES ANY ONE KNOWS HOW TO SORT a HASH?
IT IS IN THE PERLFAQ4 MANPAGE AND THE PERLFUNC MANPAGE WHICH ARE BOTH
ON YOUR SYSTEM AND WHICH YOU FORGOT to READ.
--tom
--
"Everything you said about Plan 9 is wrong"
-- Rob Pike, letting a speaker have it
------------------------------
Date: Tue, 11 May 1999 15:32:02 -0700
From: Mike Garcia <rp5280@email.sps.mot.com>
Subject: Help?
Message-Id: <3738AFD4.A414184@email.sps.mot.com>
This is a multi-part message in MIME format.
--------------3ABADAA01A4596D79E71CCEF
Content-Type: text/plain; charset=us-ascii; x-mac-type="54455854"; x-mac-creator="4D4F5353"
Content-Transfer-Encoding: 7bit
Hello,
I'm kind of new to Perl and I have a big project to create eleven Perl
programs to translate data files so a database can load them.
I have one example that works, but I think it has some errors that I
found when using the -w flag.
Also I'd like to clean up the code as much as possible and make it more
professional.
Is there anyone that is good with Perl that has some time to look at
this 9 page program and give a little advice?
Thanks for taking some time to help a newbie.
Mike in AZ
--------------3ABADAA01A4596D79E71CCEF
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Mike Garcia
Content-Disposition: attachment; filename="vcard.vcf"
begin: vcard
fn: Mike Garcia
n: Garcia;Mike
org: Motorola BMC CIM
adr;dom: ;;2200 W. Broadway Rd. ;AZ09 Maildrop M150;Mesa, Arizona 85202;Pager 888 464-6689;
email;internet: rp5280@email.sps.mot.com
title: Synergy Administrator
tel;work: (602) 655-2871
tel;fax: (602) 855-5135
x-mozilla-cpt: ;1
x-mozilla-html: FALSE
version: 2.1
end: vcard
--------------3ABADAA01A4596D79E71CCEF--
------------------------------
Date: Tue, 11 May 1999 23:23:18 GMT
From: Andrew Johnson <andrew-johnson@home.com>
Subject: Re: Help?
Message-Id: <3738BBC2.16196353@home.com>
In comp.lang.perl.misc, you wrote:
! This is a multi-part message in MIME format.
! --------------3ABADAA01A4596D79E71CCEF
! Content-Type: text/plain; charset=us-ascii; x-mac-type="54455854";x-mac-creator="4D4F5353"
! Content-Transfer-Encoding: 7bit
First things first: don't post MIME messages ... usenet is
a plain text medium ... multipart messages and vcards only
make a mess of things (your newsreader might not show the mess,
but others do).
! Hello,
!
! I'm kind of new to Perl and I have a big project to create eleven Perl
! programs to translate data files so a database can load them.
!
! I have one example that works, but I think it has some errors that I
! found when using the -w flag.
!
! Also I'd like to clean up the code as much as possible and make it more
! professional.
!
! Is there anyone that is good with Perl that has some time to look at
! this 9 page program and give a little advice?
!
! Thanks for taking some time to help a newbie.
well, I may not have to go through all 9 pages with a fine-tooth
comb --- but since you ask nicely, and are trying to use -w, and more
importantly, seem honestly interested in improving (rather than asking
for quick fixes), I'll volunteer to take a peek and see if I have
any basic pointers for you (either send me a URL where I can grab
a copy, or email me [in plain text] a copy).
regards
andrew
--
Andrew L. Johnson http://members.home.com/andrew-johnson/
Some people, when confronted with a problem, think 'I know,
I'll use regular expressions.' Now they have two problems.
-- Jamie Zawinski, on comp.lang.emacs
------------------------------
Date: Tue, 11 May 1999 18:59:36 -0400
From: Michael Dartt <mad96@hamp.hampshire.edu>
Subject: Java bindings
Message-Id: <3738B658.C910853C@hamp.hampshire.edu>
[I tried posting another message about this, but my newsreader says it
didn't make it. Apologies if you've seen this already.]
Are there any Perl bindings for Java or vice versa?
Thanks. :-)
--Mike
------------------------------
Date: Tue, 11 May 1999 23:18:51 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: Java bindings
Message-Id: <vV2_2.261$6x6.180@news.rdc1.sfba.home.com>
Michael Dartt <mad96@hamp.hampshire.edu> wrote in message
news:3738B658.C910853C@hamp.hampshire.edu...
> [I tried posting another message about this, but my newsreader says it
> didn't make it. Apologies if you've seen this already.]
>
> Are there any Perl bindings for Java or vice versa?
There is a system called JPL (Java Perl Lingo) that integrates Java and Perl
in various ways. I'm not up on the latest status, but check out
http://oreilly.com/oreilly/press/jpl_oss.html.
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: Tue, 11 May 1999 21:58:11 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: Learning Perl - help, please !
Message-Id: <3738a4fe.5871866@news.skynet.be>
Carol Herbert wrote:
>I have done the exercises at the end of chapter 3 - the one involving
>inputting strings and getting them to print in reverse order and a random
>selected word, put they do not work when tried through MSDos.
>I am ending the input of the string with ctrl+z (as recommended) - should I
>be ending with something else ?
>
>I am running Active Perl on an IBM pc running Windows 95. I know the Perl
>is working as other scripts work ok.
Well, huh... I run GNU Perl on DOS. Activestate Perl is on my other PC
(switched off and disconnected right now). I might check that out
tomorrow...
But on DOS, ctrl-D is simply included in the input.
ctrl-Z in keyboard input is recognized as the "file end", but ONLY if
it's the first thing (or the only thing) on the line! Anything behind it
is ignored. Plus, as others have said: the darn thing (shell?) waits
until you press <enter> before returning to Perl to process the line.
It's likely that Activestate Perl behaves the same way. And I don't
think reinstalling it (the "magic incantation" in the Windows world ;-)
would make any difference. If necessary, I'll check it out tomorrow.
Bart.
------------------------------
Date: Tue, 11 May 1999 22:06:46 GMT
From: todd_b_smith@my-dejanews.com
Subject: Re: Need a little looping help
Message-Id: <7ha9lk$nnn$1@nnrp1.deja.com>
In article <m3pv474vhx.fsf@joshua.panix.com>,
Jonathan Feinberg <jdf@pobox.com> wrote:
> todd_b_smith@my-dejanews.com writes:
>
> > no, I basically said, "I'm having to copy this code directly from my
> > program but from sight (I would copy and paste if I could), so
please
> > forgive obvious errors." I didn't think it was difficult to
understand.
>
> How on earth do we know what is an "obvious" error to you?
>
misspelled words? I don't know. easy mistakes. Let's end this part of
the conversation.
> At any rate, we're all still waiting to see a description of the
> *problem* you're trying to solve, so we can suggest a WTDI or two. It
> may in fact be that constructing and eval()ing code is the best way to
> go.
but the thing is, I don't care about the program or the best way to do
it, i'm just wondering how to get this particular code segment working.
If you'd look at the code, you'd see that i'm trying to return these
lines (10 sets of coords for moves)- (it's for a game my script is
playing):
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,1)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,2)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,3)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,4)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,5)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(1,0)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(1,1)
...
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(5,5)
(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,1),(0,0)
...
(5,5),(5,5),(5,5),(5,5),(5,5),(5,5),(5,5),(5,5),(5,5),(5,5)
I'm SURE there's a better way, I just wanted to get those eval's
working.
>
> --
> Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
> http://pobox.com/~jdf
>
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
------------------------------
Date: Tue, 11 May 1999 22:16:22 GMT
From: todd_b_smith@my-dejanews.com
Subject: Re: Need a little looping help
Message-Id: <7haa7j$o60$1@nnrp1.deja.com>
In article <m1btfrofe7.fsf@halfdome.holdit.com>,
merlyn@stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "todd" == todd b smith <todd_b_smith@my-dejanews.com> writes:
>
> todd> I don't want to write 20 nested for's.
>
> "20 nested for's" almost certainly means you're trying to use
> a screwdriver as a hammer. Please (as other people already asked)
> describe your PROBLEM so we can help you CODE it properly.
>
> I can bet the rent money that there won't be a single expert here that
> will agree "20 nested fors... yup, that's the only way to do this
> one".
There's never an 'only way' in Perl. I just want to get this code block
working. I don't care about better solutions, because my script is
insignificant; I'm just trying to learn new things, and finding a better
way means I'll never learn what was wrong with all these eval's.
This thing is definitely not coded properly, but my goal here is to find
problems in the current code rather than making new code.
So if anyone wants to help, I'm asking "What's wrong with this?", not
"How should I be writing this?"
Thanks,
Todd Smith
Perl Programmer
ITC^DeltaCom
>
> print "Just another Perl hacker,"
>
> --
> Name: Randal L. Schwartz / Stonehenge Consulting Services
(503)777-0095
> Keywords: Perl training, UNIX[tm] consulting, video production,
skiing, flying
> Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger
merlyn@teleport.com)
> Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
> Quote: "I'm telling you, if I could have five lines in my .sig, I
would!" -- me
>
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
------------------------------
Date: Tue, 11 May 1999 23:57:15 GMT
From: alastair@calliope.demon.co.uk (Alastair)
Subject: Re: pattern matching - confused....
Message-Id: <slrn7jhkhd.5f.alastair@calliope.demon.co.uk>
Arran Price <arranp@datamail.co.nz> wrote:
>
>#uncomment these 2 lines to see when I hardcode the chars
>#$CHAR_FROM="|";
>#$CHAR_TO="!";
Try using single quotes, not double. I think this is something to do with the
fact that double quotes interpolate, while single quotes do not.
$CHAR_FROM='\|';
$CHAR_TO='!';
HTH.
--
Alastair
work : alastair@psoft.co.uk
home : alastair@calliope.demon.co.uk
------------------------------
Date: Tue, 11 May 1999 16:42:40 -0700
From: Michael Sunday <sunday@ix.netcom.com>
Subject: Perl, C financial San Francisco
Message-Id: <3738C070.75EF@ix.netcom.com>
Very attractive/prestigious financial services
company, downtown SF seeks solid quality Perl/C
developer. Sal. to $90's. And if there is better
place to post this please let me know (be nice).
--Michael Sunday 800 456-1344 x 15
sunday@sundayusa.com
------------------------------
Date: 11 May 1999 16:28:12 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Seizing the Bull by the Horns
Message-Id: <3738aefc@cs.colorado.edu>
You know, it's very disappointing to see so many followups
to the prose, but nary a single one to the code. :-(
--tom
--
With a PC, I always felt limited by the software available. On Unix, I am
limited by my knowledge. --Peter J. Schoenster <pschon@baste.magibox.net>
------------------------------
Date: Tue, 11 May 1999 22:34:39 GMT
From: design@raincloud-studios.com (Charles R. Thompson)
Subject: Re: Seizing the Bull by the Horns
Message-Id: <MPG.11a27bd3b761e06b98969a@news>
[This followup was posted to comp.lang.perl.misc and a copy was sent to
the cited author.]
In article <3738aefc@cs.colorado.edu>, Tom Christiansen says...
> You know, it's very disappointing to see so many followups
> to the prose, but nary a single one to the code. :-(
It's rather mind boggling at this point for me as a beginner. It has been
saved to my hard drive in the 'projects' folder until I can fiddle with
it on the Linux Server.
Wasn't in vain. People are out getting their BP checked (no s/ai/ei/ pun
intended) after the initial offshoot. :)
--
CT
------------------------------
Date: Tue, 11 May 1999 15:29:28 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Sorting is too slow for finding top N keys... - BENCH
Message-Id: <MPG.11a24f24f9d6cf34989a2f@nntp.hpl.hp.com>
In article <wkr9ony9jr.fsf@turangalila.harmonixmusic.com> on 11 May 1999
10:18:16 -0400, Dan Schmidt <dfan@harmonixmusic.com> says...
> lr@hpl.hp.com (Larry Rosler) writes:
>
> | I haven't found a sort-extremes module under CPAN Sort::..., and
> | neither Knuth Vol. 3 nor Sedgewick seems to have an algorithm. I
> | will soon submit this to the Perl Function Repository, and perhaps
> | make it into a module as a training exercise.
>
> The problem is discussed in Knuth, though he doesn't present an
> algorithm that you can cut and paste (not that a MIX implementation
> would do us much good anyway).
>
> 5.2.2 Exercise 31 can be extended to find the top n elements of an
> array (you do a quicksort but ignore everything on the side you're not
> interested in; this sounds like Marko's quickselect).
>
> 5.3.3 discusses selection in general, and exercise 17 is a
> generalization of the problem you describe (selecting both the k
> largest and the l smallest elements).
Thanks for the pointers. I didn't actually look at every one of the
exercises, just the body text and a skim which missed these. They are
each rated 20, which is 'an average problem' (and the first one is
marked 'recommended'). Still, problems are not solutions.
Here is the fourth (and, I hope, final) version of the extremes()
function. The descriptive text contains my current understanding of the
performance issues. Any observations would be welcome.
sub extremes { # Return N extreme-valued elements of array or hash.
my ($ref, $n, $num, $max) = @_;
my $ref_type = $ref && ref $ref || "";
my $hash = $ref_type eq 'HASH' ? 1 : $ref_type eq 'ARRAY' ? 0 :
die "First argument '$ref' must be a reference to array or hash.\n";
$n && $n > 0 or return ();
$max = $num && ref $num eq 'CODE' ? 0 : $max ? -1 : +1;
my @out;
for (my $j = 0;
defined(my $key = $hash ? each %$ref : $ref->[$j++]); ) {
next if @out == $n &&
($max ? $max == ($hash ? $num ? $ref->{$key} <=> $ref->{$out[-1]}
: $ref->{$key} cmp $ref->{$out[-1]}
: $num ? $key <=> $out[-1]
: $key cmp $out[-1])
: do { ($a, $b) = ($key, $out[-1]); &$num > 0 });
my ($lo, $hi, $i) = (0, scalar @out);
for ($i = int($hi/2); $lo < $hi; $i = $lo + int(($hi - $lo)/2)) {
my $comp =
$max ? $max * ($hash ? $num ? $ref->{$key} <=> $ref->{$out[$i]}
: $ref->{$key} cmp $ref->{$out[$i]}
: $num ? $key <=> $out[$i]
: $key cmp $out[$i])
: do { ($a, $b) = ($key, $out[$i]); &$num };
if ($comp > 0) { $lo = $i + 1 }
elsif ($comp < 0) { $hi = $i }
else { last };
}
splice @out, $i, 0, $key;
--$#out if @out > $n;
}
@out
}
# extremes() returns the extreme N elements of a list or keys of a hash,
# sorted according to a specified criterion.
#
# The conventional way to do this is to do a complete sort, then slice
# on the list output: (sort ... LIST)[0 .. N - 1]
#
# When the size M of the aggregate is very large, pathological data sets
# may produce very long sort times, O(M**2), instead of the average,
# O(M log M). In such cases, if N << M, this function (which repeatedly
# sorts a slice of size N + 1) will be much more predictable and may be
# more efficient, O(M * N * log N).
#
# extremes(ref, n, num, max)
# ref = reference to array or hash
# n = maximum number of elements to return
# num = sort numeric if TRUE, sort lexicographic if FALSE
# max = maxima descending if TRUE, minima ascending if FALSE
#
# Trailing FALSE arguments may be omitted. More complex sorting
# comparisons may be done using a reference to a usual sorting
# subroutine as the third argument.
#
# The algorithm for this function, and an initial implementation, were
# presented originally by Michel Dalle, 8 May 1999.
#
# Larry Rosler, 11 May 1999
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 11 May 1999 23:14:54 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: sub return undef @array ?
Message-Id: <OR2_2.259$6x6.141@news.rdc1.sfba.home.com>
I've trying to write a function that takes a scalar, does some processing to
split it up and returns the various components in an array. This bit works
fine.
However, if the original scalar is of a certain pattern, I want to return
undef. When I do something like the code below, it doesn't work as expected.
Any idea why?
The following prints "defined\n" instead of "not defined\n", which is what
I'd expect... ;
---- cut
#!/usr/local/bin/perl -w
use strict ;
sub retundef
{
return undef ;
}
my @x = retundef() ;
my $text = ( defined @x ) ? "defined\n" : "not defined\n" ;
print $text ;
---- cut
Perhaps I should just return a ref to the array, or a zero-length array in
the 'null' case...
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: 11 May 1999 17:33:18 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: sub return undef @array ?
Message-Id: <3738be3e@cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
Cursed by Microsoft Outlook Express 5.00.2014.211, "Stephen Warren"
<swarren@www.wwwdotorg.org> writes in comp.lang.perl.misc:
:I've trying to write a function that takes a scalar, does some processing to
:split it up and returns the various components in an array. This bit works
:fine.
:
:However, if the original scalar is of a certain pattern, I want to return
:undef. When I do something like the code below, it doesn't work as expected.
:Any idea why?
Because you neglected to read what the perlfunc manpage on your system
has to say about the defined operator. Shall I spam it at you?
Because you neglected to read what the perlfunc manpage on your system
has to say about the return operator. Shall I spam it at you?
And because (undef) is a one-element list, whereas () is a zero-element
list.
--tom
--
I'm not writing any more tapes, ever. --Andrew Hume
------------------------------
Date: 11 May 1999 17:39:14 -0600
From: Eric The Read <emschwar@rmi.net>
Subject: Re: sub return undef @array ?
Message-Id: <xkfso93kwgt.fsf@valdemar.col.hp.com>
"Stephen Warren" <swarren@www.wwwdotorg.org> writes:
> The following prints "defined\n" instead of "not defined\n", which is what
> I'd expect... ;
This is a bit sneaky. Although TFM states: "...an array you set to the
empty list appears undefined initially, and one that once was full and
that you then set to the empty list still appears defined," you're not
actually setting @x to the empty list.
> sub retundef
> {
> return undef ;
> }
This returns an array 1 item long, whose first item is undef.
> my @x = retundef() ;
This assigns @x an array that looks like this: [ undef ]. You probably
wanted to assign 'undef' instead. perdoc perlsub to understand why you
can't (at least, I don't see any way you can).
> Perhaps I should just return a ref to the array, or a zero-length array in
> the 'null' case...
The latter is what the documentation recommends. Unless you have a good
reason not to, that's what you ought to use.
-=Eric
------------------------------
Date: Tue, 11 May 1999 23:56:25 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: sub return undef @array ?
Message-Id: <Js3_2.266$6x6.79@news.rdc1.sfba.home.com>
Tom Christiansen <tchrist@mox.perl.com> wrote in message
news:3738be3e@cs.colorado.edu...
> [courtesy cc of this posting sent to cited author via email]
>
> Cursed by Microsoft Outlook Express 5.00.2014.211, "Stephen Warren"
> <swarren@www.wwwdotorg.org> writes in comp.lang.perl.misc:
Arguably, but I could start posting in HTML with .zip'd MIME attachments...
:-)
> :I've trying to write a function that takes a scalar, does some processing
to
> :split it up and returns the various components in an array. This bit
works
> :fine.
> :
> :However, if the original scalar is of a certain pattern, I want to return
> :undef. When I do something like the code below, it doesn't work as
expected.
> :Any idea why?
>
> Because you neglected to read what the perlfunc manpage on your system
> has to say about the defined operator. Shall I spam it at you?
<cringe> Guess I asked for it. Ok, so the answer's there...
> Because you neglected to read what the perlfunc manpage on your system
> has to say about the return operator. Shall I spam it at you?
So, since I returned a scalar, but @wantarray, it automatically wrapped it
into a 1-element list...
Do you think many programs (intentionally) depend on this? It'd be kind-of
nice, IMO, to be able to use Perl like I expected I could... i.e. returning
undef would actually undefine the array...
Of course, it would be a performance hit if the array were to be redefined,
since the symbol table entry's typeglob would have to have "@" entry
re-allocated as noted in the docs...
Perhaps for aggregates, there could be an extra flag in the data stored in
the typeglob's data that stated defined/undefined, so the pointer to the
allocated data could still exist even in an undef state.
This would of course require co-operation from defined(), so there would be
a few changes. The bit would have to be set on each asignment too, I guess.
Overhead, but how much?
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: Tue, 11 May 1999 23:24:10 GMT
From: "K.D. Donley" <kd@idpubs.com>
Subject: system "cmd" for NT
Message-Id: <u_2_2.1412$Yy4.39150@dfiatx1-snr1.gtei.net>
System "cmd"; fails on NT. acts as if command not really there. fails to
execute. suggestions?
------------------------------
Date: 11 May 1999 16:09:30 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Ten Tips toward *DIVERSITY COMPLIANCE* in Web Design
Message-Id: <3738aa9a@cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, alastair@calliope.demon.co.uk writes:
:I agree with the sentiment wholeheartedly. However, considering the grief people
:get around here for posting off-topic messages. I see precious little Perl
:content in there.
It was a set-up. Look to the followup -- the one with the perlian
bullcrap descrewing proxy. It's super cool.
--tom
--
What about WRITING it first and rationalizing it afterwords? :-)
--Larry Wall in <8162@jpl-devvax.JPL.NASA.GOV>
------------------------------
Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>
Administrivia:
Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing.
]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body. Majordomo will then send you instructions on how to confirm your
]subscription. This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 5633
**************************************