[25251] in Perl-Users-Digest
Perl-Users Digest, Issue: 7496 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Dec 8 14:05:43 2004
Date: Wed, 8 Dec 2004 11:05:07 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 8 Dec 2004 Volume: 10 Number: 7496
Today's topics:
Catch Timeout for system calls in Windows? peterkayatwork@yahoo.com
Re: Catch Timeout for system calls in Windows? <ithinkiam@gmail.com>
Re: Determining connection type <cal@not.splitreflection.com>
Re: Determining connection type (Michael Fuhr)
Re: Dynamic Search Tool <jwillmore@fastmail.us>
Re: examples using PDF::Template <sbryce@scottbryce.com>
Re: examples using PDF::Template <botfood@yahoo.com>
Re: grammar function <uri@stemsystems.com>
Re: grammar function <notvalid@email.com>
Re: How to detect an undefined SV* value in XS? xhoster@gmail.com
Re: How to handle a HTTP::Request with gzip, deflate he <gisle@activestate.com>
Re: HTML2JPEG <katja_zina@web.de>
Maximum length of hash key <rob@nova.hbx.us>
Re: Maximum length of hash key <craig@codenation.net>
Re: Maximum length of hash key (Peter Corlett)
Re: Maximum length of hash key <mritty@gmail.com>
Re: Maximum length of hash key <craig@codenation.net>
Re: Maximum length of hash key <rob@nova.hbx.us>
Re: Maximum length of hash key <uri@stemsystems.com>
Re: Maximum length of hash key <uri@stemsystems.com>
Re: Maximum length of hash key <uri@stemsystems.com>
Re: Maximum length of hash key <No_4@dsl.pipex.com>
Re: Maximum length of hash key <craig@codenation.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 8 Dec 2004 07:14:45 -0800
From: peterkayatwork@yahoo.com
Subject: Catch Timeout for system calls in Windows?
Message-Id: <1102518885.886082.22430@c13g2000cwb.googlegroups.com>
I'm using ActiveState Perl on a Windows XP platform, and want to kick
off some scripts from one central location. However, I want to make
sure these scripts exit, and would like to timeout if they don't.
Ideally, I want to grab the output as well, but I can handle that by
redirecting to output files if I need.
Can anyone suggest a way to do this? Can anyone suggest what to look
for?
If I were doing this in a *nix environment, I'd try signals, but I
suspect that cygwin just isn't going to handle them; if anyone knows
better, let me know :)
Example of what won't work:
foreach my $script (@scripts) {
system($script); # no timeout!
}
Thanks,
--Peter
------------------------------
Date: Wed, 08 Dec 2004 15:42:37 +0000
From: Chris Cole <ithinkiam@gmail.com>
Subject: Re: Catch Timeout for system calls in Windows?
Message-Id: <pan.2004.12.08.15.42.35.233353@gmail.com>
On Wed, 08 Dec 2004 07:14:45 -0800, peterkayatwor wrote:
> I'm using ActiveState Perl on a Windows XP platform, and want to kick
> off some scripts from one central location. However, I want to make
> sure these scripts exit, and would like to timeout if they don't.
> Ideally, I want to grab the output as well, but I can handle that by
> redirecting to output files if I need.
>
> Can anyone suggest a way to do this? Can anyone suggest what to look
> for?
>
> If I were doing this in a *nix environment, I'd try signals, but I
> suspect that cygwin just isn't going to handle them; if anyone knows
> better, let me know :)
>
> Example of what won't work:
> foreach my $script (@scripts) {
> system($script); # no timeout!
> }
>
How about using an eval block with an alarm? Someething like:
foreach my $script (@scripts) {
eval {
alarm(360); # set time limit to 6 mins
system($script);
alarm(0); # if $script finishes in <6min reset alarm
}
if ($@) {
# do stuff if eval fails (eg timeout)
}
Chris.
------------------------------
Date: Wed, 08 Dec 2004 08:54:05 -0800
From: penguinista <cal@not.splitreflection.com>
Subject: Re: Determining connection type
Message-Id: <41b731ac$0$19032$d368eab@news.calweb.com>
Marshall Dudley wrote:
> In a script I have I have to throw an image normally if the connection
> is normal, and securily if it is secure. However I cannot find any way
> in perl to tell if the connection is secure or non secure. The $0
> strips the http vs https off of the value.
>
> Is there any way to determine this?
>
> Thanks,
>
> Marshall
>
try $ENV{"SERVER_PROTOCOL"}
------------------------------
Date: 8 Dec 2004 10:32:27 -0700
From: mfuhr@fuhr.org (Michael Fuhr)
Subject: Re: Determining connection type
Message-Id: <41b73aab$1_4@omega.dimensional.com>
penguinista <cal@not.splitreflection.com> writes:
> Marshall Dudley wrote:
> > In a script I have I have to throw an image normally if the connection
> > is normal, and securily if it is secure. However I cannot find any way
> > in perl to tell if the connection is secure or non secure. The $0
> > strips the http vs https off of the value.
> >
> > Is there any way to determine this?
>
> try $ENV{"SERVER_PROTOCOL"}
SERVER_PROTOCOL contains the application protocol, which will likely be
"HTTP/1.1" or "HTTP/1.0" regardless of the scheme ("http" or "https").
http://hoohoo.ncsa.uiuc.edu/cgi/env.html
http://www.rfc-editor.org/rfc/rfc3875.txt
What web server sets SERVER_PROTOCOL to different values for http vs.
https?
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
------------------------------
Date: Wed, 08 Dec 2004 10:08:41 -0500
From: James Willmore <jwillmore@fastmail.us>
Subject: Re: Dynamic Search Tool
Message-Id: <pan.2004.12.08.15.08.24.275447@fastmail.us>
On Wed, 08 Dec 2004 00:20:24 -0800, xtracold wrote:
<...>
> I'm not looking for a written solution, i'm looking for peoples past
> experience of binding to each keystroke event in an Tk::Entry widget
> and what performance I can expect. Perhaps there is another existing
> add-on that I don't know about that already does this?
Might I suggest that you take the above posting and re-post it to a more
appropriate newsgroup - comp.lang.perl.tk
Or, just read the Tk::Entry documentation and any documentation the
Tk::Entry documentation references, write some code, see how it works,
maybe do some benchmarking ... you know, see for yourself :-)
I can say this - depending upon what you're trying to do, there are
widgets that have builtin key bindings that aid in making a robust bit of
code. For example, the Tk::Text widget has a very nice right-mouse click
menu ready to use that include the ability to go to a particular line of a
file. I believe that it also has the more common keystroke bindings (like
"Ctrl-C" to copy text and "Ctrl-V" to paste).
And I have found "Mastering Perl/Tk" (O'Reilly publishes this book -
*not* the "Learning Perl/Tk" book ... it's not as helpful as "Mastering
Perl/Tk") to be a very good reference with losts of good examples. This,
of course, is if you want to but a book :-)
HTH
Jim
------------------------------
Date: Wed, 08 Dec 2004 09:11:20 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: examples using PDF::Template
Message-Id: <OP-dnffVpNI0uircRVn-rg@comcast.com>
botfood wrote:
> - Can I take an existing authored .pdf page and clone all the
> margins settings and stuff like that?
Probably not.
> - Can I author a template with any special tags where I want to plug
> in my variable fields?
Probably not.
A PDF file includes a cross reference that has byte offsets to all of
the objects in the file. Any modifications in the PDF file will require
an update of the cross reference.
> If you have any tips, tutorials, or examples illustrating these modules
> that would help me determine if this is the way to go....
The first thing is to become familiar with the PDF spec. The spec has
been available on Adobe's web site. I can't find it right now. The file
name is PDFReference15_v6.pdf. (A subsequent update may have a different
file name.)
PDF files were not designed for easy modification, so trying to fill in
a template can be as much effort as creating a new PDF from scratch.
As for PDF::Template and PDF::Create, I don't use them. I couldn't find
documentation for them, and they do much more than I need them to. I
wrote my own module to create simple PDFs. In order to use that module,
one would have to be very familiar with portions of the PDF spec.
Try posting to comp.text.pdf and see if anyone there has some ideas.
------------------------------
Date: 8 Dec 2004 09:16:04 -0800
From: "botfood" <botfood@yahoo.com>
Subject: Re: examples using PDF::Template
Message-Id: <1102526164.910630.269880@c13g2000cwb.googlegroups.com>
botfood wrote:
> I have a possible project I am looking into that is basically
creating
> a "mail-merge" utility that would chew through a big CSV spreadsheet
> and merge the fields into a template to generate a big .pdf document.
> The reason the client wants to go .pdf is to have a "print ready"
> format.
> --------------------------
after a night of poking around, and you all's input, it looks like
creating .pdf files on the fly is a pretty involved and painful
process. PDF::Template has potential for what I want to do, but
requires learning special XML syntax to set up the template, and buying
the PDFlib for about $450. PDF::Create has some simple capabilities,
but neither look straightforward to use in a mail-merge application as
there are some complex issues with pagination and text position.
Neither module had very complete docs, and I couldn't find any
reasonably complete tutorials or examples online.
I read thru the overview and some syntax of the actual .pdf spec on the
adobe site... and while a super simple layout might be pretty easy to
code, it would get REAL deep real fast.
so.... just thought I'd post a followup in case somebody googles later.
Its looking to me like its a lot easier to generate the output is a
text, html, or other man-readable format and then use a cheap "print to
pdf" convertor at the backend.
if there is somebody out there who has actually used PDF::Create and/or
PDF::Template and would like to share some thoughts on the
capabilities/limitations, ease of use, examples, etc, I still think it
would be great to share some knowledge of these packages.
I realize that some people feel this group is for asking questions
after you get stuck... but I HOPE there are also people out their
interested in sharing experiences with specific packages to help others
evaluate alternatives before investing a lot of learning time and test
code.
d
------------------------------
Date: Wed, 08 Dec 2004 16:33:22 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: grammar function
Message-Id: <x7653coilq.fsf@mail.sysarch.com>
>>>>> "KS" == Ken Sington <ken_sington@nospam_abcdefg.com> writes:
KS> And I'm using American English where colour became color and
KS> centre became center.
KS> Or worst, "culla" "centa".
s/t/e/ since we are being grammar pedants in this thread. :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 08 Dec 2004 17:03:04 GMT
From: Ala Qumsieh <notvalid@email.com>
Subject: Re: grammar function
Message-Id: <ctGtd.30539$zx1.11850@newssvr13.news.prodigy.com>
Ken Sington wrote:
> I've been working on a simple one word grammar checker.
Simple is a good description. Unfortunately, the problem you are seeking
to solve is a hard one, and has been a topic of AI research for years.
> I'm hoping other's can add ideas to it.
>
> you access the function like this:
> my $word = grammarAdjust("command", "word", <quantity>);
>
> so table becomes tables if there are more than one.
> chair remains chair if there are one or zero.
>
> if there are more than one james, we get jameses. and if something
> belongs to them, it's jameses'
Alas, life is not that easy. Perhaps with other languages this might
work, but English, although has a relatively simple grammar, has way too
many special cases.
>
> # grammarAdjust
> #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
> # takes single word and determines proper word grammar
> # to use:
> # grammarAdjust("command", "word", <quantity>);
> # commands:
> # ("plural", "word", <qty>)
> # ("possessive", "word", <qty>)
> sub grammarAdjust {
> my ($command, $word, $qty) = @_;
> my ($returnIt, $s)=();
>
> if ($command eq "plural"){
> if ($qty > 1){
> if (1 == 10){
> # 1 is not 10 of course
Why do you need this test, if I may ask?
> } elsif ($word =~ m/es$/){
> $returnIt = $word ."es"; # james -> jameses
> } elsif ($word =~ m/x$/){
> $returnIt = $word . "es"; # box -> boxes
What about "index"? -> indices.
> } elsif ($word =~ m/ey$/){
> $returnIt = $word =~ s/ey$/ies/; # monkey -> monkies
What about "key"? -> keys.
> } elsif ($word =~ m/oy$/){
> $returnIt = $word . "s"; # boy -> boys
> } elsif ($word =~ m/^(woman|man)$/){
> $word =~ s/an$/en/; # (wo)man -> (wo)men
It's "cleaner" not to use capturing parentheses, if you don't need them:
$word =~ /^(?:wo)?man$/
Speaking of which, you don't handle the word "parenthesis" correctly.
> $returnIt = $word;
> } elsif ($word !~ m/s$/){
> $returnIt = $word ."s"; # word -> words
> }
What about "class"? -> classes.
Other inconsistencies off the top of my head:
datum -> data.
information -> information.
news -> news.
stigma -> stigmata.
There are many, many more of course. Hypothetically, you can handle
everything with a sufficient number of elsifs, but I don't see the point
in this.
Good luck :)
--Ala
------------------------------
Date: 08 Dec 2004 16:58:10 GMT
From: xhoster@gmail.com
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <20041208115810.451$yp@newsreader.com>
"jl_post@hotmail.com" <jl_post@hotmail.com> wrote:
> Ben Morrow said:
> >
> > I *believe* that all you need to do is
> > if (sv != &PL_sv_undef)
> >
> > If I am wrong here I would appreciate correction... :)
>
> I don't think that's correct. I might be wrong, but I think that
> &PL_sv_undef is a macro that's used when one wants to return an
> undefined value, either by pushing it onto the return stack or setting
> RETVAL to it, like this:
>
> RETVAL = &PL_sv_undef;
>
> Thanks to the replies of other posters, I now know of three
> different ways to check for an undefined value:
>
> if (SvTYPE(sv) == SVt_NULL) printf("Undefined");
> # Contributed by Rob (Sisyphus):
> if (SvANY(sv) == NULL) printf("Undefined");
> # Contributed by Tassilo v. Parseval:
> if (SvOK(sv) == false) printf("Undefined");
>
> Of these three methods, only SvOK is documented in "perldoc
> perlapi", which is worth considering if portability is a concern. But
> I tested all three methods, and they all seem to work.
> Thanks for all who replied!
SvANY doesn't seem to work properly for me.
It claims a variable that was once defined but is now undef as if it
were defined. It also treats array slots which are off the end of the
array differently than those "inside" the array (but never assigned to).
Xho
use strict;
use Inline 'C' ;
my @x;
$x[3]=5;
$x[1]=4;
$x[1]=undef;
$x[2]='';
my $h=4;
$h=undef;
print foo($h);
foreach (0..6) {
print foo($x[$_]);
};
__DATA__
__C__
int foo(SV* sv) {
if (SvANY(sv) == NULL ) return 1;
return 0;
};
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 08 Dec 2004 15:19:33 +0100
From: Gisle Aas <gisle@activestate.com>
Subject: Re: How to handle a HTTP::Request with gzip, deflate headers
Message-Id: <877jnsg9e2.fsf@ask.g.aas.no>
Gisle Aas <gisle@activestate.com> writes:
> I'll see if I can hack libwww-perl to retry decoding with the
> "deflate" format if decoding according to "zlib" fails. Seems like a
> good idea to be MSIE bug compatible again :-(
This patch fixes the problem for me. It will be in libwww-perl-5.803
when released.
Index: lib/HTTP/Message.pm
===================================================================
RCS file: /cvsroot/libwww-perl/lwp5/lib/HTTP/Message.pm,v
retrieving revision 1.55
diff -u -p -r1.55 Message.pm
--- lib/HTTP/Message.pm 6 Dec 2004 13:27:20 -0000 1.55
+++ lib/HTTP/Message.pm 8 Dec 2004 14:01:40 -0000
@@ -201,8 +201,39 @@ sub decoded_content
}
elsif ($ce eq "deflate") {
require Compress::Zlib;
- $content_ref = \Compress::Zlib::uncompress($$content_ref);
- die "Can't inflate content" unless defined $$content_ref;
+ my $out = Compress::Zlib::uncompress($$content_ref);
+ unless (defined $out) {
+ # "Content-Encoding: deflate" is supposed to mean the "zlib"
+ # format of RFC 1950, but Microsoft got that wrong, so some
+ # servers sends the raw compressed "deflate" data. This
+ # tries to inflate this format.
+ unless ($content_ref_iscopy) {
+ # the $i->inflate method is documented to destroy its
+ # buffer argument
+ my $copy = $$content_ref;
+ $content_ref = \$copy;
+ $content_ref_iscopy++;
+ }
+
+ my($i, $status) = Compress::Zlib::inflateInit(
+ WindowBits => -Compress::Zlib::MAX_WBITS(),
+ );
+ my $OK = Compress::Zlib::Z_OK();
+ die "Can't init inflate object" unless $i && $status == $OK;
+ ($out, $status) = $i->inflate($content_ref);
+ if ($status != Compress::Zlib::Z_STREAM_END()) {
+ if ($status == $OK) {
+ $self->push_header("Client-Warning" =>
+ "Content might be truncated; incomplete deflate stream");
+ }
+ else {
+ # something went bad, can't trust $out any more
+ $out = undef;
+ }
+ }
+ }
+ die "Can't inflate content" unless defined $out;
+ $content_ref = \$out;
$content_ref_iscopy++;
}
elsif ($ce eq "compress" || $ce eq "x-compress") {
--
Gisle Aas
------------------------------
Date: Wed, 8 Dec 2004 19:57:02 +0100
From: Katja Zinchenko <katja_zina@web.de>
Subject: Re: HTML2JPEG
Message-Id: <Pine.LNX.4.58.0412081919410.6966@isar.geschichte.uni-muenchen.de>
*** Roman M. Parparov, 07-Dec-04, 14:06 h ***
> katja <katja_zina@web.de> wrote:
> > How can I convert an Internet-page to a PDF/JPEG-file from a
> > Perl-script under Linux? (I would like to make a screenshot from a
> > command line).
> > Thank you
> > Katja
>
> Since every browser for every platform for every user renders the WWW
> pages differently, it is unclear whether your task is well-defined, thus
> an answer should not be possible. You can download the contents of a web
> page as a text file easily. Converting text files on Linux is often
> done using wv suite (wvText, wvPDF et al.) which operates installed
> converting programs like latex, distill and others through a shell script
> wrapper.
Thanks for the hint, and sorry, yes, my question was too vague. What I wanted to
ask is: Is there a perlish way to save the contents of a web page to an image
file exactly the way a browser (any recent browser) would render it? I'd like to
pass my script a url and automatically get a kind of screen shot of the web
page. There doesn't seem to be a perl module (at least not on CPAN) to that
purpose, and as far as I could find out, mozilla, firefox, galeon, konqueror and
opera don't support any command line options to have them print to a file.
Thank you,
Katja
------------------------------
Date: Wed, 8 Dec 2004 16:07:47 +0100
From: Robert Manea <rob@nova.hbx.us>
Subject: Maximum length of hash key
Message-Id: <3c57pc.bhi.ln@rob.unisolblade.de>
Hello,
I'm searching for different code snippets (Golfers welcome) to solve the
following problem:
#v+
%category = {
'Mozilla' => 20,
'Forte Agent' => 12,
'slrn' => 45
};
#v-
Find the longest key (most characters) and return
'$longest_key_length + 1;'.
Since the hash can get pretty big, efficiency should be an issue.
Currently I'm using this:
#v+
my @arr; for ( keys %category ) {$arr[length $_] = defined;};
my $longest_key_length = $#arr+1;
#v-
in favor over the more golfish one liner:
#v+
my $longest_key_length =
1 + length +(sort { length $b <=> length $a } keys %category)[0];
#v-
Any other suggestions are highly appreciated.
Thx & Greets, Rob
--
The Enterprise meets God, and it's a child, a computer, or a C program.
------------------------------
Date: Wed, 8 Dec 2004 15:33:06 +0000
From: Craig Dunn <craig@codenation.net>
Subject: Re: Maximum length of hash key
Message-Id: <Pine.LNX.4.61.0412081530400.11433@gw.codenation.net>
> Find the longest key (most characters) and return
> '$longest_key_length + 1;'.
[snip]
>
> in favor over the more golfish one liner:
>
> my $longest_key_length =
> 1 + length +(sort { length $b <=> length $a } keys %category)[0];
>
Or another way:
my $l = (sort{$b<=>$a} map{length($_)} keys(%category))[0]+1;
Craig
------------------------------
Date: 08 Dec 2004 15:49:45 GMT
From: abuse@dopiaza.cabal.org.uk (Peter Corlett)
Subject: Re: Maximum length of hash key
Message-Id: <41b72299$0$215$5a6aecb4@news.aaisp.net.uk>
Robert Manea <rob@nova.hbx.us> wrote:
> I'm searching for different code snippets (Golfers welcome) to solve the
> following problem:
> %category = {
> 'Mozilla' => 20,
> 'Forte Agent' => 12,
> 'slrn' => 45
> };
I don't think that means what you think it means.
> Find the longest key (most characters) and return
> '$longest_key_length + 1;'.
If we're playing golf, I'm going to offer:
my $l;
$l|=$_ foreach keys %category; $l=1+length $l;
--
PGP key ID E85DC776 - finger abuse@mooli.org.uk for full key
------------------------------
Date: Wed, 08 Dec 2004 15:52:20 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Maximum length of hash key
Message-Id: <UqFtd.5518$MS6.1154@trndny01>
"Robert Manea" <rob@nova.hbx.us> wrote in message
news:3c57pc.bhi.ln@rob.unisolblade.de...
> Hello,
>
> I'm searching for different code snippets (Golfers welcome) to solve
the
> following problem:
>
> #v+
>
> %category = {
> 'Mozilla' => 20,
> 'Forte Agent' => 12,
> 'slrn' => 45
> };
> #v-
>
Do you know you just created a hash with one key - a hash ref - that
contains no value?
> Find the longest key (most characters) and return
> '$longest_key_length + 1;'.
>
> Since the hash can get pretty big, efficiency should be an issue.
Then sorting is definately not something you want to be doing.
> Currently I'm using this:
> my @arr; for ( keys %category ) {$arr[length $_] = defined;};
Why 'defined'? Why bother checking the return value of defined($_),
instead of just using 1?
> my $longest_key_length = $#arr+1;
>
>
> in favor over the more golfish one liner:
>
> my $longest_key_length =
> 1 + length +(sort { length $b <=> length $a } keys %category)[0];
Again, sorting will be a costly operation if the point is just to find
the maximal value. An iterative solution would seem to be preferred:
> Any other suggestions are highly appreciated.
%hash = ('foo'=>1, 'foobar'=>2, 'a'=>3, 'asdfasd'=>4, 'ab'=>5);
length($_) > $len and $len = length($_) for keys %hash;
print "Max Length plus 1 = " . ($len + 1) . "\n";
Paul Lalli
------------------------------
Date: Wed, 8 Dec 2004 16:36:26 +0000
From: Craig Dunn <craig@codenation.net>
Subject: Re: Maximum length of hash key
Message-Id: <Pine.LNX.4.61.0412081633000.12989@gw.codenation.net>
> "Robert Manea" <rob@nova.hbx.us> wrote in message
> news:3c57pc.bhi.ln@rob.unisolblade.de...
> Again, sorting will be a costly operation if the point is just to find
> the maximal value. An iterative solution would seem to be preferred:
>
>> Any other suggestions are highly appreciated.
>
> %hash = ('foo'=>1, 'foobar'=>2, 'a'=>3, 'asdfasd'=>4, 'ab'=>5);
> length($_) > $len and $len = length($_) for keys %hash;
> print "Max Length plus 1 = " . ($len + 1) . "\n";
Or back to the golf...
$len = length>$len?length:$len for keys %hash;$len++;
------------------------------
Date: Wed, 8 Dec 2004 17:36:37 +0100
From: Robert Manea <rob@nova.hbx.us>
Subject: Re: Maximum length of hash key
Message-Id: <lia7pc.npi.ln@rob.unisolblade.de>
Segfault in module "Peter Corlett" - dump details are as follows:
> Robert Manea <rob@nova.hbx.us> wrote:
>> I'm searching for different code snippets (Golfers welcome) to solve the
>> following problem:
>> %category = {
>> 'Mozilla' => 20,
>> 'Forte Agent' => 12,
>> 'slrn' => 45
>> };
> I don't think that means what you think it means.
You're pretty much thinking duly. Obviously I meant to mean '(' and ')'.
>> Find the longest key (most characters) and return
>> '$longest_key_length + 1;'.
> If we're playing golf, I'm going to offer:
> my $l;
> $l|=$_ foreach keys %category; $l=1+length $l;
That's a real nice one. Thanks for it.
Greets, Rob
--
The Enterprise meets God, and it's a child, a computer, or a C program.
------------------------------
Date: Wed, 08 Dec 2004 16:35:36 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Maximum length of hash key
Message-Id: <x73bygoihz.fsf@mail.sysarch.com>
>>>>> "CD" == Craig Dunn <craig@codenation.net> writes:
>> Find the longest key (most characters) and return
>> '$longest_key_length + 1;'.
CD> [snip]
>>
>> in favor over the more golfish one liner:
>>
>> my $longest_key_length =
>> 1 + length +(sort { length $b <=> length $a } keys %category)[0];
>>
CD> Or another way:
CD> my $l = (sort{$b<=>$a} map{length($_)} keys(%category))[0]+1;
ewww! O(N log N) for an O(N) problem? use List::Util::max
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 08 Dec 2004 16:39:56 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Maximum length of hash key
Message-Id: <x7zn0on3qb.fsf@mail.sysarch.com>
>>>>> "RM" == Robert Manea <rob@nova.hbx.us> writes:
RM> Find the longest key (most characters) and return
RM> '$longest_key_length + 1;'.
RM> Since the hash can get pretty big, efficiency should be an issue.
RM> my @arr; for ( keys %category ) {$arr[length $_] = defined;};
^^^^^^^
what is 'defined' in this case? it may actually work since keys are
always defined and $_ is aliased to each key and defined works on $_ by
default. but that is a FUGLY way to get a simple true value. just use 1.
RM> my $longest_key_length = $#arr+1;
@arr is better.
use List::Util qw( max ) ;
my $max_len = max( map length, keys %category ) + 1 ;
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 08 Dec 2004 16:49:43 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Maximum length of hash key
Message-Id: <x7k6rsn3a1.fsf@mail.sysarch.com>
>>>>> "RM" == Robert Manea <rob@nova.hbx.us> writes:
>> my $l;
>> $l|=$_ foreach keys %category; $l=1+length $l;
RM> That's a real nice one. Thanks for it.
should be very slow as it has to copy and or each of the keys and you
said this is large hash. the solution i posted aliases to each key (so
no copying) and length and max are fast. for your education benchmark
them (on a large hash) and post the results.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 08 Dec 2004 16:56:18 +0000
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Maximum length of hash key
Message-Id: <41b73233$0$16575$cc9e4d1f@news-text.dial.pipex.com>
Craig Dunn wrote:
> Or back to the golf...
>
> $len = length>$len?length:$len for keys %hash;$len++;
Shouldn't that be ... for $len keys ....
And there is this:
$k^=$_ for keys%hash;length($k)+1
However, this needs to read every character of every key - I would
expect the original idea (set an array with length(key) and then check the
length of the array) to be much quicker.
--
-*- Just because I've written it here doesn't -*-
-*- mean that you should, or I do, believe it. -*-
------------------------------
Date: Wed, 8 Dec 2004 17:05:36 +0000
From: Craig Dunn <craig@codenation.net>
Subject: Re: Maximum length of hash key
Message-Id: <Pine.LNX.4.61.0412081704070.12989@gw.codenation.net>
>> Or back to the golf...
>>
>> $len = length>$len?length:$len for keys %hash;$len++;
>
> Shouldn't that be ... for $len keys ....
Nope. You're setting $len to $_ if the length of $_ is greater than $len.
> $k^=$_ for keys%hash;length($k)+1
Nicer :)
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 7496
***************************************