[18178] in Perl-Users-Digest
Perl-Users Digest, Issue: 346 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 23 21:05:47 2001
Date: Fri, 23 Feb 2001 18:05: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: <982980312-v10-i346@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 23 Feb 2001 Volume: 10 Number: 346
Today's topics:
Re: (OFF TOPIC) Re: This is driving me nuts and I need <straith@modusvarious.org>
Re: Basic: Arrays, shift, and the mystery element <ren@tivoli.com>
Re: Can't use 'pop' as hash key ()
Cross Reference Listing of Variables <Bret.Tucker@home.com>
Re: Difficult Split Question <bertilow@chello.se>
Re: Difficult Split Question <bertilow@chello.se>
Re: flushing stdout <gtoomey@usa.net>
Re: HTML in a Perl Script <tony_curtis32@yahoo.com>
HTTP connection <maheshasolkar@yahoo.com>
Re: HTTP connection <gtoomey@usa.net>
Re: Is there a perl history equivalent? <crowj@aol.com>
Re: max value in list? <tony_curtis32@yahoo.com>
Re: max value in list? (Damian Conway)
Re: max value in list? (Damian Conway)
Re: max value in list? <mischief@velma.motion.net>
Re: question about arrays <ren@tivoli.com>
Re: question about arrays <ren@tivoli.com>
Re: question about arrays <ren@tivoli.com>
Re: RFD - comp.lang.perl.db, comp.lang.perl.network, co <Jonathan.L.Ericson@jpl.nasa.gov>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 24 Feb 2001 00:56:31 GMT
From: SJ Straith<straith@modusvarious.org>
Subject: Re: (OFF TOPIC) Re: This is driving me nuts and I need a guru
Message-Id: <9770rv$4ni$1@samba.rahul.net>
Chris Stith (reaching for the coffee) wrote:
: SJ Straith <straith@modusvarious.org> wrote:
:> Chris Stith (reaching for the coffee) wrote:
:> : SJ Straith <straith@modusvarious.org> wrote:
:> soc. ne social
:> soc. eq sociology
: My mistake. Sociology does suffer if someone is using poor
: communication and reference skills, too. Perhaps moreso, since
: the references can't define things as accurately in one of the
: few softer sciences that is actually softer than computer science.
Especially in the soc.history grouping. History has enough trouble
with incomplete data without people deliberately giving inaccurate
data.
:> : It's about computers, computer languages, perl, miscellaneous
:> : topics about Perl. That what comp.lang.perl.misc means. We're
:> : here to discuss, not to be forgiving of those who make a mess
:> : of the newsgroup.
:> If I respond to this paragraph as I feel it deserves, this will
:> turn into a flame, which I at least did not intend.
: Thank you for being polite. I did not mean to offend, or to imply
: ignorance. I only meant to reinforce. I'm sure the name is clear
: to you, much moreso than the soc. heirarchy is to me, since I do
: not visit any of those groups.
I thought that might be the case, which is why I didn't respond
as I would normally have.
: [snip]
:> :> : - Type perldoc and read the usage message. Pointer to the FAQ.
:> :> The way the usage message reads, it appears as though one would need
:> :> to know exactly what to ask for there. Some of the RTFF responses
:> :> indicate that the question is a variation on a question in the FAQ.
:> : So look through the perlfaq manpage.
:> Sounds like a good idea. One problem. When I type "man perlfaq" I get
:> "No manual entry for perlfaq."
: This is another case of your specific installation being non-standard.
: As you mention, this may be the fault of your ISP or web hosting
: service, but we did not choose your hosting service for you.
Agreed. I and my roomies did, in part because the owner of the company
has said that he will not let the Verioborg get their little claws into
it. (The other reason is that we can have shell access.)
:> :> : - The main perl manpage points to the faq on the first page.
:> :> Oh, really?
:> : On my versions it does. 5.005_03 and 5.6.0
:> :> I just took a look at the main perl manpage accessable from
:> :> the shell I'm in and didn't see it in any part of the manpage.
:> :> Now this COULD be because the manpage is old, as this cut from
:> :> it seems to indicate,
:> :> 24/Mar/96 Last change: perl 5.003 with
: [snip]
:> : Don't blame others for your outdated documentation. It's free,
: [snip]
:> Did I say I was blaming anyone? No. I was simply showing that
:> sometimes things aren't as you expect them to be.
: Which is not the fault of someone expecting to find a standard
: thing in a standard place. It is the fault of the one who (re)moved it.
Or who doesn't have the time to add it.
:> As for whether the perl was installed correctly or not, take that
:> up with my ISP. I don't, at the moment, have my linux boxen up
:> with the complete install of the most recent version of perl. I
:> am using my shell account with my ISP and their perl installation.
:> I use what's available.
: I will not take it up with your ISP. I didn't choose your ISP, I don't
: pay your ISP, and I don't have a complaint against the usability of
: your ISP. It would seem that you do have a valid complaint against them.
Not really. I can do a hell of a lot within this shell account.=,
and I am sure that when things pick up here in the Valley he will
probably be adding another techie to see to such things.
:> :> : - www.perl.com has "FAQ" on their main navigation.
:> :> A lot of people don't think to STFW. I've had to be reminded of
:> :> this occasionally
:> : A lot of people don't think in general.
:> I have to agree. That's why "common sense" isn't.
:> : Sometimes, people just don't think about who's going to be the
:> : designated driver. Does that mean it's okay to drive drunk?
:> Now THAT has to be one of the most ridculous comparisons I have
:> even seen made in an arguement.
: Is it? The drunk driving example and failing to read documentation
: before posting both boil down to leaving out one detail crucial to
: the days activities which cause you to be in an undesirable state
: among a group of your fellow humans. One has much greater potential
: cost, but the nature of inconsideration of others is the same no
: matter how it manifests itself.
Put that way, I can see where you're coming from. I still think
it's a poor analogy, but I can understand the point you are making.
:> :> : - It's mentioned on this ng several times a day.
:> :> That one I won't argue. In tin, which I commonly use for
:> :> reading ngs, all I had to do was type FAQ in the search
:> :> and it took me to the first of the FAQ segments.
:> : There's a newsgroup FAQ, too, which mentions the Perl FAQ. Try
:> : searching for that and reading it, as it is common netiquette to
:> : do before posting, if you have any questions about the newsgroup
:> : policy or need to find resources about Perl to search (like
:> : perl.com if you should happen to actually dare to STFW).
Checked for the group FAQ. For some strange reason, I couldn't find it.
Maybe it's just the wrong time in the refresh cycle. I DO, however,
like the e-mailed mini-FAQ. It gave me some info I had been looking
for in a cleaner fashion than I had found elsewhere.
And I say again, <G>.
:> :> : - a search for "perl faq" on google yields the thing as the first
:> :> : listing.
:> :> See above.
:> : See the discussion of driving drunk above.
:> <G> See response to the discussion of driving drunk above.
: :-/ Again, I admit it's hyperbole, but it is intended as such. Perhaps
: my explanation of why I feel it's an appropriate comparison will at least
: make it read as a failed analogy instead of an absurd one.
Your explanation did just that.
:> :> : It's hard work to avoid konwing about the FAQ!
:> :> Not quite as hard as you think. I will say, that while I read
:> :> this group I have only asked for help once, maybe twice. I
:> :> won't do THAT again. Only my perl programmer roomie is allowed
:> :> to heap that kind of abuse on me. <G>
:> : Know the software you are going to use. Read the Free Manual.
:> : Read the Fine Manual. Read the F******* Manual. Then, Read the
:> : Full Manual when you have time.
:> You DO realize that reading Manualese is a talent, knack, ability
:> (pick one) that not everybody has, don't you?
: I realize that reading a manual and understanding exactly what it
: means outside the context of the tools is very difficult. However,
: you should have Perl readily available to you when you are reading
: the online documentation. Open three or four windows if you must,
: one beside another. One for docs, one for an editor, one for STDOUT
: of the program, and maybe one other for another doc source or for
: tailing a log file if your program makes one.
Even within the context of the tools it can be difficult.
: Most abilities, regardless of talent, can be learned to some extent
: with education, practice, and determination. Even sports and music
: can be taught and learned to a great deal, although those with
: natural talents excel easier and farther than the rest. Reading for
: comprehension and comparing results of tests to documentation are
: no more difficult than rehearsing a musical piece and changing
: what you do based on how you sound. In fact, being a strong amateur
: musician who sings and plays many instruments, I'd say reading
: documentation for Perl is much easier.
<G>I am a trained singer, also, and I would have to say, that it
depends on which book you have. Who wrote the bloody thing makes
a BIG difference in whether it's understandable or not.
:> The fact that I can make sense out of most manuals, some of which
:> really appear to be written with information obfuscation in mind,
:> doesn't mean that I expect everyone (even other programmers) to
:> be able to do so.
: If one can't make sense of a manual, then one needs to do something
: other than technical work. Public service employees with no technical
: knowledge read and comprehend procedural manuals on a regular basis.
: High school students (at least where I went to school) must be able to
: follow a dissection manual, a lab experiment manual, a writing style
: guide, and perhaps a cooking recipe or a prose assembly specification
: among other possible types of instruction manuals, including a syllabus
: for some classes. I went to a public high school, too, so it's probably
: not that special a case.
Procedural manuals, lab manuals of various sorts, style guides and
cooking recipes are generally written by people who not only know
what they are doing they know how to explain it. Computer manuals
(both hardware and software) at least give the impression that they
where written by engineers who didn't know how to explain, properly,
what needs to be done or tech writers who don't understand what needs
to be done but do their best to explain it anyway.
: Most people presume at least a high school diploma if not an associate's
: degree as a very minimum for a programmer, especially for someone who
: develops software full-time. Much more likely is an undergraduate degree
: or perhaps a graduate or advanced degree. I would guess you have at least
: an undergraduate degree yourself from your writing, so you should know
: how much reading and writing that can take.
Thank you for the compliment, but I ran out of money before I could
complete my Bachelor's degree. I do, however, know how much reading
and writing a degree (expecially in the soft sciences) takes. I had
PLANNED on a history degree. <shrug> I find I love computers even
more than I love history. (Didn't think that was possible. <G>)
:> Do have a good day, won't you?
: I would hope you do the same. :-)
Thank you.
SJ Straith
--
**************************
St Vidicon of Cathode,
protect me from Murphy.
**************************
Life's to short to suffer
fools gladly.
**************************
------------------------------
Date: 23 Feb 2001 15:40:23 -0600
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Basic: Arrays, shift, and the mystery element
Message-Id: <m3wvahulh4.fsf@dhcp9-179.support.tivoli.com>
Daniel Berger <djberge@uswest.com> writes:
> while($arrayref->[0]->{name} =~ /dan|ginger/i){
This line autovivifies a hash reference, if there isn't one, in which
to lookup "name". I think some people even consider this a bug,
though i expect there are reasons that it isn't going to change.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 24 Feb 2001 01:00:05 GMT
From: rfield@best.com ()
Subject: Re: Can't use 'pop' as hash key
Message-Id: <97712l$gjh$1@nntp1.ba.best.com>
Also
@key_vals = ("name", "address", "phone", "pop");
should work.
Bart Lateur <bart.lateur@skynet.be> writes:
>oakbox@my-deja.com wrote:
>>I tried to use the word 'pop' as a key in a hash, and it does not
>>work. Are their reserved words that cannot be hash keys?
>Sure they can. That's not your problem.
>>@key_vals=(name,address,phone,pop);
>Here's your problem. You're depending on perl autoquoting unrecognized
>barewords. Guess what: pop is a function, and it will be executed.
>This ought to work (if your perl is more recent than, say, 4.036 ;-)
> @key_vals = qw(name address phone pop);
>--
> Bart.
------------------------------
Date: Fri, 23 Feb 2001 23:15:37 GMT
From: "Bret Tucker" <Bret.Tucker@home.com>
Subject: Cross Reference Listing of Variables
Message-Id: <taCl6.4516$jg1.296726@news1.alsv1.occa.home.com>
SSBoYXZlIHNlYXJjaCBQZXJsIGNvbW1hbmQgbGluZSB2YXJpYWJsZXMgYW5kIGRvIG5vdCBzZWUg
aG93IHRvIGdldCBhIGxpc3Rpbmcgb2YgdmFyaWFibGVzIGluIHByb2dyYW06IA0KDQphLiBkZWNs
YXJlZCBidXQgdW51c2VkDQoNCmIuIGRlY2xhcmVkIGFuZCB1c2VkIGluIHRoZSBmb2xsb3dpbmcg
bGluZSAoYnkgbnVtYmVycykNCg0KQW55IGhlbHAgZ3JlYXRseSBhcHByZWNpYXRlZC4NCkJyZXQN
Cg==
------------------------------
Date: Sat, 24 Feb 2001 00:12:20 GMT
From: "Bertilo Wennergren" <bertilow@chello.se>
Subject: Re: Difficult Split Question
Message-Id: <E%Cl6.902$bX.9816@nntp1.chello.se>
Godzilla!:
> Bertilo Wennergren wrote:
> [...]
> > Great stuff! Simple and beautiful.
> > The whole war thing is of course just a way to make us all learn how
> > to code better. There's nothing like some friendly and good-humoured
> > competition! :-)
> Having been given ample time to gloat and masturbate your
> fragile masculine ego, under various fake names, you will
> eventually notice you have given yourself a rather large
> self-inflicted blackeye.
Sorry? What are you talking about? I haven't posted anything
under "various fake names".
That's me down there in the sig. I always use that name on the
Internet, because it is my name.
And I have no illusions that my code is perfect. I'm just trying
to learn by giving myself some difficult tasks, and trying to
improve on what others do.
--
#####################################################################
Bertilo Wennergren
<http://purl.oclc.org/net/bertilo>
<bertilow@chello.se>
#####################################################################
------------------------------
Date: Sat, 24 Feb 2001 00:15:41 GMT
From: "Bertilo Wennergren" <bertilow@chello.se>
Subject: Re: Difficult Split Question
Message-Id: <N2Dl6.903$bX.9680@nntp1.chello.se>
Joe Schaefer:
> Yes, and there are faster snippets than yours as well, but I
> won't humor the small-minded hobgoblins. Benchmarking code on
> a line-by-line basis is no way to learn how to program.
No. But the work you do trying to get the code to run faster
gives plenty of opportunity to find out new things about Perl.
But thanks anyway for pointing out mistakes in the code. It could
have been done in a slightly friendlier way though.
I find it a bit annoying that you use wors like "troll" and
"hobgoblin" without clearly stating who you mean. I even got
the feeling that you're calling me a troll or hobgoblin. If that
is so, why?
Now, back to my copy of the Camel (perhaps I should stick to
it rather than this group...).
--
#####################################################################
Bertilo Wennergren
<http://purl.oclc.org/net/bertilo>
<bertilow@chello.se>
#####################################################################
------------------------------
Date: Sat, 24 Feb 2001 11:15:46 +1000
From: "Gregory Toomey" <gtoomey@usa.net>
Subject: Re: flushing stdout
Message-Id: <eJDl6.2100$v5.7391@newsfeeds.bigpond.com>
I asked the same question a few weeks ago.
It all depends on how your Web server is set up. I tried all sorts of things
and none worked.
The only way would be to close stdout and stderr,
then do a fork system call and make sure the original process exits.
gtoomey
"Erland Nylend" <nylend@nextra.com> wrote in message
news:3a96e5c2.29287393@news.online.no...
> I have tried to make a script that pings www.vg.no, and prints the
> result to a html-page .... My problem is that when i run the script,
> my browser waits until the whole script is finished before it shows
> anything ...
>
> this doesnt work:
>
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> # Autoflush
> $| = 1;
> $tull = `ping -c 20 www.vg.no `;
> print $tull;
>
>
> Does anyone know how to make a script that prints one line at a time
> to stdout ?! It doesnt seem to work with my attempt to autoflush ...
>
> Bonusquestion: how do I make a script wait one second or so? Can i use
> wait(1), or something like that?
>
> for(@i=0;$i<20;i++)
> {
> $ping = `ping -c 1 www.vg.no`;
> #wait one second ....
> #flush the stdout ...
> print $ping;
> }
>
>
> Any help appreciated!!
>
> Regards,
> Erland Nylend
------------------------------
Date: 23 Feb 2001 17:48:30 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: HTML in a Perl Script
Message-Id: <87zofdhsfl.fsf@limey.hpcc.uh.edu>
>> On Fri, 23 Feb 2001 19:55:01 GMT,
>> Rachael Blank <blankrr@mars-systems.com> said:
> Hello! Thank you in advance for your assistance. I
> have the following subroutine that displays files:
> sub display_files { my ($files,$sort_by,@show) = @_; my
> @tr = (); foreach my $file (sort {$a->{$sort_by} cmp
> $b->{$sort_by}} @$files) { push @tr,'<TR><TD
> WIDTH=100><A HREF=\"$file\">'. join('</a></TD><TD
> WIDTH=100><A
> HREF=\"$file\">',@{$file}{@show}).'</a></TD> </TR>'; }
> The script produces a list of files(in this case, MS
> Word files) that are in a specific directory. It
> actually works without the bolded code. Now, I would
bolded code? Hmm...
> like to make them hyperlinked so that they click on a
> link to pull up the MS Word files in their IE browser.
> Right now, the hyperlink is:
> Obviously, the value of the $file var is not passed in
> correctly. I think it might be a syntax error.
Single quotes cause variable interpolation not to happen.
You need to change your quoting strategy to double quotes.
See "Quote and Quote-like Operators" in "perldoc perlop".
Alternatively you could also use a "here" document (<<) to
encapsulate the text.
Or you could use the HTML shortcuts as in CGI.pm, e.g.
print a({href => "/path/to/whatever/$file"}, 'text for link');
which I tend to prefer for generating HTML as they tend to
avoid at least 1 level of perl/HTML quoting interaction.
hth
t
--
The avalanche has already started.
It is too late for the pebbles to vote.
------------------------------
Date: Fri, 23 Feb 2001 16:37:13 -0800
From: "Mahesh A" <maheshasolkar@yahoo.com>
Subject: HTTP connection
Message-Id: <t9e0hprg5k1g2d@corp.supernews.com>
Hi,
How can i establish an HTTP connection with a host and communicate with it?
Thanks in advance,
M.
__END__
Reply...
Shameless creature... Haven't you tried...
perldoc ______________
------------------------------
Date: Sat, 24 Feb 2001 11:22:26 +1000
From: "Gregory Toomey" <gtoomey@usa.net>
Subject: Re: HTTP connection
Message-Id: <uPDl6.2114$v5.7014@newsfeeds.bigpond.com>
I answered a similar question recently.
This is my implementation, complete with cookies.
1. Open a Web page and store a cookie in a file.
use LWP::UserAgent;
use HTTP::Cookies;
$ua = new LWP::UserAgent;
$ua->agent("$0/0.1 " . $ua->agent);
$ua->timeout(30);
$req = new HTTP::Request 'GET' => 'http://www.somesite.com'; #change
as apprporiate
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if ($res->is_success) {
$cookie_jar = new HTTP::Cookies( ignore_discard =>TRUE);
$cookie_jar->extract_cookies($res);
$cookie_jar->save('/usr/local/cookie_jar); #change as
appropriate
#web page is now in the variable $res
}
2. Open a Web page and send the stored cookie
$ua = new LWP::UserAgent;
$ua->agent("$0/0.1 " . $ua->agent);
$ua->timeout(30);
$req = new HTTP::Request 'GET' =>
'http://www.somesite.com/';
$req = new HTTP::Request 'GET' =>
'http://www.somesite.com/somepage.htm'; #change as apprporiate
$req->header('Accept' => 'text/html');
$cookie_jar = new HTTP::Cookies( ignore_discard =>TRUE);
$cookie_jar->load('/usr/local/cookie_jar') #change as
appropriate
$cookie_jar->add_cookie_header($req);
$res = $ua->request($req);
if ($res->is_success) {
#web page is now in the variable $res
}
gtoomey
-------------
"Mahesh A" <maheshasolkar@yahoo.com> wrote in message
news:t9e0hprg5k1g2d@corp.supernews.com...
> Hi,
>
> How can i establish an HTTP connection with a host and communicate with
it?
>
> Thanks in advance,
> M.
> __END__
>
> Reply...
>
> Shameless creature... Haven't you tried...
>
> perldoc ______________
>
>
>
------------------------------
Date: Fri, 23 Feb 2001 18:19:18 -0500
From: John Crowley <crowj@aol.com>
Subject: Re: Is there a perl history equivalent?
Message-Id: <3A96EFF6.1C0D7E44@aol.com>
BUCK NAKED1 wrote:
>
> I want to show the last URL visited before someone visits my webpage,
> but I don't want to use JS. Is there a perl equivalent that shows the
> URL the user came from right before coming to your page. I looked at the
> modules Netscape::History, Netscape::HistoryURL, and DBI FIle, and
> couldn't find it there.
>
> Thanks,
> Dennis
HTTP allows for the referring URL (misspelled as referer) to be passed
to the server in the HTTP_REFERER environment variable. The referring
URL may not be the result of user input but from something programmed
such as a hyperlink or form submission. It is not required to be passed
by the client and is easily spoofed.
------------------------------
Date: 23 Feb 2001 17:53:43 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: max value in list?
Message-Id: <87vgq1hs6w.fsf@limey.hpcc.uh.edu>
>> On Fri, 23 Feb 2001 15:07:24 -0600,
>> "Phil R Lawrence" <prlawrence@lehigh.edu> said:
> I want to get the maximum value from a list of numbers.
> I could say: my @srtd = sort {$b <=> $a} (34, 12, 54,
> 17); my $max = $srtd[0]; ...but I hate to make a whole
> array when I only want one scalar.
> This works: my $max = $_ for ( sort {$a <=> $b} (34, 12,
> 54, 17) ) ...but I hate to reassign to $max for every
> element of the list.
> This works: my $max; for ( sort {$b <=> $a} (34, 12, 54,
> 17) ) { $max = $_; last; } ...but I hate having to
> declare $max in a preceeding statement.
> So, any suggestions for a more concise way to grab the
> max value from a list?
Well, you have to do a linear scan so you can't improve on
that.
You could do:
$max = (sort {$b <=> $a} qw(43 12 54 17))[0];
I suppose. Another way, which may or may not be drastic
overkill depending on what else you're doing in your
program, is to use Statistics::Descriptive, viz.
use Statistics::Descriptive;
$s = new Statistics::Descriptive::Sparse;
$s->add_data(qw(43 12 54 17));
print $s->max(), "\n";
hth
t
--
The avalanche has already started.
It is too late for the pebbles to vote.
------------------------------
Date: 24 Feb 2001 00:14:39 GMT
From: damian@cs.monash.edu.au (Damian Conway)
Subject: Re: max value in list?
Message-Id: <976udf$8gn$1@towncrier.cc.monash.edu.au>
Tony Curtis <tony_curtis32@yahoo.com> writes:
>> So, any suggestions for a more concise way to grab the
>> max value from a list?
use List::Util 'reduce';
$max = reduce { $_[0] > $_[1] ? $_[0] : $_[1] } @list;
or:
use Quantum::Superpositions;
$max = any(@list) >= all(@list);
Damian
------------------------------
Date: 24 Feb 2001 00:15:45 GMT
From: damian@cs.monash.edu.au (Damian Conway)
Subject: Re: max value in list?
Message-Id: <976ufh$78b$1@towncrier.cc.monash.edu.au>
Tony Curtis <tony_curtis32@yahoo.com> writes:
>> So, any suggestions for a more concise way to grab the
>> max value from a list?
...or, of course, boring old:
use List::Util 'max';
$max = max @list;
;-)
Damian
------------------------------
Date: Sat, 24 Feb 2001 01:30:01 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: max value in list?
Message-Id: <t9e3kps4av05cb@corp.supernews.com>
Phil R Lawrence <prlawrence@lehigh.edu> wrote:
> I want to get the maximum value from a list of numbers. I could
> say:
> my @srtd = sort {$b <=> $a} (34, 12, 54, 17);
> my $max = $srtd[0];
> ...but I hate to make a whole array when I only want one scalar.
> This works:
> my $max = $_ for ( sort {$a <=> $b} (34, 12, 54, 17) )
> ...but I hate to reassign to $max for every element of the list.
> This works:
> my $max;
> for ( sort {$b <=> $a} (34, 12, 54, 17) ) { $max = $_; last; }
> ...but I hate having to declare $max in a preceeding statement.
> So, any suggestions for a more concise way to grab the max value
> from a list?
Concise in chars total for golf, concise in instructions?
Depending upon where I'm doing it, I use one of the following
methods:
###---------------------------------- _begin_
#!/usr/bin/perl -w
use strict;
###
### the clear way
sub max(@) {
my $biggest = shift;
foreach(@_) {
$biggest = $_ if $_ > $biggest;
}
return $biggest;
}
my $max = max(3, 4, 5, 2, 1, 7, 6);
print $max ."\n";
###--------------------------------- _end_
which works well, or
###-------- _begin_
#!/usr/local/bin/perl -w
use strict;
###
### the somewhat obfuscated way
my($max, $foo);
for( @_ = (3, 2, 7, 5, 6, 1, 4); $foo = shift(@_); ) {
$max = $foo if( !(defined $max) or ($foo > $max));
}
print $max . "\n";
###-------- _end_
which pukes on any member of the list being zero, or maybe
###-------------- _begin_
#!/usr/local/bin/perl -w
use strict;
###
### the somewhat obfuscated way without prior definitions
for( @_ = (3, 2, 7, 5, 6, 1, 4), my($max, $foo); $foo = shift(@_); ) {
$max = $foo if( !(defined $max) or ($foo > $max));
unless( @_ ) {
print $max . "\n";
}
}
###-------------- _end_
which also pukes on zeros in the list, or
###-------------- _begin_
#!/usr/bin/perl -w
use strict;
###
### the one-line way
@_=sort{$b<=>$a}(3,5,2,6,9,7,4);print $_[0]."\n";
###-------------- _end_
which builds an array and throws all but the first element away
for your purposes, and leaves the rest in there to screw with
the rest of your program, since it's playing with a default global.
Icky. It does do it in one line, though. @_ is already in existence,
so if you don't need it for anything else you can get away with no
declaration and no additional namespace or memory usage. You could
be shot by a maintenance programmer, though.
Chris
--
Christopher E. Stith
The purpose of a language is not to help you learn the
language, but to help you learn other things by using the
language. --Larry Wall, The Culture of Perl, August 1997
------------------------------
Date: 23 Feb 2001 15:50:43 -0600
From: Ren Maddox <ren@tivoli.com>
Subject: Re: question about arrays
Message-Id: <m3lmqxukzw.fsf@dhcp9-179.support.tivoli.com>
damian@qimr.edu.au (Damian James) writes:
> Thus spake Damian James on 23 Feb 2001 02:21:27 GMT:
> >
> >This is equivalent to: @myarray = 0..9;
> > ^^^^^^
>
> Of course I meant @myarray = (0..9);
Why? The first version was fine. The parens don't do anything for
you there -- except contribute to the commonly held mistaken belief
that parens *create* lists. The only reason parens are normally
needed is because "=" binds tighter than ",". But ".." binds tighter
than "=", so they aren't need here.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 23 Feb 2001 16:02:25 -0600
From: Ren Maddox <ren@tivoli.com>
Subject: Re: question about arrays
Message-Id: <m3d7c9ukge.fsf@dhcp9-179.support.tivoli.com>
abigail@foad.org (Abigail) writes:
> The for loop can be written as: @myarray = ( 0 .. 9 );
> The second one can be written as: @myarray = ([0 .. 9]);
I'm surprised to see you include those parens... I guess they provide
some additional clarity, particular in the second case -- making it
clear that you really know that you are assigning a list reference to
the first element of @array.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 23 Feb 2001 15:59:46 -0600
From: Ren Maddox <ren@tivoli.com>
Subject: Re: question about arrays
Message-Id: <m3hf1lukkt.fsf@dhcp9-179.support.tivoli.com>
"Kurt Stephens" <kstep@pepsdesign.com> writes:
> There's a big difference between the () list constructor and the [] array
> reference constructor. Read perldata and perlref for details.
Minor nit: () is not a list constructor.
While reading perldata, be sure and take note of the first paragraph
under "List value constructors":
List values are denoted by separating individual values by
commas (and enclosing the list in parentheses where
precedence requires it):
The point being that the parens are only necessary if precedence rules
get in the way -- which the most certainly do for the common case of
assignment. A moderately interesting demonstration is:
@array =(1, 2, 0 or 3, 4, 5);
more mundane are any of the list operators:
print 1, 2, 3; # no parens required to make a list
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 23 Feb 2001 23:40:52 +0000
From: Jon Ericson <Jonathan.L.Ericson@jpl.nasa.gov>
Subject: Re: RFD - comp.lang.perl.db, comp.lang.perl.network, comp.lang.perl.regex
Message-Id: <86itm1x917.fsf@jon_ericson.jpl.nasa.gov>
Daniel Berger <djberge@uswest.com> writes:
> news.groups postings suggested that I should canvas relevant
> newsgroups to see if there was interest for some new newsgroups.
> Hopefully I am not flogging a dead horse here, but I haven't seen
> this mentioned before.
You might want to find a mentor to guide you through the RFD process -
ideally someone who is a regular on both comp.lang.perl.misc and
news.groups. (Identities of such individuals left as an exercise for
the reader. :)
> 1)questions about database interfaces and database drivers (the
> DBI/DBD modules, DBM files, etc). I realize that there is a mailing
> list for this, but I *much* prefer a newsgroup to a mailing list. I
> suspect others do also (but could be wrong).
You had best ask in the DBI mailing lists. (Subscribe at
http://www.isc.org/services/public/lists/dbi-lists.html.) Some people
prefer mailing lists. Unless they are interested in
comp.lang.perl.dbi as a news gateway or replacement to the mailing
list, this group is likely to be a non-starter.
> comp.lang.perl.db Discussion of perl as it relates to interfacing
> with databases of any sort. This would include the DBI module and
> any DBD module, including installation (and any problems therein)
> and use of those modules. Discussion of DBM hashes would also be on
> topic as well as development of any new modules as they relate to
> databases (DDL modules, for example).
I don't think you gain much by creating this group. Most database
related traffic is of the form:
Q: How do I use perl to interface with database X?
A: Use DBI.
Then the OP goes off to learn DBI and use the DBI mailing list.
> comp.lang.perl.regex Regular expressions - period. Generally, "How
> do I match ..." and "Here's how to match ..." would be the subject
> line of mose messages. I imagine obfuscated regex competitions
> would grow out of this as well.
Sometimes a regex question but is better solved another way. Regex
has life as a language outside Perl. Regexen are an important part of
Perl - I would hate to see discussion of them outlawed on c.l.p.m.
> comp.lang.perl.network CGI, LWP, and networking in general
> including, of course, sockets and the IO::Socket module, and any of
> the Net:: modules. This is the broadest of the three, and I
> considered a narrower newsgroup like comp.lang.perl.web strictly for
> CGI, Apache, etc, but figured that there would be too much overlap.
mod_perl also has a mailing list (http://perl.apache.org/#maillists).
The LWP web page (http://www.linpro.no/lwp/) says
A mailing list has been established for technical discussion about
libwww-perl, including problem reports, interim fixes, suggestions
for features, and contributions. General support questions should
be directed to the comp.lang.perl.modules USENET News group.
Jon
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 346
**************************************