[31031] in Perl-Users-Digest
Perl-Users Digest, Issue: 2276 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 15 03:10:05 2009
Date: Sun, 15 Mar 2009 00:09:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 15 Mar 2009 Volume: 11 Number: 2276
Today's topics:
Finding gateway address <XXjbhuntxx@white-star.com>
Re: Finding gateway address <ben@morrow.me.uk>
Re: Finding gateway address <XXjbhuntxx@white-star.com>
Re: How to set the message displayed when a Perl Script <1usa@llenroc.ude.invalid>
How to set the message displayed when a Perl Script fai <nigel@bouteyres.com>
Re: Most efficient way to do set-comparison sln@netherlands.com
Re: Most efficient way to do set-comparison sln@netherlands.com
new CPAN modules on Sun Mar 15 2009 (Randal Schwartz)
Re: perl as email client <waveright@gmail.com>
Re: regex for finding jumbled words <ced@blv-sam-01.ca.boeing.com>
Re: regex for finding jumbled words sln@netherlands.com
Re: regex for finding jumbled words <ced@blv-sam-01.ca.boeing.com>
Re: regex for finding jumbled words <1usa@llenroc.ude.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 14 Mar 2009 23:17:23 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Finding gateway address
Message-Id: <Xns9BCEA63A527C3ccruizermydejacom@207.115.17.102>
I'm writing a subroutine that will derive the gateway address for a
specified IP address and subnet mask.
I started writing it using elsif for each subnet mask with nested if
statements, but the code is getting very long due to the number of possible
subnet masks.
Question: Does anybody know of a module or cleanly written code segment
that will take the subnet mask and IP address, then return the gateway
address? (Reinventing the wheel is great for practice, but it is
devistating on productivity.)
Thanks.
Cos
------------------------------
Date: Sun, 15 Mar 2009 00:59:30 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Finding gateway address
Message-Id: <ig0u86-omd.ln1@osiris.mauzo.dyndns.org>
Quoth Cosmic Cruizer <XXjbhuntxx@white-star.com>:
> I'm writing a subroutine that will derive the gateway address for a
> specified IP address and subnet mask.
>
> I started writing it using elsif for each subnet mask with nested if
> statements, but the code is getting very long due to the number of possible
> subnet masks.
>
> Question: Does anybody know of a module or cleanly written code segment
> that will take the subnet mask and IP address, then return the gateway
> address? (Reinventing the wheel is great for practice, but it is
> devistating on productivity.)
How are you determining the gateway address for a given network? Which
machine is gateway is a matter of local network policy: there is no way
to determine it numerically in general. Presumably your network has some
rule like 'the gateway is always the .1 address on the network' but we
have no way of knowing what.
Finding the network number from an address and mask is easy:
my ($paddr, $pmask) = map {
pack "C4", split /\./
} $addr, $mask;
my $pnet = $paddr & $pmask;
my $net = join ".", unpack "C4", $pnet;
If you need to manipulate ip addreses as numbers, you will find the "N"
pack/unpack format useful as well; for instance, to find the .1 address
on the above network you can use something like
my $gw =
join ".",
unpack "C4",
pack "N",
1 + unpack "N",
$pnet;
It's important here not to fall into the trap of using inet_aton and the
associated functions from Socket.pm. They produce address packed
suitably for passing to your system's socket functions, and the exact
format is system-dependant.
Ben
------------------------------
Date: Sun, 15 Mar 2009 02:03:30 GMT
From: Cosmic Cruizer <XXjbhuntxx@white-star.com>
Subject: Re: Finding gateway address
Message-Id: <Xns9BCEC1DC3C4DFccruizermydejacom@207.115.17.102>
Ben Morrow <ben@morrow.me.uk> wrote in
news:ig0u86-omd.ln1@osiris.mauzo.dyndns.org:
> How are you determining the gateway address for a given network? Which
> machine is gateway is a matter of local network policy: there is no
> way to determine it numerically in general. Presumably your network
> has some rule like 'the gateway is always the .1 address on the
> network' but we have no way of knowing what.
>
> Finding the network number from an address and mask is easy:
>
> my ($paddr, $pmask) = map {
> pack "C4", split /\./
> } $addr, $mask;
>
> my $pnet = $paddr & $pmask;
>
> my $net = join ".", unpack "C4", $pnet;
>
> If you need to manipulate ip addreses as numbers, you will find the
> "N" pack/unpack format useful as well; for instance, to find the .1
> address on the above network you can use something like
>
> my $gw =
> join ".",
> unpack "C4",
> pack "N",
> 1 + unpack "N",
> $pnet;
>
> It's important here not to fall into the trap of using inet_aton and
> the associated functions from Socket.pm. They produce address packed
> suitably for passing to your system's socket functions, and the exact
> format is system-dependant.
>
> Ben
>
Ben,
That is far simplier then the 60+ lines I wrote. They both give the same
results, but your code is much cleaner, efficient, and scalable.
As you mentioned, our policy is to use the .1 address on the network.
Therefore, $gw is the targetted value.
Thank you very much!
...Cos
------------------------------
Date: Sun, 15 Mar 2009 01:37:12 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to set the message displayed when a Perl Script fails compilation?
Message-Id: <Xns9BCEDBEDBF640asu1cornelledu@127.0.0.1>
Nigel <nigel@bouteyres.com> wrote in news:85b78e80-10b5-480f-a83f-
bcb571f7678a@w34g2000yqm.googlegroups.com:
> Ideally I'd like to handle the failed compilation in some way that
> would allow me to display a friendly page to my visitor and send me an
> email to alert me to the problem.
>
> Has anyone got any ideas?
Maybe you should read the documentation for the HTTP server you are using.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Sat, 14 Mar 2009 18:32:08 -0700 (PDT)
From: Nigel <nigel@bouteyres.com>
Subject: How to set the message displayed when a Perl Script fails compilation?
Message-Id: <85b78e80-10b5-480f-a83f-bcb571f7678a@w34g2000yqm.googlegroups.com>
Hi all,
I'm sure this is glaringly obvious to those in the know, but my
efforts to find the answer have failed.
My problem is with the message that is displayed in a web browser when
a script fails compilation. At the moment I'm getting this:
For help, please send mail to the webmaster ([no address given]),
giving this error message and the time and date of the error.
At least, I'd like to know how to set the email address for the
webmaster instead of no address given.
Ideally I'd like to handle the failed compilation in some way that
would allow me to display a friendly page to my visitor and send me an
email to alert me to the problem.
Has anyone got any ideas? Apart that is from not uploading scripts
that don't compile! ;-)
Thanks in advance,
Nigel
------------------------------
Date: Sat, 14 Mar 2009 21:10:26 GMT
From: sln@netherlands.com
Subject: Re: Most efficient way to do set-comparison
Message-Id: <nj6or4tm63v61g35k8fakis89okbfis1rk@4ax.com>
On Fri, 13 Mar 2009 06:05:56 -0700 (PDT), Krishna Chaitanya <schaitan@gmail.com> wrote:
>Hi all,
>
>If set A defined elements a,b,c (a set of permissible program options,
>let's say), and we receive from user options in set B.
>
>I want to make sure that user options in B are exactly same as those
>defined in A .... nothing more, nothing less and completely
>identical....this represents some kinda set operation if I'm not
>wrong...
>
>A U B = A
>A int B = A
>
>How to do this most efficiently in Perl...?
You seem (in another post under this thread) to want more than just intersection
and equality. There is no real efficient way to do this under any language, but
if you want to report over/under relationships, this may help.
This is one approach. There may be more generalization that could be done,
perhaps combining all the mappings into one array. Certainly though, there
is no one-liner hiding for this problem.
-sln
-----------------------------------------------------------------------------
## options2.pl
##
use strict;
use warnings;
my $Debug = 1;
my %legalopts = (a => 1, b => 1, c => 1, d => 1);
my %useropts = (a => 1, c => 1, e => 1, f => 1, g => 1);
#my %useropts = (a => 1, b => 1, c => 1, d => 1);
sub mapOptions {
my ($legal, $user) = @_;
my %all = ();
@all { keys %{$legal} } = ( 1 ) x ( keys %{$legal} );
@all { keys %{$user} } = map { !defined $_ ? 2 : 3 } @all { keys %{$user} };
\%all;
}
sub getOptMap {
my ($options, $mask) = @_;
return map { $options->{$_} == $mask ? $_ : () } sort keys %{$options};
}
##
my $all = mapOptions ( \%legalopts, \%useropts );
my @legalonly = getOptMap ( $all, 1 );
my @useronly = getOptMap ( $all, 2 );
my @common = getOptMap ( $all, 3 );
##
if ( $Debug ) {
print "\nDebug:\n";
print "All - ",( sort keys %{$all} ),"\n";
print "Legal missing/non-matching - ",@legalonly,", elements = ", scalar(@legalonly), "\n";
print "User too many/non-matching - ",@useronly,", elements = ", scalar(@useronly), "\n";
print "Common matching - ",@common,", elements = ", scalar(@common), "\n";
}
##
if ( @legalonly || @useronly ) {
print "\nError:\n";
print "Legal options are '",( sort keys %legalopts ),"'\n";
print "Missing options '",@legalonly,"'\n" if (@legalonly);
print "Non-legal options '",@useronly,"'\n" if (@useronly);
} else {
print "\nSucessfully enterred legal options '",( sort keys %useropts ),"' !\n"
}
__END__
output:
-------------
Debug:
All - abcdefg
Legal missing/non-matching - bd, elements = 2
User too many/non-matching - efg, elements = 3
Common matching - ac, elements = 2
Error:
Legal options are 'abcd'
Missing options 'bd'
Non-legal options 'efg'
------------------------------
Date: Sat, 14 Mar 2009 21:19:07 GMT
From: sln@netherlands.com
Subject: Re: Most efficient way to do set-comparison
Message-Id: <km7or4d8g26aqf57fp0vqopoi4bjtsoib4@4ax.com>
On Sat, 14 Mar 2009 21:10:26 GMT, sln@netherlands.com wrote:
>On Fri, 13 Mar 2009 06:05:56 -0700 (PDT), Krishna Chaitanya <schaitan@gmail.com> wrote:
>
[snip]
>sub getOptMap {
> my ($options, $mask) = @_;
> return map { $options->{$_} == $mask ? $_ : () } sort keys %{$options};
^^^^^^
or, no need for return statement, the map ... is just fine
-sln
------------------------------
Date: Sun, 15 Mar 2009 04:42:26 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Sun Mar 15 2009
Message-Id: <KGJ6Eq.1uJB@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.
App-Framework-0.07
http://search.cpan.org/~sdprice/App-Framework-0.07/
A framework for creating applications
----
App-ZofCMS-Plugin-FileTypeIcon-0.0103
http://search.cpan.org/~zoffix/App-ZofCMS-Plugin-FileTypeIcon-0.0103/
present users with pretty icons depending on file type
----
App-ZofCMS-Plugin-Search-Indexer-0.0102
http://search.cpan.org/~zoffix/App-ZofCMS-Plugin-Search-Indexer-0.0102/
plugin that incorporates Search::Indexer module's functionality
----
Bio-Graphics-1.86
http://search.cpan.org/~lds/Bio-Graphics-1.86/
Generate GD images of Bio::Seq objects
----
Bio-Graphics-1.87
http://search.cpan.org/~lds/Bio-Graphics-1.87/
Generate GD images of Bio::Seq objects
----
Crypt-OTP26-0.03
http://search.cpan.org/~osfameron/Crypt-OTP26-0.03/
a classic form of encryption
----
DBD-Google-0.50
http://search.cpan.org/~darren/DBD-Google-0.50/
Treat Google as a datasource for DBI
----
FLV-Info-0.24
http://search.cpan.org/~cdolan/FLV-Info-0.24/
Extract metadata from Adobe Flash Video files
----
Filter-Arguments-0.03
http://search.cpan.org/~dylan/Filter-Arguments-0.03/
Configure and read your command line arguments from @ARGV.
----
FreeBSD-i386-Ptrace-0.01
http://search.cpan.org/~dankogai/FreeBSD-i386-Ptrace-0.01/
Ptrace for FreeBSD-i386
----
GitStore-0.01
http://search.cpan.org/~fayland/GitStore-0.01/
Git as versioned data store in Perl
----
GitStore-0.02
http://search.cpan.org/~fayland/GitStore-0.02/
Git as versioned data store in Perl
----
GitStore-0.03
http://search.cpan.org/~fayland/GitStore-0.03/
Git as versioned data store in Perl
----
IPC-SysV-2.01
http://search.cpan.org/~mhx/IPC-SysV-2.01/
System V IPC constants and system calls
----
Jcode-CP932-0.05
http://search.cpan.org/~asakura/Jcode-CP932-0.05/
----
Jifty-Plugin-Gravatar-0.01
http://search.cpan.org/~cornelius/Jifty-Plugin-Gravatar-0.01/
Jifty Plugin for Gravatar Icon Service
----
Mail-Chimp-0.1
http://search.cpan.org/~dpirotte/Mail-Chimp-0.1/
Perl wrapper around the Mailchimp v1.1 API
----
Module-Install-ProvidesClass-0.000001_01
http://search.cpan.org/~ash/Module-Install-ProvidesClass-0.000001_01/
provides detection in META.yml for 'class' keyword
----
Muldis-D-0.61.0
http://search.cpan.org/~duncand/Muldis-D-0.61.0/
Formal spec of Muldis D relational DBMS lang
----
NetHack-Item-0.08
http://search.cpan.org/~sartak/NetHack-Item-0.08/
parse and interact with a NetHack item
----
PerlCryptLib-1.08
http://search.cpan.org/~alvarol/PerlCryptLib-1.08/
Perl interface to Peter Guttman's cryptlib API
----
Proc-Branch-0.01
http://search.cpan.org/~isjoung/Proc-Branch-0.01/
Creating Multiple Child Processes and Merging
----
RDF-Trine-0.110
http://search.cpan.org/~gwilliams/RDF-Trine-0.110/
An RDF Framework for Perl.
----
Scalar-String-0.000
http://search.cpan.org/~zefram/Scalar-String-0.000/
string aspects of scalars
----
Simo-0.1006
http://search.cpan.org/~kimoto/Simo-0.1006/
Very simple framework for Object Oriented Perl.
----
Simo-Wrapper-0.0217
http://search.cpan.org/~kimoto/Simo-Wrapper-0.0217/
Wrapper class to manipulate object.
----
Simo-Wrapper-0.0218
http://search.cpan.org/~kimoto/Simo-Wrapper-0.0218/
Wrapper class to manipulate object.
----
Simo-Wrapper-0.0219
http://search.cpan.org/~kimoto/Simo-Wrapper-0.0219/
Wrapper class to manipulate object.
----
Socket-Class-1.99_11
http://search.cpan.org/~chrmue/Socket-Class-1.99_11/
A class to communicate with sockets
----
Socket-Class-SSL-0.99_01
http://search.cpan.org/~chrmue/Socket-Class-SSL-0.99_01/
SSL support for Socket::Class
----
Sys-Info-0.69_05
http://search.cpan.org/~burak/Sys-Info-0.69_05/
Fetch information from the host system
----
Sys-Info-0.69_06
http://search.cpan.org/~burak/Sys-Info-0.69_06/
Fetch information from the host system
----
Sys-Info-0.69_07
http://search.cpan.org/~burak/Sys-Info-0.69_07/
Fetch information from the host system
----
Sys-Info-Base-0.69_05
http://search.cpan.org/~burak/Sys-Info-Base-0.69_05/
Base class for Sys::Info
----
Sys-Info-Base-0.69_06
http://search.cpan.org/~burak/Sys-Info-Base-0.69_06/
Base class for Sys::Info
----
Sys-Info-Driver-BSD-0.69_07
http://search.cpan.org/~burak/Sys-Info-Driver-BSD-0.69_07/
BSD driver for Sys::Info
----
Sys-Info-Driver-Linux-0.69_05
http://search.cpan.org/~burak/Sys-Info-Driver-Linux-0.69_05/
Linux driver for Sys::Info
----
Sys-Info-Driver-Linux-0.69_06
http://search.cpan.org/~burak/Sys-Info-Driver-Linux-0.69_06/
Linux driver for Sys::Info
----
Sys-Info-Driver-Unknown-0.69_05
http://search.cpan.org/~burak/Sys-Info-Driver-Unknown-0.69_05/
Compatibility layer for Sys::Info
----
Sys-Info-Driver-Unknown-0.69_06
http://search.cpan.org/~burak/Sys-Info-Driver-Unknown-0.69_06/
Compatibility layer for Sys::Info
----
Sys-Info-Driver-Unknown-0.69_07
http://search.cpan.org/~burak/Sys-Info-Driver-Unknown-0.69_07/
Compatibility layer for Sys::Info
----
Sys-Info-Driver-Windows-0.69_05
http://search.cpan.org/~burak/Sys-Info-Driver-Windows-0.69_05/
Windows driver for Sys::Info
----
Sys-Info-Driver-Windows-0.69_06
http://search.cpan.org/~burak/Sys-Info-Driver-Windows-0.69_06/
Windows driver for Sys::Info
----
Sys-Syslog-1.00
http://search.cpan.org/~maxschube/Sys-Syslog-1.00/
----
Sys-Syslog-OO-1.00
http://search.cpan.org/~maxschube/Sys-Syslog-OO-1.00/
Thin object-oriented wrapper around Sys::Syslog::OO
----
TAEB-0.03
http://search.cpan.org/~sartak/TAEB-0.03/
the Tactical Amulet Extraction Bot (for NetHack)
----
Test-Sys-Info-0.11
http://search.cpan.org/~burak/Test-Sys-Info-0.11/
Centralized test suite for Sys::Info.
----
Test-Sys-Info-0.12
http://search.cpan.org/~burak/Test-Sys-Info-0.12/
Centralized test suite for Sys::Info.
----
Test-Weaken-2.002000
http://search.cpan.org/~jkegl/Test-Weaken-2.002000/
Test that freed memory objects were, indeed, freed
----
Text-Chord-Piano-0.0.5
http://search.cpan.org/~bayashi/Text-Chord-Piano-0.0.5/
This module is a chord table generator of Piano by the text
----
Text-xSV-0.18
http://search.cpan.org/~tilly/Text-xSV-0.18/
read character separated files
----
XHTML-Util-0.01
http://search.cpan.org/~ashley/XHTML-Util-0.01/
(alpha software) powerful utilities for common but difficult to nail HTML munging.
----
XHTML-Util-0.02
http://search.cpan.org/~ashley/XHTML-Util-0.02/
(alpha software) powerful utilities for common but difficult to nail HTML munging.
----
XHTML-Util-0.03
http://search.cpan.org/~ashley/XHTML-Util-0.03/
(alpha software) powerful utilities for common but difficult to nail HTML munging.
----
forks-0.31
http://search.cpan.org/~rybskej/forks-0.31/
drop-in replacement for Perl threads using fork()
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: Sat, 14 Mar 2009 14:34:49 -0700 (PDT)
From: Todd Wade <waveright@gmail.com>
Subject: Re: perl as email client
Message-Id: <bec5bb32-07f0-4349-837a-2b72f7c5c197@v39g2000yqm.googlegroups.com>
On Mar 7, 1:31 am, Larry Gates <la...@example.invalid> wrote:
>
> Gosh, I would have thought that writing an e-mail client in perl would be
> as commonplace as ways to calculate pi with fortran.
Great! Because it is. Your OP dosen't give enough information to tell
us what it is that you want to do (generate a .csv file based on
emails in an IMAP folder? Call the police when your security system
emails you that someone has broke in?) so you are getting very little
feedback and the feedback you are getting is very general.
I would have thought that someone posting general questions in a
technical newsgroup wouldn't be surprised when they get general
responses :0)
trwww
------------------------------
Date: Sat, 14 Mar 2009 13:16:37 -0700 (PDT)
From: "C.DeRykuks" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: regex for finding jumbled words
Message-Id: <5e9cf87b-bb56-48f2-a1fd-ed930523f185@b38g2000prf.googlegroups.com>
On Mar 11, 2:12=A0pm, luser-ex-troll <mijo...@yahoo.com> wrote:
> On Mar 11, 1:00=A0pm, sso <strongsilent...@gmail.com> wrote:
>
> > Hi, I need help figuring out the regex that would find if a word can
> > be made from another word.
> > For example
>
> > apple =A0could make =A0pal, lap, leap
> > it could not make all or peel
>
> > Suggestions?
>
> Regex might not be the best strategy here.
> I'd try counting the letters into a hash,
> keyed by letter. Then you can generate a similar
> hash for words to test and loop through the keys
> to check if 'apple' has the right number of each
> letter needed to make the word in question.
>
> This could be very slow.
Another regex possibility:
my $src =3D 'apple';
my $src_sort =3D join '', sort split //, $src;
my @targets =3D qw( pal lap leap all peel );
for my $target ( @targets ) {
my $target_re =3D join '.*?',
sort split //,$target;
printf( "'%s' %s be made from '%s'\n\n",
$target,
$src_sort =3D~ /$target_re/
? 'can' : 'cannot', $src
);
}
--
Charles DeRykus
------------------------------
Date: Sat, 14 Mar 2009 21:29:25 GMT
From: sln@netherlands.com
Subject: Re: regex for finding jumbled words
Message-Id: <768or4p22l6ovslge7ohh6kq26sqgmoij0@4ax.com>
On Sat, 14 Mar 2009 13:16:37 -0700 (PDT), "C.DeRykuks" <ced@blv-sam-01.ca.boeing.com> wrote:
>On Mar 11, 2:12 pm, luser-ex-troll <mijo...@yahoo.com> wrote:
>> On Mar 11, 1:00 pm, sso <strongsilent...@gmail.com> wrote:
>>
>> > Hi, I need help figuring out the regex that would find if a word can
>> > be made from another word.
>> > For example
>>
>> > apple could make pal, lap, leap
>> > it could not make all or peel
>>
>> > Suggestions?
>>
>> Regex might not be the best strategy here.
>> I'd try counting the letters into a hash,
>> keyed by letter. Then you can generate a similar
>> hash for words to test and loop through the keys
>> to check if 'apple' has the right number of each
>> letter needed to make the word in question.
>>
>> This could be very slow.
>
>
>Another regex possibility:
>
>my $src = 'apple';
>my $src_sort = join '', sort split //, $src;
>my @targets = qw( pal lap leap all peel );
^^^^^^^^
What is this? The example is ficticious. It has nothing
to do with a solution, its just an example. Throw away the
example, then answer the OP's question.
-sln
------------------------------
Date: Sat, 14 Mar 2009 17:15:28 -0700 (PDT)
From: "C.DeRykuks" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: regex for finding jumbled words
Message-Id: <a4107769-8b0e-40c1-a789-df8844868b8a@k19g2000prh.googlegroups.com>
On Mar 14, 2:29=A0pm, s...@netherlands.com wrote:
> On Sat, 14 Mar 2009 13:16:37 -0700 (PDT), "C.DeRykuks" <c...@blv-sam-01.c=
a.boeing.com> wrote:
> >On Mar 11, 2:12=A0pm, luser-ex-troll <mijo...@yahoo.com> wrote:
> >> On Mar 11, 1:00=A0pm, sso <strongsilent...@gmail.com> wrote:
>
> >> > Hi, I need help figuring out the regex that would find if a word can
> >> > be made from another word.
> >> > For example
>
> >> > apple =A0could make =A0pal, lap, leap
> >> > it could not make all or peel
>
> >> > Suggestions?
>
> >> Regex might not be the best strategy here.
> >> I'd try counting the letters into a hash,
> >> keyed by letter. Then you can generate a similar
> >> hash for words to test and loop through the keys
> >> to check if 'apple' has the right number of each
> >> letter needed to make the word in question.
>
> >> This could be very slow.
>
> >Another regex possibility:
>
> >my $src =3D 'apple';
> >my $src_sort =3D join '', sort split //, $src;
> >my @targets =3D qw( pal lap leap all peel );
>
> =A0 =A0 ^^^^^^^^
> What is this? The example is ficticious. It has nothing
> to do with a solution, its just an example. Throw away the
> example, then answer the OP's question.
Check the OP's original question. I used his example.
--
Charles DeRykus
------------------------------
Date: Sun, 15 Mar 2009 00:29:44 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: regex for finding jumbled words
Message-Id: <Xns9BCED07E2925Casu1cornelledu@127.0.0.1>
"C.DeRykuks" <ced@blv-sam-01.ca.boeing.com> wrote in
news:5e9cf87b-bb56-48f2-a1fd-ed930523f185@b38g2000prf.googlegroups.com:
> On Mar 11, 2:12 pm, luser-ex-troll <mijo...@yahoo.com> wrote:
>> On Mar 11, 1:00 pm, sso <strongsilent...@gmail.com> wrote:
>>
>> > Hi, I need help figuring out the regex that would find if a word
>> > can be made from another word.
>> > For example
>>
>> > apple could make pal, lap, leap
>> > it could not make all or peel
>>
...
>> Regex might not be the best strategy here.
...
> Another regex possibility:
>
> my $src = 'apple';
> my $src_sort = join '', sort split //, $src;
> my @targets = qw( pal lap leap all peel );
>
> for my $target ( @targets ) {
> my $target_re = join '.*?',
> sort split //,$target;
> printf( "'%s' %s be made from '%s'\n\n",
> $target,
> $src_sort =~ /$target_re/
> ? 'can' : 'cannot', $src
> );
> }
Looks clever, but there is a significant disadvantage for what I
perceive to be the requested usage scenario. In your version, a new
regex needs to be computed from scratch each time a word is checked.
Anyhow, here is a version that overcomes that deficiency. I don't think
it would be very slow either.
#!/usr/bin/perl
use strict;
use warnings;
my $src = 'apple';
my $src_re = qr{
\A@{[ join '', map { "$_?" } sort split //, $src ]}\z
}x;
my @targets = qw( pal lap leap all peel );
for my $target ( @targets ) {
my $target_canon = join '', sort split //, $target;
printf( "'%s' %s be made from '%s'\n\n",
$target,
$target_canon =~ $src_re ? 'can' : 'cannot',
$src,
);
}
__END__
C:\DOCUME~1\asu1\LOCALS~1\Temp> s
'pal' can be made from 'apple'
'lap' can be made from 'apple'
'leap' can be made from 'apple'
'all' cannot be made from 'apple'
'peel' cannot be made from 'apple'
C:\DOCUME~1\asu1\LOCALS~1\Temp> t
Rate re with_hash re_o
re 9335/s -- -40% -43%
with_hash 15512/s 66% -- -6%
re_o 16469/s 76% 6% --
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw( cmpthese );
cmpthese -1, {
with_hash => sub {
my $src = 'apple';
my @targets = qw( pal lap leap all peel );
my %src;
++ $src{ $_ } for split //, $src;
my $hash_checker = sub {
my ($target) = @_;
my @target = split //, $target;
for my $x ( @target ) {
return unless exists $src{ $x };
return unless $src{ $x }--;
}
return 1;
};
for my $target ( @targets ) {
my $x = $hash_checker->( $target );
}
},
re => sub {
my $src = 'apple';
my $src_sort = join '', sort split //, $src;
my @targets = qw( pal lap leap all peel );
my $re_checker = sub {
my ($target) = @_;
my $target_re = join '.*?', sort split //,$target;
$src_sort =~ /$target_re/;
};
for my $target ( @targets ) {
my $x = $re_checker->( $target );
}
},
re_o => sub {
my $src = 'apple';
my $src_re = qr{
\A@{[ join '', map { "$_?" } sort split //, lc $src ]}\z
}x;
my @targets = qw( pal lap leap all peel );
for my $target ( @targets ) {
my $target_canon = join '', sort split //, lc $target;
my $x = ( $target_canon =~ $src_re );
}
},
};
__END__
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
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 2276
***************************************