[30977] in Perl-Users-Digest
Perl-Users Digest, Issue: 2222 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 20 03:09:46 2009
Date: Fri, 20 Feb 2009 00:09:09 -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 Fri, 20 Feb 2009 Volume: 11 Number: 2222
Today's topics:
Re: extracting values from vmstat output <xml.devel@gmail.com>
Re: FAQ 6.11 How do I use a regular expression to strip <brian.d.foy@gmail.com>
new CPAN modules on Fri Feb 20 2009 (Randal Schwartz)
Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <larry@example.invalid>
Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <tadmc@seesig.invalid>
Re: Posting Guidelines for comp.lang.perl.misc ($Revisi <tim@burlyhost.com>
Re: Read/write with UCS-2* encodings - Possible??? <nospam-abuse@ilyaz.org>
Re: Sorting based on existence of keys <uri@stemsystems.com>
Re: Sorting based on existence of keys <hjp-usenet2@hjp.at>
Re: Sorting based on existence of keys <uri@stemsystems.com>
Re: Sorting based on existence of keys sln@netherlands.com
Re: Sorting based on existence of keys <whynot@pozharski.name>
Re: Sorting based on existence of keys <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 19 Feb 2009 21:04:32 -0800 (PST)
From: Ashish Kumar <xml.devel@gmail.com>
Subject: Re: extracting values from vmstat output
Message-Id: <c31a724c-9b1a-4cd8-8792-00dd1c03f4f2@y38g2000prg.googlegroups.com>
thank you so much list for the beautiful codes and the explanations.
------------------------------
Date: Thu, 19 Feb 2009 14:22:25 -0800
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 6.11 How do I use a regular expression to strip C style comments from a file?
Message-Id: <190220091422259735%brian.d.foy@gmail.com>
In article <hd9mp45nkj34dtm8qupq2dhc37f9dg2aai@4ax.com>,
<sln@netherlands.com> wrote:
> On Mon, 16 Feb 2009 12:03:01 -0800, PerlFAQ Server <brian@stonehenge.com>
> wrote:
> faq> A slight modification also removes C++ comments, as long as they are
> not
> faq> spread over multiple lines using a continuation character):
> faq>
> faq>
> s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"
> '\\]*)#defined $2 ? $2 : ""#gse;
> ^^^^^^^^
> ^^^^^^^^^^^^^^^
> //([^\\]|[^\n][\n]?)*?\n
> defined $3 ? $3
>
> With another slight modification, C++ comments can be removed even if they
> span lines via continuation character(s) '\';
I've patched perlfaq6 for your update.
Thanks, :)
------------------------------
Date: Fri, 20 Feb 2009 05:42:25 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Fri Feb 20 2009
Message-Id: <KFCnuq.1424@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
Acme-CPANAuthors-0.07
http://search.cpan.org/~ishigaki/Acme-CPANAuthors-0.07/
We are CPAN authors
----
Apache2-ASP-2.30
http://search.cpan.org/~johnd/Apache2-ASP-2.30/
ASP for Perl, reloaded.
----
Apache2-Response-FileMerge-1.001
http://search.cpan.org/~wazzuteke/Apache2-Response-FileMerge-1.001/
Merge and include static client-side style and code
----
Audio-Digest-MP3-0.1
http://search.cpan.org/~bartl/Audio-Digest-MP3-0.1/
Get a message digest for the audio stream out of an MP3 file (skipping ID3 tags)
----
Business-MaxMind-1.49
http://search.cpan.org/~borisz/Business-MaxMind-1.49/
----
Business-MaxMind-1.50
http://search.cpan.org/~borisz/Business-MaxMind-1.50/
----
CGI-Auth-FOAF_SSL-0.02
http://search.cpan.org/~tobyink/CGI-Auth-FOAF_SSL-0.02/
Authentication using FOAF+SSL.
----
Catalyst-Example-InstantCRUD-0.0.26
http://search.cpan.org/~zby/Catalyst-Example-InstantCRUD-0.0.26/
A CRUD scaffolding for Catalyst.
----
Catalyst-Example-InstantCRUD-0.0.27
http://search.cpan.org/~zby/Catalyst-Example-InstantCRUD-0.0.27/
A CRUD scaffolding for Catalyst.
----
DBD-DB2-1.6
http://search.cpan.org/~ibmtordb2/DBD-DB2-1.6/
DataBase Driver for DB2 UDB
----
DBD-DB21.6
http://search.cpan.org/~ibmtordb2/DBD-DB21.6/
----
Data-Visitor-Encode-0.10004
http://search.cpan.org/~dmaki/Data-Visitor-Encode-0.10004/
Encode/Decode Values In A Structure
----
DateTime-Format-Natural-0.75
http://search.cpan.org/~schubiger/DateTime-Format-Natural-0.75/
Create machine readable date/time with natural parsing logic
----
DateTimeX-Format-POSIX-Strptime-00.01_02
http://search.cpan.org/~ecarroll/DateTimeX-Format-POSIX-Strptime-00.01_02/
OO interface into the POSIX library's strptime
----
DateTimeX-Lite-0.00001_03
http://search.cpan.org/~dmaki/DateTimeX-Lite-0.00001_03/
A Low Calorie DateTime
----
Devel-Pragma-0.50
http://search.cpan.org/~chocolate/Devel-Pragma-0.50/
helper functions for developers of lexical pragmas
----
Devel-Pragma-0.51
http://search.cpan.org/~chocolate/Devel-Pragma-0.51/
helper functions for developers of lexical pragmas
----
Devel-Pragma-0.52
http://search.cpan.org/~chocolate/Devel-Pragma-0.52/
helper functions for developers of lexical pragmas
----
Drupal-Admin-.01
http://search.cpan.org/~durist/Drupal-Admin-.01/
screen scraping Perl API to some Drupal admin functions
----
ExtUtils-MakeMaker-6.49_01
http://search.cpan.org/~mschwern/ExtUtils-MakeMaker-6.49_01/
Create a module Makefile
----
HTML-FormHandler-0.16
http://search.cpan.org/~gshank/HTML-FormHandler-0.16/
form handler written in Moose
----
HTML-FormHandler-0.17
http://search.cpan.org/~gshank/HTML-FormHandler-0.17/
form handler written in Moose
----
HTML-FormatExternal-14
http://search.cpan.org/~kryde/HTML-FormatExternal-14/
HTML to text formatting using external programs
----
HTTP-Engine-0.1.4_02
http://search.cpan.org/~yappo/HTTP-Engine-0.1.4_02/
Web Server Gateway Interface and HTTP Server Engine Drivers (Yet Another Catalyst::Engine)
----
HTTP-Engine-Middleware-0.08_02
http://search.cpan.org/~yappo/HTTP-Engine-Middleware-0.08_02/
middlewares distribution
----
IPC-Lite-0.1.22
http://search.cpan.org/~earonesty/IPC-Lite-0.1.22/
Share variables between processes
----
IPC-Lite-0.1.23
http://search.cpan.org/~earonesty/IPC-Lite-0.1.23/
Share variables between processes
----
IPC-Lite-0.1.26
http://search.cpan.org/~earonesty/IPC-Lite-0.1.26/
Share variables between processes
----
Image-ANSI-0.10
http://search.cpan.org/~bricas/Image-ANSI-0.10/
(DEPRECATED) Load, create, manipulate and save ANSI files
----
Image-XBin-0.06
http://search.cpan.org/~bricas/Image-XBin-0.06/
(DEPRECATED) Load, create, manipulate and save XBin image files
----
JSON-DWIW-0.29
http://search.cpan.org/~dowens/JSON-DWIW-0.29/
JSON converter that Does What I Want
----
Method-Lexical-0.04
http://search.cpan.org/~chocolate/Method-Lexical-0.04/
private methods and lexical method overrides
----
Moose-0.71
http://search.cpan.org/~drolsky/Moose-0.71/
A postmodern object system for Perl 5
----
MooseX-MethodAttributes-0.03
http://search.cpan.org/~flora/MooseX-MethodAttributes-0.03/
code attribute introspection
----
NCBIx-BigFetch-0.55
http://search.cpan.org/~rogerhall/NCBIx-BigFetch-0.55/
Retrieve very large NCBI sequence result sets based on keyword search
----
Net-Jifty-0.10
http://search.cpan.org/~jesse/Net-Jifty-0.10/
interface to online Jifty applications
----
Net-Jifty-0.11
http://search.cpan.org/~jesse/Net-Jifty-0.11/
interface to online Jifty applications
----
Net-SMTP-Pipelining-v0.0.1
http://search.cpan.org/~marcb/Net-SMTP-Pipelining-v0.0.1/
Send email using ESMTP PIPELINING extension
----
Net-Trac-0.07
http://search.cpan.org/~jesse/Net-Trac-0.07/
Interact with a remote Trac instance
----
Net-Twitter-2.08
http://search.cpan.org/~cthom/Net-Twitter-2.08/
Perl interface to twitter.com
----
PAR-0.986
http://search.cpan.org/~smueller/PAR-0.986/
Perl Archive Toolkit
----
PAR-Repository-0.19
http://search.cpan.org/~smueller/PAR-Repository-0.19/
Create and modify PAR repositories
----
PAR-Repository-Client-0.24
http://search.cpan.org/~smueller/PAR-Repository-Client-0.24/
Access PAR repositories
----
POE-Component-Client-HTTP-0.88
http://search.cpan.org/~rcaputo/POE-Component-Client-HTTP-0.88/
a HTTP user-agent component
----
POE-Component-Pool-DBI-0.014
http://search.cpan.org/~tag/POE-Component-Pool-DBI-0.014/
Simplified DBI access through a pooled resource.
----
POE-Declare-0.03
http://search.cpan.org/~adamk/POE-Declare-0.03/
A POE abstraction layer for conciseness and simplicity
----
Parse-BBCode-0.07
http://search.cpan.org/~tinita/Parse-BBCode-0.07/
Module to turn BBCode into HTML or plain text
----
Pod-Coverage-0.20
http://search.cpan.org/~rclamp/Pod-Coverage-0.20/
Checks if the documentation of a module is comprehensive
----
Queue-Q4M-0.00017
http://search.cpan.org/~dmaki/Queue-Q4M-0.00017/
Simple Interface To q4m
----
SVGraph-0.01
http://search.cpan.org/~comsultia/SVGraph-0.01/
Library that generates cool SVG graphs
----
Scope-Upper-0.07
http://search.cpan.org/~vpit/Scope-Upper-0.07/
Act on upper scopes.
----
Search-Indexer-Incremental-MD5-0.02.6
http://search.cpan.org/~nkh/Search-Indexer-Incremental-MD5-0.02.6/
Incrementaly index your files
----
Task-BeLike-RJBS-20090219
http://search.cpan.org/~rjbs/Task-BeLike-RJBS-20090219/
be more like RJBS -- use the modules he likes!
----
Test-Harness-3.16
http://search.cpan.org/~andya/Test-Harness-3.16/
Run Perl standard test scripts with statistics
----
Test-XML-Deep-0.01
http://search.cpan.org/~jlavallee/Test-XML-Deep-0.01/
= XML::Simple + Test::Deep
----
Test-XML-Deep-0.02
http://search.cpan.org/~jlavallee/Test-XML-Deep-0.02/
= XML::Simple + Test::Deep
----
Tie-Proxy-Changes-0.2
http://search.cpan.org/~maluku/Tie-Proxy-Changes-0.2/
Track changes in your tied objects
----
Variable-Magic-0.31
http://search.cpan.org/~vpit/Variable-Magic-0.31/
Associate user-defined magic to variables from Perl.
----
WWW-Hanako-0.01
http://search.cpan.org/~hamano/WWW-Hanako-0.01/
Perl interface for Hanako(Pollen observation system at Japan)
----
Win32-WindowsUpdate-0.04
http://search.cpan.org/~wilsond/Win32-WindowsUpdate-0.04/
Access to Windows Update functions
----
mysubs-1.00
http://search.cpan.org/~chocolate/mysubs-1.00/
lexical subroutines
----
mysubs-1.01
http://search.cpan.org/~chocolate/mysubs-1.01/
lexical subroutines
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: Thu, 19 Feb 2009 18:49:43 -0700
From: Larry Gates <larry@example.invalid>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <89qw3w9vdqnr$.4ylg2oiupv8b$.dlg@40tude.net>
On Tue, 17 Feb 2009 08:16:22 GMT, tadmc@seesig.invalid wrote:
> Search a Usenet archive
> There are tens of thousands of Perl programmers. It is very likely
> that your question has already been asked (and answered). See if you
> can find where it has already been answered.
>
> One such searchable archive is:
>
> http://groups.google.com/advanced_search
I'm curious why this fails:
#!/usr/bin/perl
# url http://groups.google.com/advanced_search
use warnings;
use strict;
use HTML::TreeBuilder;
use LWP::Simple;
my $site_url = 'http://groups.google.com';
my $url_args = 'advanced_search';
my $t = get "$site_url?$url_args" || "Problem";
my $tree = HTML::TreeBuilder->new_from_content($t);
$tree->dump;
# perl goog1.pl
C:\MinGW\source>perl goog1.pl
<html> @0 (IMPLICIT)
<head> @0.0 (IMPLICIT)
<body> @0.1 (IMPLICIT)
"Problem"
C:\MinGW\source>
--
larry gates
And don't tell me there isn't one bit of difference between null and space,
because that's exactly how much difference there is. :-)
-- Larry Wall in <10209@jpl-devvax.JPL.NASA.GOV>
------------------------------
Date: Thu, 19 Feb 2009 19:49:14 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <slrngps30q.238.tadmc@tadmc30.sbcglobal.net>
Larry Gates <larry@example.invalid> wrote:
> I'm curious why this fails:
>
> #!/usr/bin/perl
> # url http://groups.google.com/advanced_search
^
^ slash, not question mark
> my $site_url = 'http://groups.google.com';
> my $url_args = 'advanced_search';
my $url = "$site_url?$url_args";
print "URL=$url\n";
> my $t = get "$site_url?$url_args" || "Problem";
^
^
There is no question mark in the correct URL...
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 19 Feb 2009 18:01:28 -0800
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
Message-Id: <Y9onl.21824$nu6.20472@newsfe21.iad>
Larry Gates wrote:
> On Tue, 17 Feb 2009 08:16:22 GMT, tadmc@seesig.invalid wrote:
>
>> Search a Usenet archive
>> There are tens of thousands of Perl programmers. It is very
>> likely that your question has already been asked (and
>> answered). See if you can find where it has already been
>> answered.
>>
>> One such searchable archive is:
>>
>> http://groups.google.com/advanced_search
>
> I'm curious why this fails:
>
> #!/usr/bin/perl
> # url http://groups.google.com/advanced_search
>
> use warnings;
> use strict;
> use HTML::TreeBuilder;
> use LWP::Simple;
>
> my $site_url = 'http://groups.google.com';
> my $url_args = 'advanced_search';
> my $t = get "$site_url?$url_args" || "Problem";
>
> my $tree = HTML::TreeBuilder->new_from_content($t);
>
> $tree->dump;
>
> # perl goog1.pl
>
> C:\MinGW\source>perl goog1.pl
> <html> @0 (IMPLICIT)
> <head> @0.0 (IMPLICIT)
> <body> @0.1 (IMPLICIT)
> "Problem"
>
> C:\MinGW\source>
There's a great difference between
http://groups.google.com/advanced_search and
http://groups.google.com?advanced_search
You meant
my $site_url = 'http://groups.google.com';
my $url_args = 'advanced_search';
my $t = get "$site_url/$url_args" || "Problem";
or just:
my $t = get "http://groups.google.com/advanced_search" || "Problem";
See, also:
- Carefully choose the contents of your Subject header
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
------------------------------
Date: Fri, 20 Feb 2009 00:13:39 GMT
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Read/write with UCS-2* encodings - Possible???
Message-Id: <slrngprtdj.hdj.nospam-abuse@chorin.math.berkeley.edu>
On 2009-02-18, Ben Morrow <ben@morrow.me.uk> wrote:
>> > I must admit I've never had anything to do with creating Win32-format
>> > text files. Generally the extent of my knowledge of such things is 'how
>> > do I get this wretched OS to stop messing up my files?' :).
>>
>> This is one of the sides of the problem. *This* part can be fixed if
>> :encoding(UCS2) would just remove :crlf flag.
> Yes, but I don't think this will change either.
Sorry, can't parse your sentence... (Removing :crlf has no bad side
effects, and a lot of good ones [like some programs suddenly starting
to produce non-junk ;-].)
>> By definition, piconv does not work with binary files...
> Yes it does.
If you think so, your mental picture of text/vs/binary is IMO
seriously screwed up... Not surprising if you claim little experience
with DOSISH systems.
> 'Text' as used by perl means 'some 8bit extension of ASCII,
> with reasonably short lines delimited with the OS newline sequence, no
> non-spacing control characters and no NULs'.
Nope, `text' means non-`binary'. And `binary' means that preservation
of the exact layout of bits is tantamount. So changing encoding on
binary data is a nonono.
> By this definition UCS-2 is
> (and most wide encodings are) 'binary'. -B will return true on a UCS-2
> file, for instance.
-B makes no sense at all in today's world. Anyway, even when it had,
it has no relation to :crlf etc.
>> > While this is certainly possible, I somewhat doubt the behaviour of
>> > :encoding will be changed now.
>>
>> Given that its current behaviour makes no sense, why not?
> Because perl 5.8 was released a good while ago, so people have been
> working with and around the current behaviour for some time. Changing it
> now would almost certainly break working code,
I claim that removing :crlf can't break any code. I suspect that
reinserting it on top of :encoding(UCS2) would also have no
detrimental side effects, but to check this, one needs more knowledge
about (IMO, completely botched) behaviour of PerlIO.
In short, if "remove :crlf" worked AFTER "insert :encoding(UCS2)", one
must make sure that it still does. (Well, given the current pitiful
state of Perl, it might be that just `applying voodoo programing' may be
a sufficient justification - just do several experiments on how Perl
behaves without any inspection of source code...)
> whereas a new layer on CPAN would not,
... and would not fix thousands of programs which do not work now...
> and would allow those with old perls to get the new behaviour if
> they want it.
This does not make any sense to me. The problem is not with "those
who want it" (there are too few of them), but with "those who need it"
(read: anyone working on DOSISH, or writing code which can potentially
be used on DOSISH platforms).
> It's possible that some sort of flag to :encoding (or to 'use
> PerlIO::encoding') would be OK.
No. What is OK is to have correct behaviour. If having correct
behaviour has a non-0 (but still negligible) chance of breaking old
stuff, one should be able to request bug-for-bug compatibility by
environment variable.
Yours,
Ilya
------------------------------
Date: Thu, 19 Feb 2009 14:11:30 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Sorting based on existence of keys
Message-Id: <x7iqn6w8bh.fsf@mail.sysarch.com>
>>>>> "JG" == Jim Gibson <jimsgibson@gmail.com> writes:
JG> my @ks = qw(two three one four);
JG> my %h = ( one => 1, three => 333 );
JG> my @sorted = (
JG> (sort {length $h{$a} <=> length $h{$b}} grep {exists $h{$_}} @ks),
JG> (grep {!exists $h{$_}} @ks)
JG> );
all this confusing redundant code is annoying me. i left an exercise on
doing this all one time but it seems i will have to answer it myself. i
will assume length of a key will always be less than some large number
which is how missing keys will sort late. this is an untested ST but
Sort::Maker can generate this for you too.
@sorted = map $_->[0],
sort { $a->[1] <=> $b->[1] }
map [ $_, exists($h{$_}) ? length $h{$_} : 9999999 ],
@unsorted ;
there is only one call to exists and length and it is much clearer what
you mean to do. and Sort::Maker cleans up all the cruft and you only
need the exists expression.
the point is that you want to do key extraction/processing only one time
both for speed and for clarity. perl6's sort supports this style and
drops the $a/$b redundant stuff in general.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 19 Feb 2009 21:48:37 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Sorting based on existence of keys
Message-Id: <slrngprhd5.h0t.hjp-usenet2@hrunkner.hjp.at>
On 2009-02-19 19:11, Uri Guttman <uri@stemsystems.com> wrote:
> i will assume length of a key will always be less than some large
> number which is how missing keys will sort late. this is an untested
> ST but Sort::Maker can generate this for you too.
>
> @sorted = map $_->[0],
> sort { $a->[1] <=> $b->[1] }
> map [ $_, exists($h{$_}) ? length $h{$_} : 9999999 ],
> @unsorted ;
'Inf' is always larger than any real number, so you don't have to choose
an arbitrary large number.
hp
------------------------------
Date: Thu, 19 Feb 2009 16:24:41 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Sorting based on existence of keys
Message-Id: <x7r61uunl2.fsf@mail.sysarch.com>
>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:
PJH> On 2009-02-19 19:11, Uri Guttman <uri@stemsystems.com> wrote:
>> i will assume length of a key will always be less than some large
>> number which is how missing keys will sort late. this is an untested
>> ST but Sort::Maker can generate this for you too.
>>
>> @sorted = map $_->[0],
>> sort { $a->[1] <=> $b->[1] }
>> map [ $_, exists($h{$_}) ? length $h{$_} : 9999999 ],
>> @unsorted ;
PJH> 'Inf' is always larger than any real number, so you don't have to choose
PJH> an arbitrary large number.
i wanted to keep it an integer. in Sort::Maker that makes a real
difference - comparing 4 (or 8) byte ints vs 8 byte floats. maybe with
64 bit ints it doesn't matter, but you do need to specify integer v
float if you use the GRT style of sorting. the other styles use <=> so
it doesn't matter and Inf would work. and i don't think i have ever seen
a proper need for Inf before this. :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 19 Feb 2009 22:25:33 GMT
From: sln@netherlands.com
Subject: Re: Sorting based on existence of keys
Message-Id: <dsmrp4la08hou1bb784n0hbf6j1uqmcpn5@4ax.com>
On Thu, 19 Feb 2009 14:28:16 +0100, Rasmus Villemoes <burner+usenet@imf.au.dk> wrote:
>Hi experts
>
>Say I have an array @ks containing some strings which may or may not
>exist as keys in a hash %h. How do I sort @ks such that the
>non-existent keys come last (in any order), while the existing keys
>are sorted using, say, length($h{$a}) <=> length($h{$a}) (that is not
>really the property of the values I'll sort by, but that's not
>important).
>
>my @ks = qw(two three one four);
>my %h = ( one => 1,
> three => 333 );
>
>for (sort { exists $h{$b} cmp exists $h{$a} ||
> length $h{$a} <=> length $h{$b}} @ks) { print "$_\n" }
>
>
>This produces the intended result, but under "use warnings;" one also
>gets the expected warnings when $h{two} and $h{four} are compared in
>the second line. Is there some smarter way to do this? (Also, I don't
>know if cmp is the right tool for comparing boolean values).
I think cmp AND <=> were exactly the right tools.
This should work for you.
Thanks for posting this!
-sln
-----------------------------------------
## sort_junk.pl
## -sln
##
use strict;
use warnings;
my @ks = qw(two three one four);
my %h = ( one => 1,
three => 333 );
## Could probably take advantage of an apparent bug/feature that exists() returns stringified '1' for true and '' for false.
## As in $vvv = ''; print "not '$vvv'\n" if (!$vvv);
print "\n1: Your equivalent -----\n";
for (sort {
my $Ea = exists $h{$a};
my $Eb = exists $h{$b};
my $comp = $Eb cmp $Ea;
print "a=$a , b=$b , Ea='$Ea'(".length($Ea)."), Eb='$Eb'(".length($Eb)."), cmp=$comp\n";
$Ea != $Eb ?
$Eb cmp $Ea :
length $h{$a} <=> length $h{$b}
} @ks)
{ print "$_\n" }
print "\n2: Your new -------\n";
for (sort {
my ($xb, $xa) = (exists $h{$b}, exists $h{$a});
!($xb & $xa) ? $xb cmp $xa : length $h{$a} <=> length $h{$b}
} @ks)
{ print "$_\n" }
__END__
output:
1: Your equivalent -----
a=two , b=three , Ea=''(0), Eb='1'(1), cmp=1
a=one , b=four , Ea='1'(1), Eb=''(0), cmp=-1
a=three , b=one , Ea='1'(1), Eb='1'(1), cmp=0
a=three , b=four , Ea='1'(1), Eb=''(0), cmp=-1
a=two , b=four , Ea=''(0), Eb=''(0), cmp=0
Use of uninitialized value in length at sort_junk.pl line 21.
Use of uninitialized value in length at sort_junk.pl line 21.
one
three
two
four
2: Your new -------
one
three
two
four
------------------------------
Date: Fri, 20 Feb 2009 00:51:55 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Sorting based on existence of keys
Message-Id: <slrngprolp.fsh.whynot@orphan.zombinet>
On 2009-02-19, Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
>
> JE> Rasmus Villemoes <burner+usenet@imf.au.dk> wrote:
> >> Say I have an array @ks containing some strings which may or may not
> >> exist as keys in a hash %h. How do I sort @ks such that the
> >> non-existent keys come last (in any order), while the existing keys
> >> are sorted using, say, length($h{$a}) <=> length($h{$a}) (that is not
> >> really the property of the values I'll sort by, but that's not
> >> important).
>
> JE> There are four different cases you have to consider in your custom
> JE> compare function:
>
> JE> $h{$a} and $h{$b} exist ===> length($h{$a}) <=> length($h{$a})
> JE> $h{$a} exists but $h{$b} doesn't ===> -1
> JE> $h{$a} does't exist but $h{$b} does ===> 1
> JE> Neither $h{$a} nor $h{$b} exists ===> 0
>
> this is why doing a prefilter on the sort keys makes life much
> simpler. then you only have 1 real case to worry about - whether a key
> exists or not. you can replace a non-existant key with a fake one that
> always sorts higher than normal keys. this can be done in an ST or in
> the key extraction code in Sort::Maker. i leave how to do that as an
> exercise but it is much simpler than all the double sided checking you
> propose.
I beg to differ. Jurgen is right that OP must get realtional and
equality operators and boolean algebra straight first (as if mine is).
Fish (untested, subject to precednce issues)
sort {
(exists $h{$a} xor exists $h{$b}) ?
exists $h{$b} - exists $h{$a} :
0
} @ks
--
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom
------------------------------
Date: Fri, 20 Feb 2009 00:34:20 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Sorting based on existence of keys
Message-Id: <x7ljs1u0wz.fsf@mail.sysarch.com>
>>>>> "EP" == Eric Pozharski <whynot@pozharski.name> writes:
EP> On 2009-02-19, Uri Guttman <uri@stemsystems.com> wrote:
>>
JE> $h{$a} and $h{$b} exist ===> length($h{$a}) <=> length($h{$a})
JE> $h{$a} exists but $h{$b} doesn't ===> -1
JE> $h{$a} does't exist but $h{$b} does ===> 1
JE> Neither $h{$a} nor $h{$b} exists ===> 0
>>
>> this is why doing a prefilter on the sort keys makes life much
>> simpler. then you only have 1 real case to worry about - whether a key
>> exists or not. you can replace a non-existant key with a fake one that
>> always sorts higher than normal keys. this can be done in an ST or in
>> the key extraction code in Sort::Maker. i leave how to do that as an
>> exercise but it is much simpler than all the double sided checking you
>> propose.
EP> I beg to differ. Jurgen is right that OP must get realtional and
EP> equality operators and boolean algebra straight first (as if mine is).
EP> sort {
EP> (exists $h{$a} xor exists $h{$b}) ?
EP> exists $h{$b} - exists $h{$a} :
EP> 0
EP> } @ks
and where does that sort the lengths? even though it isn't a real
requirement we all seem to have been using it. when you add that back
you get much more complicated comparisons. i haven't even brought up
speed for which the presort key extraction is needed. see my other post
for an example which should work if i typed it cleanly and is simpler,
clearer and faster.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
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 V11 Issue 2222
***************************************